Skip to main content

Hi,

I am trying to use the API to create a new item on an existing board, populated with column values. I can successfully create the item without column values, but as soon as I try to add them I get errors.


let query ='mutation { create_item (board_id: [boardid], group_id: [groupid], item_name: "Just put those column values in please", column_values: \\"{ \\"long_text\\": {\\"text\\":\\"Testing testing\\" }}\\") { id }}'

const response = await fetch('https://api.monday.com/v2/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': process.env.GATSBY_MONDAY_TOKEN,
},
body: JSON.stringify({
query: query
})
})

current error is: “Parse error on ": " (STRING) at t1, 153]”, but I have played around with so many different variations of escaping quotes, different fields, etc. that I have gotten different variations on that error. I can successfully pass column values using graphQL from Postman, just not from my JS code (in React/Gatsby). I also tried passing query variables following the responses to several other users’ issues, but in that case I always get “variable is referenced but not defined” errors.


Thanks for any help on this - I have been pulling my hair out for hours on this now!

Hi @samanthaff,


I would recommend logging your “query” to see if all your IDs are being passed in correctly.

Have you tried passing in the IDs using string interpolation? e.g. ${boardId}


Let me know what you find from these suggestions 🙂


Hi Alessandra - thanks for the suggestion. The board ID and group ID are being passed correctly - I actually have them hardcoded in my project and replaced them just for privacy in this post but I see how that could be unclear to look at. I am successfully able to create the item when the query consists of only board id, group id, and item name. As soon as I add the column_values key is when it breaks.


Hello @samanthaff,


I think you might be having issues because of how you are escaping quotes.


Can you please try the following?


column_values: "{ \\"long_text\\": {\\"text\\":\\"Testing testing\\"} }"


Please let me know if that worked for you!


Cheers,

Matias


Hi @Matias.Monday


I have a similar problem. Except that my request worked for weeks until yesterday.

There is my query :


$query =
'mutation {
create_item ( board_id: 409788845, group_id: "topics", item_name: "TEST MODULE",
column_values: "{
\\"texte5\\" : \\"test tex\\",
\\"texte\\" : \\"Dev\\",
\\"person\\" : \\"2556545674\\",
\\"connecter_les_tableaux7\\" : {\\"item_ids\\" : [14564564960]},
\\"date\\" : {\\"date\\" : \\"2023-03-30\\", \\"time\\" : \\"08:49:02\\"},
\\"date6\\" : {\\"date\\" : \\"2023-03-30\\", \\"time\\" : \\"10:25:00\\"},
\\"chiffres\\" : \\"1.6\\",
\\"chiffres0\\" : \\"372847239\\"
}") {
id
}
}';

This return me this error :


{
"errors": [
{
"message": "Parse error on bad Unicode escape sequence: \\"{\\\\n \\\\\\\\\\\\\\"texte5\\\\\\\\\\\\\\" : \\\\\\\\\\\\\\"test tex\\\\\\\\\\\\\\",\\\\n \\\\\\\\\\\\\\"texte\\\\\\\\\\\\\\" : \\\\\\\\\\\\\\"Dev\\\\\\\\\\\\\\",\\\\n \\\\\\\\\\\\\\"person\\\\\\\\\\\\\\" : \\\\\\\\\\\\\\"2556545674\\\\\\\\\\\\\\",\\\\n \\\\\\\\\\\\\\"connecter_les_tableaux7\\\\\\\\\\\\\\" : {\\\\\\\\\\\\\\"item_ids\\\\\\\\\\\\\\" : [14564564960]},\\\\n \\\\\\\\\\\\\\"date\\\\\\\\\\\\\\" : {\\\\\\\\\\\\\\"date\\\\\\\\\\\\\\" : \\\\\\\\\\\\\\"2023-03-30\\\\\\\\\\\\\\", \\\\\\\\\\\\\\"time\\\\\\\\\\\\\\" : \\\\\\\\\\\\\\"08:49:02\\\\\\\\\\\\\\"},\\\\n \\\\\\\\\\\\\\"date6\\\\\\\\\\\\\\" : {\\\\\\\\\\\\\\"date\\\\\\\\\\\\\\" : \\\\\\\\\\\\\\"2023-03-30\\\\\\\\\\\\\\", \\\\\\\\\\\\\\"time\\\\\\\\\\\\\\" : \\\\\\\\\\\\\\"10:25:00\\\\\\\\\\\\\\"},\\\\n \\\\\\\\\\\\\\"chiffres\\\\\\\\\\\\\\" : \\\\\\\\\\\\\\"1.6\\\\\\\\\\\\\\",\\\\n \\\\\\\\\\\\\\"chiffres0\\\\\\\\\\\\\\" : \\\\\\\\\\\\\\"372847239\\\\\\\\\\\\n }\\" (error) at [3, 32]",
"locations": [
{
"line": 3,
"column": 32
}
]
}
],


Here is how I send the data in PHP :


 $result = @file_get_contents($this->apiUrl, false, stream_context_create([
'http' => [
'method' => 'POST',
'header' => $this->headers,
'content' => json_encode(['query' => $query]),
]
]));

Again I want to specify that my request were working yesterday (I have tried with the exact data who works yesterday).


Could you take a look at this ?


Thanks.

Clement.



Now I get the error Parse error on “: {” (STRING) at I1, 156]


Hello @Cleement and welcome to the community!


I hope you like it here 💪


I have checked with the team and it looks like you might be seeing this errors because of a migration that was done on our end.


The new changes follow the GraphQL specifications more closely and patches lots of different gaps, just like strings with line endings.


To have a multiline string you need to use block strings """text""" , not "text".


You can also remove line endings from the query and keep it in one line.


You can also use GraphQL variables to put in any text as a JSON. It’s recommended to use this approach for big values, as this improves readability of queries.


I attached some examples of valid and invalid queries.


Valid:


Valid:


Invalid:


I hope that helps!


Hello again @samanthaff,


I just created a working example in Postman and exported it to JS:


Javascript - fetch:


var myHeaders = new Headers();
myHeaders.append("Authorization", "MYAPIKEYHERE");
myHeaders.append("Content-Type", "application/json");

var graphql = JSON.stringify({
query: "mutation { create_item (board_id: 1234567890, group_id: \\"topics\\", item_name: \\"Just put those column values in please\\", column_values: \\"{ \\\\\\"long_text\\\\\\": {\\\\\\"text\\\\\\":\\\\\\"Testing testing\\\\\\" }}\\") { id }}",
variables: {"myColumnValues":"{\\"text\\":\\"Some text\\"}"}
})
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: graphql,
redirect: 'follow'
};

fetch("https://api.monday.com/v2", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));

Javascript JQuery:


var settings = {
"url": "https://api.monday.com/v2",
"method": "POST",
"timeout": 0,
"headers": {
"Authorization": "MYAPIKEYHERE",
"Content-Type": "application/json",
},
"data": JSON.stringify({
query: "mutation { create_item (board_id: 1234567890, group_id: \\"topics\\", item_name: \\"Just put those column values in please\\", column_values: \\"{ \\\\\\"long_text\\\\\\": {\\\\\\"text\\\\\\":\\\\\\"Testing testing\\\\\\" }}\\") { id }}",
variables: {"myColumnValues":"{\\"text\\":\\"Some text\\"}"}
})
};

$.ajax(settings).done(function (response) {
console.log(response);
});

Let me know if that helps!


Cheers,

Matias


Thanks Matias, that worked for me.


I’m still not sure what the critical difference was between my original attempts and your fix - the extra backslashes around the column value key/values? Which step I stringified it? In any case, hoping I can successfully pattern match yours as I add more column values of different types.


Thanks again!


@Matias.Monday Although the hardcoded string example worked, I am now still having the same issue as before with passing query variables. I tried creating a columnValues variable as above in your fetch example, and substituted $columnValues in the query in place of the entire JSON. I get the error: “Variable $columnValues is used by anonymous mutation but not declared”


Hello again @samanthaff,


Sorry about that, if you want to use the variables, you have to define them like this:


var myHeaders = new Headers();
myHeaders.append("Authorization", "YOURAPIKEY");
myHeaders.append("Content-Type", "application/json");

var graphql = JSON.stringify({
query: "mutation ($columnValues: JSON!) { create_item (board_id: 1234567890, group_id: \\"topics\\", item_name: \\"Just put those column values in please\\", column_values: $columnValues) { id }}",
variables: {"columnValues":"{\\"text\\":\\"Some text\\"}"}
})
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: graphql,
redirect: 'follow'
};

fetch("https://api.monday.com/v2", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));


Let me know if you have any questions!


Cheers,

Matias


Reply