Monday.com API: Inserting record into group fails

I want to take a portion of the data from a registration form from a web app, create an item in an existing board, and then insert it into a Mysql database.

Can someone tell me where I’m going wrong? I’m not getting a PHP error code, and the API is not returning an error code.

As a starting point, I have used the example of Creating a new item with column values populated in the API-Quickstart-Tutorial-PHP.

This code below fails. Following the code is the GraphQL query and results for reference.

<?php
  #insert record
  $firstName      = 'Johnny';
  $lastName       = 'Smith';
  $contactEmail   = 'info@mee.com';
  $contactPhone   = '726-222-3333';
  $companyName    = 'Johnny Contractor';
  $companyAddress = '123 Main Street';
  $companyCity    = 'Dixon';
  $companyState   = 'MI';
  $companyPostal  = '45451';
  $companyEmail   = 'info@jcons.com';
  $companyPhone   = '555-999-9999';
  $date           = date('Y-m-d');

  // **insert into Monday.com**
  $token = 'API_KEY';
  $apiUrl = 'https://api.monday.com/v2';
  $headers = ['Content-Type: application/json', 'Authorization: ' . $token];
  $incentive_users_id = BOARD_ID;

  $query = 'mutation ($myItemName: String!, $columnVals: JSON!) { create_item (board_id:BOARD_ID, item_name:$myItemName, column_values:$columnVals) { id } }';
  $vars = ['myItemName' => $companyName, 
  'columnVals' => json_encode([
    'person'  => [ 'people' =>'Scott Steward' ],
    'status' => ['label' => 'New Lead'], 
    'date' => ['date' => $date],
    'text' =>[ 'text' => $firstName ],
    'text8' =>[ 'text' => $lastName ],
    'email' =>[ 'email' => $contactEmail ],
    'phone' =>[ 'phone' => $contactPhone ],
    'text1' =>[ 'text' => $companyAddress ],
    'text7' =>[ 'text' => $companyCity ],
    'text6' =>[ 'text' => $companyState ],
    'text0' =>[ 'text' => $companyPostal ],
    'email_1' =>[ 'email' =>$companyEmail ],
    'phone_1' =>[ 'phone' => $companyPhone ]
  ])];

  $data = @file_get_contents($apiUrl, false, stream_context_create([
  'http' => [
  'method' => 'POST',
  'header' => $headers,
  'content' => json_encode(['query' => $query, 'variables' => $vars]),
  ]
  ]));    
?>

// **GraphQL query**:
query { 
  boards (ids: BOARD_ID) {
    columns {
      title
      id
      type
    } 
  } 
} 

// **GraphQL results**:
{
  "data": {
    "boards": [
      {
        "columns": [
          {
            "title": "Name",
            "id": "name",
            "type": "name"
          },
          {
            "title": "Person",
            "id": "person",
            "type": "multiple-person"
          },
          {
            "title": "Status",
            "id": "status",
            "type": "color"
          },
          {
            "title": "Date",
            "id": "date4",
            "type": "date"
          },
          {
            "title": "First Name",
            "id": "text",
            "type": "text"
          },
          {
            "title": "Last Name",
            "id": "text8",
            "type": "text"
          },
          {
            "title": "Email",
            "id": "email",
            "type": "email"
          },
          {
            "title": "Phone",
            "id": "phone",
            "type": "phone"
          },
          {
            "title": "Street Address",
            "id": "text1",
            "type": "text"
          },
          {
            "title": "City",
            "id": "text7",
            "type": "text"
          },
          {
            "title": "State",
            "id": "text6",
            "type": "text"
          },
          {
            "title": "Zipcode",
            "id": "text0",
            "type": "text"
          },
          {
            "title": "Company Email",
            "id": "email_1",
            "type": "email"
          },
          {
            "title": "Company Phone",
            "id": "phone_1",
            "type": "phone"
          }
        ]
      }
    ]
  },
  "account_id": 9999999
}

Hello there @mark.stout ,

Would you be able to please send this over to appsupport@monday.com so we can take a closer look?

Please test this again now, and send in the email the timestamp of when you tested it including:

  1. Date
  2. Hour
  3. Minute
  4. Your location
  5. Full query
  6. Full response
  7. The user ID of the user whose API token is being used
  8. The board ID

Looking forward to hearing from you via email!

Cheers,
Matias

1 Like

Hoe can I test this on postman?

Hello again @mark.stout ,

Here are examples of how this looks like in Postman:

You can check how the format for each column type is here.

Cheers,
Matias

1 Like

This is being called from a PHP file.

I’m getting an errror beginning with ‘text’ =>[ ‘text’ => $firstName ],

I get the following error: “error_message”:“invalid value, . . . “error_data”:{“column_value”:”{"text"=>"Johnny"}",“column_type”:“TextColumn”}}

What is this error telling me and how would I correct it?

The actual call:

include("file.php"); // holds the API authorization key as $token and board id

$query = 'mutation ($myItemName: String!, $incentive_users_id: Int!, $columnVals: JSON!) { create_item (board_id: $incentive_users_id, item_name:$myItemName, column_values:$columnVals) { id } }';
$vars = ['myItemName' => $companyName, 'incentive_users_id' => $incentive_users_id,
  'columnVals' => json_encode([
    'status'  => [ 'label' => 'New Lead' ], 
    'date4'   => [ 'date'  => $date ],
    'text'    => [ 'text'  => $firstName ],
    'text8'   => [ 'text'  => $lastName ],
    'email'   => [ 'email' => $contactEmail],
    'phone'   => [ 'phone' => $contactPhone]
])];

$data = @file_get_contents($apiUrl, false, stream_context_create([
  'http' => [
  'method' => 'POST',
  'header' => $headers,
  'content' => json_encode(['query' => $query, 'variables' => $vars]),
  ]
]));

$responseContent = json_decode($data, true);

echo json_encode($responseContent);

Any updates?

Also, what should I be using? ID or TYPE when updfating a column?

Hello there @mark.stout,

I am not experienced using PHP but I just recreated this in Postman as an example for you:

Query:

mutation ($myItemName: String!, $incentive_users_id: ID!, $columnVals: JSON!) {
  create_item (board_id: $incentive_users_id, item_name:$myItemName, column_values:$columnVals) { id }
}

Variables:

{
    "myItemName": "My Name",
    "incentive_users_id": 1234567890,
    "columnVals":"{\"status\": \"New Lead\", \"date4\": \"2023-04-24\",\"text\": \"Some text\",\"text8\": \"Some text\",\"email\": \"example@example.com This is an example email\",\"phone\": \"+19175998722 US\"}"
}

cURL:

curl --location 'https://api.monday.com/v2' \

--header 'Authorization: YOURAPITOKEN' \

--header 'API-Version: 2023-10' \

--header 'Content-Type: application/json' \

--data-raw '{"query":"mutation ($myItemName: String!, $incentive_users_id: ID!, $columnVals: JSON!) {\n create_item (board_id: $incentive_users_id, item_name:$myItemName, column_values:$columnVals) { id }\n} \n \n","variables":{"myItemName":"My Name","incentive_users_id":1234567890,"columnVals":"{\"status\": \"New Lead\", \"date4\": \"2023-04-24\",\"text\": \"Some text\",\"text8\": \"Some text\",\"email\": \"example@example.com This is an example email\",\"phone\": \"+19175998722 US\"}"}}'

PHP HTTP request 2:

<?php
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('https://api.monday.com/v2');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setConfig(array(
  'follow_redirects' => TRUE
));
$request->setHeader(array(
  'Authorization' => 'YOURAPITOKEN',
  'API-Version' => '2023-10',
  'Content-Type' => 'application/json',
));
$request->setBody('{"query":"mutation ($myItemName: String!, $incentive_users_id: ID!, $columnVals: JSON!) {\\n  create_item (board_id: $incentive_users_id, item_name:$myItemName, column_values:$columnVals) { id }\\n} \\n \\n","variables":{"myItemName":"My Name","incentive_users_id":1234567890,"columnVals":"{\\"status\\": \\"New Lead\\", \\"date4\\": \\"2023-04-24\\",\\"text\\": \\"Some text\\",\\"text8\\": \\"Some text\\",\\"email\\": \\"example@example.com This is an example email\\",\\"phone\\": \\"+19175998722 US\\"}"}}');
try {
  $response = $request->send();
  if ($response->getStatus() == 200) {
    echo $response->getBody();
  }
  else {
    echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
    $response->getReasonPhrase();
  }
}
catch(HTTP_Request2_Exception $e) {
  echo 'Error: ' . $e->getMessage();
}

PHP Guzzle:

<?php
$client = new Client();
$headers = [
  'Authorization' => 'YOURAPITOKEN',
  'API-Version' => '2023-10',
  'Content-Type' => 'application/json',
];
$body = '{"query":"mutation ($myItemName: String!, $incentive_users_id: ID!, $columnVals: JSON!) {\\n  create_item (board_id: $incentive_users_id, item_name:$myItemName, column_values:$columnVals) { id }\\n} \\n \\n","variables":{"myItemName":"My Name","incentive_users_id":1234567890,"columnVals":"{\\"status\\": \\"New Lead\\", \\"date4\\": \\"2023-04-24\\",\\"text\\": \\"Some text\\",\\"text8\\": \\"Some text\\",\\"email\\": \\"example@example.com This is an example email\\",\\"phone\\": \\"+19175998722 US\\"}"}}';
$request = new Request('POST', 'https://api.monday.com/v2', $headers, $body);
$res = $client->sendAsync($request)->wait();
echo $res->getBody();

I hope that helps!

Cheers,
Matias

1 Like