Issues with creating an item Via API

Hi, I am new to the world of Monday.com and am having an issue trying to connect my custom form on my wix website to a board I have setup in monday.com.

I can create an item with a name, no problem but when I try to add a column value it simply wont work from my javascript request. This exact same query works from the monday API playground though.

Help!

mutation { create_item (board_id: XXX, group_id: “XXX”, item_name: “tests from wix”, column_values: “{“status”: {“label”:“Sign-Up”}}”) { id }}

If it helps here is the full function

function submitToMonday() {
	let query = "mutation { create_item (board_id: XXX, group_id: XXX, item_name: \"direct from wix\", column_values: “{\“status\”: {\“label\”:\“Sign-Up\”}}”) { id }}";

	fetch ("https://api.monday.com/v2", {
	method: 'post',
	headers: {
		'Content-Type': 'application/json',
		'Authorization' : 'XXX.XXX'
	},
	body: JSON.stringify({
		query : query
	})
	})
	.then(res => res.json())
	.then(res => console.log(JSON.stringify(res, null, 2)));
}

Hello @qcasburn,
If you are creating a status label that doesn’t already exist on your board, you will have to add the create_labels_if_missing: true argument. Without that, your mutation will throw an error. Additionally, it is necessary to use the right quotation marks. The quotation marks for your column values aren’t valid.
Your query will therefore look like this:

let query = "mutation { create_item (board_id: XXX, group_id: XXX, create_labels_if_missing: true, item_name: \"direct from wix\", column_values: "{\"status\": {\"label\":\"Sign-Up\"}}") { id }}";

Hi @kolaai
Thank you so much for your response. The column “status” is already on my board and I have been able to post to it via the API playground before. It only gives me issues when I try to make the same request via javascript on my client side script.

I updated my code with your suggestion but now I receive a javascript syntax error.

The syntax error states a missing semicolon at the end of column_values:

let query = "mutation { create_item (board_id: XXX, group_id: XXX, create_labels_if_missing: true, item_name: \"direct from wix\", column_values: "{\"status\": {\"label\":\"Sign-Up\"}}") { id }}";

Hey @qcasburn,

I’d love to help with this! To me, it seems like you are not escaping the column values correctly, in this case. I’d recommend using GraphQL variables in order to store your column values, and then JSON.stringify those, instead of opting to escape JSON manually.

Here’s an example that will populate the following column types with data:

  • Date
  • Dropdown
  • Status
  • Text
  • Numbers
let query4 = 'mutation ($myItemName: String!, $boardId: Int!, $column_values: JSON!) { create_item (board_id:$boardId, item_name:$myItemName, create_labels_if_missing: true, column_values: $column_values) { id } }';
let today = new Date().toISOString().split("T")[0]
let vars = 
{"myItemName" : "Hello, world!",
"boardId":**YOUR_BOARD_ID**,
"column_values": JSON.stringify({
"status":"New request",
"numbers":100,
"dropdown":"Size M",
"text":"Next day delivery would be great",
"date4" : today})}


fetch ("https://api.monday.com/v2", {
  method: 'post',
  headers: {
    'Content-Type': 'application/json',
    'Authorization' : 'YOUR_API_KEY'
  },
  body: JSON.stringify({
    'query' : query4,
    'variables' : JSON.stringify(vars)
  })
})
  .then(res => res.json())
  .then(res => console.log(JSON.stringify(res, null, 2)));

And the result:

I hope this helps!

-Alex

Hey @AlexSavchuk!
Thank you so much for that, it works perfect!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.