Board ID as a variable in a query

I am trying to use a variable in the API to query a board using Python.
I want to use the variable to replace the board id.
I can successfully do a mutation using a variable for the board id, but I can’t get the query right.

This one works:

import requests
import json

apiKey = “**********************************************************”
apiUrl = “https://api.monday.com/v2
headers = {“Authorization” : apiKey}

query = ‘{boards (ids: 590638178) { name id description items { name column_values{title id type text } } } }’
data = {‘query’ : query}
r = requests.post(url=apiUrl, json=data, headers=headers) # make request

print(r.json())

Data returned:

{‘data’: {‘boards’: [{‘name’: ‘M1-Weekly Schedule’, ‘id’: ‘**********’, ‘description’: ‘Board Owner: *******. Only **** can change data in Monday (dates etc). \nView & Comment Access: All Management & Supervisors. \n\nFunction: MYOB Exo - Monday.com will only collect any new jobs “Pending Commencement” in MYOB Exo and place them in the “New Jobs” group. \n\nPhase 2 - Man Hours / Labour / Equipment Type’, ‘items’:…

This doesn’t work:

import requests
import json

apiKey = “**********************************************************”
apiUrl = “https://api.monday.com/v2
headers = {“Authorization” : apiKey}

board_id = 590638178

query = ‘($myBoardID: Int!) {boards (ids: $myBoardID) { name id description items { name column_values{title id type text } } } }’
vars = {‘myBoardID’ : board_id}
data = {‘query’ : query, ‘variables’ : vars}

r = requests.post(url=apiUrl, json=data, headers=headers) # make request

print(r.json())

No data is returned:
{‘errors’: [{‘message’: ‘Parse error on “(” (LPAREN) at [1, 1]’, ‘locations’: [{‘line’: 1, ‘column’: 1}]}], ‘account_id’:…

However, I can successfully use a variable for board ID in a mutation e.g.

query = 'mutation ($myItemName: String!, $myBoardID: Int!, $columnVals: JSON!) { create_item (board_id:$myBoardID, …

If you’re sending a query with no variables, you can omit the “query” keyword from your request and it will work:

{boards(limit:1){id name} }

However, when you’re using variables, you will need to include the query keyword (since variables are arguments on the base “query” field. You will also need to send the board ID inside a list (or declare your variable as a list).

Try this:

query = ‘query($myBoardID: Int!) {boards (ids: [$myBoardID]) { name id description items { name column_values{title id type text } } } }’
2 Likes

Hi Dipro,

Thanks, that almost worked and it got me very close.
I also had to send the $myBoardID as a list:

query = ‘query($myBoardID: Int!) {boards (ids: [$myBoardID]) { name id description …

Thanks again.

Great. I updated my solution in case anyone else has the same issue!

1 Like