Powershell to create an item

New to Monday.com, and I’m trying to use the API to create an item. I’ve tried with SQL server and PowerShell and always get a 500 error back.
I’ve read, and tried the code from this post:
Using PowerShell to create an item in Monday.com?
with no luck. I’ve tried and re-tried using different formatting, but I’m sure the issue is with $query below (tried with escaping the quotes etc.).

$apiKey = “xxxxx”
$boardId = “1234567890”

Set the header with your API key

$headers = @{
“Authorization” = $apiKey
“Content-Type” = “application/json”
}

Define the query to create an item

$query = "
mutation{
createItem1: create_item (board_id: 1234567890, item_name:““Test Item 1"”) {
id
name
}
}”

Execute the request

$response = Invoke-RestMethod -Uri ‘https://api.monday.com/v2/’ -Method Post -Headers $headers -Body $query

Any help would be appreciated. This seems like it should be pretty easy, but I cannot get it to work and the error isn’t much help. I can query the board (return data) so I know the authentication works, and inserting using the API playground works so I think the query (mutation) is right (have tried adding “query:” before mutation as well). But I can’t get anything other 500 back.

you need to serialize the body to JSON using ConvertTo-Json, the body should be an object like:

$body = {query: $query}

My powershell is very rusty I should say - so its possible (likely) that Invoke-RestMethod serializes the body so no specific steps are required.

1 Like

Thanks. I tried with/without ConvertTo-Json:

$body = {query: $query | ConvertTo-Json}

$response = Invoke-RestMethod -Uri ‘https://api.monday.com/v2/’ -Method Post -Headers $headers -Body ($body)

with
$query=
mutation {
create_item(
board_id: “123456789”
item_name: “New Item 123”
column_values: “{“text”:“This is a new item”}”
) {
id
}
}

I can copy/paste the $query text into the playground and it works, but always get:
Invoke-RestMethod : The remote server returned an error: (500) Internal Server Error.

Hello there @Rich and welcome to the community!

I hope you like it here :muscle:

I do not have experience with that language, but this might be related to escaping outer quotes. Here is a cURL example you can use (if you want, you can import it to Postman, and then “translate” it to the language you want.

curl --location 'https://api.monday.com/v2/' \
--header 'Content-Type: application/json' \
--header 'API-Version: 2023-10' \
--header 'Authorization: APITOKENHERE' \
--data '{"query":"mutation { create_item( board_id: 1234567890 item_name: \"New Item 123\" column_values: \"{\\\"text\\\":\\\"This is a new item\\\"}\") {id}}"
}'

I hope that helps!

Cheers,
Matias

1 Like

Thanks - using postman helped, it was related to escaping quotes. I have a proof of concept working, though automating the whole process may take a lot of work.
Thanks for help.

1 Like

Hello again @Rich, happy to help!!

1 Like