Basic PHP API v2 Example

Here’s a basic snippet of PHP that lets me execute queries against the v2 API and return the data as JSON. I hope it can provide a leg up and save someone time as they get started.

Just enter your own API key, change the URL to match yours, and put in your query.

$token = '[Personal API Token]';
$tempUrl = "https://[MYTEAM].monday.com/v2/";

$query = '  {
                users{
                    id 
                    email
                    name
                }
            }';
$headers = ['Content-Type: application/json', 'User-Agent: [MYTEAM] GraphQL Client', 'Authorization: ' . $token];
$data = @file_get_contents($tempUrl, false, stream_context_create([
    'http' => [
        'method' => 'POST',
        'header' => $headers,
        'content' => json_encode(['query' => $query]),
    ]
]));
$tempContents = json_decode($data, true);
3 Likes

Hey @jsachief – this is amazing!

Thanks for sharing :100:

thanks, I’ve tried to create a board with a group “test” that contains a simple title and a note column (type:text). I want to add an item with this query below.
It works, but it doesn’t add the value in the text column “note”. the column stays empty… :frowning:

$token = ‘[Personal API Token]’;
$tempUrl = “https://[MYTEAM].monday.com/v2/”;

$query = ’
mutation {
create_item (board_id: xxxxxxxxx, group_id: “test”, item_name: “new item”, column_values: “{“note”: “a easy text here”}” ) {
id
}
}
';

$headers = [‘Content-Type: application/json’, ‘User-Agent: [MYTEAM] GraphQL Client’, 'Authorization: ’ . $token];
$data = @file_get_contents($tempUrl, false, stream_context_create([
‘http’ => [
‘method’ => ‘POST’,
‘header’ => $headers,
‘content’ => json_encode([‘query’ => $query]),
]
]));
$tempContents = json_decode($data, true);

hey @thomasvandenbulcke,
first of all, lets make sure that you have the correct column id for the notes column.
you can try out this query via our GraphiQL try it yourself interface to see all columns of that board:

query{
  boards (ids: xxxxxxxxx){
    columns{
      id
      title
      type
    }
  }
}

now that you are sure you have the correct column ID lets look at the mutation itself.
since the column values are a string inside a string they need to be escaped, like so:
column_values: "{\"note\": \"a easy text here\"}"

let us know if it worked :slight_smile:

I have used this Code to create a pulse/item and change columns values in a board. It works well. I am working on moving changing the code from the API v1 to API v2. In v1 when I create a new pulse/item in a group it places the new pulse/item at the top of the group.

I noticed with v2 it places the pulse/item at the bottom of the group. Is there anyway to make a new pulse/item go to the top like it does with v1?

Hey @renow,
Currently it is not possible to add a new item to the top of the group.
API v2 works similar to the way you add items in the monday platform using the “New” button or the empty item row marked “+Add”.

i copied the code “as is” , only replaced token and MYTEAM text, but it does not work for me . I do not know where a mistake is

Hey @Ayelet
Thanks for the response. I have another question. How do I insert variables in this code? I would like to pass $_POST data from a web form to this code so it can dynamically add the value to the column with the variable $columnValue. How do I go about doing this?

$token = ‘[Personal API Token]’;
$tempUrl = “https://[MYTEAM].monday.com/v2/”;

//Place a Web form Name field value into a variable to add to a monday.com column
$$columnValue = $_POST[‘name’];

$query = ’
mutation {
create_item (board_id: xxxxxxxxx, group_id: “test”, item_name: “new item”, column_values: “{“Name”: “$columnValue”}” ) {
id
}
}
';

$headers = [‘Content-Type: application/json’, ‘User-Agent: [MYTEAM] GraphQL Client’, 'Authorization: ’ . $token];
$data = @file_get_contents($tempUrl, false, stream_context_create([
‘http’ => [
‘method’ => ‘POST’,
‘header’ => $headers,
‘content’ => json_encode([‘query’ => $query]),
]
]));
$tempContents = json_decode($data, true);

Hey @renow!

You can dynamically update variables by including references to variables in your query and then passing the actual data for these variables in another “variables” JSON string in the request body. For example, this request would let you dynamically populate the board ID, item name, and columns in a mutation.

Query string (“query” value in request body)

mutation ($board: Int!, $name: String!, $column: JSON!) { 
  create_item( board_id: $board, item_name: $name, column_values:$column) { 
    id 
    column_values { 
      id 
      value 
    } 
  } 
} 

Variables (“variables” value in request body)

{"board": 139415582, 
"name": "Hello world", 
"column" : "{\"status\" : {\"index\" : 5}}"
} 

For more context, the GraphQL website has a great introduction to this: https://graphql.org/learn/queries/#variables

Alex, what error are you receiving?

Hi Dipro, thanks for coming back . I am new in php and have a feeling that have made some basic mistake, here is my code:

<?php $token = '[ here is my token ]'; $tempUrl = 'https://[whatpresents].monday.com/v2/'; $query=' mutation { create_item (board_id: 231718005, item_name: "new ABC customer", column_values: "{\"note\": \"test text \"}" ) { id } } ' ; $headers = ['Content-Type: application/json', 'User-Agent: [whatpresents] GraphQL Client', 'Authorization:' . $token]; $data = @file_get_contents($tempUrl, false, stream_context_create([ 'http' => [ 'method' => 'POST', 'header' => $headers, 'content' => json_encode(['query' => $query]), ] ])); $tempContents = json_decode($data, true); ?>

mutation snippet works fine through “…/v2/try-it-yourself” page.

Hey @alextop – are you using “https://[whatpresents].monday.com/v2” as the URL, or is this dummy data? This is not a valid URL, I’d suggest using https://api.monday.com/v2 :slight_smile:

1 Like

thanks !
i have replaced the url but is is something else (

Are you receiving an error from our server? If you are, it indicates your query isn’t formatted correctly and I can help you figure out what the problem with it is.

If your code isn’t sending a request to our server at all, that means there’s an issue with your code (as opposed to the query). I’m not super familiar with PHP and can’t really help you debug syntax errors.

To figure out if the issue is with your query or the code itself, let’s try a simple query first (before moving to a mutation). Can you try the code below and let me know if it works?

Ah @alextop – did you fix your code, as per this message?

Can you tell me what was wrong?

Hi Dipro,
yes I have fixed , the reason was that I missed CURLOPT_HTTPHEADER information.
I have another question. When I create a new item and populate columns, I can fill the cells with Text type, but not with Long Text format. IDs of columns are correct (taken from query). Has someone complained on similar issue with Long Text format ?

Oh nice!

The long text column has a different structure as the text column – check our documentation at developers.monday.com for a full example.

Cheers!
Dipro

hi Dipro,
i have found and fixed. Thanks a lot for your support !
Cheers,
Alex

mutation ($name: String!) {
create_item ( board_id: 201725833, group_id: “lost”,
item_name: $name,
column_values:
“{
“text7”:“NY” ,
“numbers60”:“2” ,
“delivery_address”:{“text”:“address text is here”} ,
“email”:{“email”:“test@test.com”,“text”:“test@test.com”}
}”
) { id } }

{
“name” : “new user”
}

Hello,
the code works fine with constant “item_name” (like: item_name: “superuser”. However, once i declare variable $name, (the code is above) i got parse error. What am I doing wrong with variables value, or something else ?

Hey Alex! The query structure looks correct. Are you sending the variables as a variables value in your HTTP request?

Your request’s body should look like this:
{ “query” : [your query here],
“variables” : [your variables here] }