Link Column Query

Hi Everyone,

I’m trying to use the API to input a url generated from on board elements into a link column in python. I’ve C&P’d my query as you can see the url and text for the link column are variables I’m unsure how to actually write the query so that the API understand what I’m sending.

query = 'mutation ($boardID:Int!,$itemID:Int!,$colID:String!,$linkVal: JSON!) { change_multiple_column_values(item_id:[$itemID],board_id:[boardID],column_values:$linkVal)}'
    vars = {
        'boardID':boardID,
        'itemID':itemID,
        'colID': colID,
        'linkVal': {
            'colID': {'url': itemURL, 'text': itemName}
        }
    }

Full Code:

boardID= myBoardID

query = 'query ($boardID:Int!) {boards(ids:[$boardID]){items {id name}}}'
vars={
    'boardID': boardID
}

data = {'query': query, 'variables': vars}
r = requests.post(url=apiUrl, json=data, headers=headers)  # make request
itemsInjest=r.json()
itemsDict=itemsInjest['data']
itemsVal=itemsDict['boards']
itemsURL = []
boardID=str(boardID)
for items in itemsVal:
    for itemsIDs in items['items']:
        url= str("https://acep-space.monday.com/boards/"+ boardID+ "/pulses/"+ itemsIDs['id'])
        itemsURL.append([int(itemsIDs['id']),itemsIDs['name'],url])

boardID = int(boardID)


query = 'query ($boardID:Int!) {boards(ids:[$boardID]){items(limit:1){column_values{id type}}}}'
vars = {
    'boardID': boardID
}

data = {'query': query, 'variables': vars}
r = requests.post(url=apiUrl, json=data, headers=headers)  # make request

colInjest = r.json()
colDict = colInjest['data']
colItems = colDict['boards']
for items in colItems:
    for colVals in items['items']:
        break
    break
colData = colVals['column_values']

i = 0
while i < len(colData):
    colVal = colData[i]
    colType = colVal['type']
    if colType == 'link':
        colID = colVal['id']
    i += 1

i=0
while i<len(itemsURL):
    itemData=itemsURL[i]
    itemID=itemData[0]
    itemName=itemData[1]
    itemURL=itemData[2]
    query = 'mutation ($boardID:Int!,$itemID:Int!,$colID:String!,$linkVal: JSON!) { change_multiple_column_values(item_id:[$itemID],board_id:[boardID],column_values:$linkVal)}'
    vars = {
        'boardID':boardID,
        'itemID':itemID,
        'colID': colID,
        'linkVal': {
            'colID': {'url': itemURL, 'text': itemName}
        }
    }

    data = {'query': query, 'variables': vars}
    r = requests.post(url=apiUrl, json=data, headers=headers)  # make request
    print(r.json())
    i+=1

Doing some more testing the issue seems to be within how python formats quotes when passing this data

output = '{"query":"mutation{change_multiple_column_values(item_id:$myItemID, board_id:$myBoardID, column_values:\"{\\\"link9\\\" : {\\\"url\\\" : \\\"https://acep-space.monday.com/boards/$myBoardID/pulses/$myItemID\\\", \\\"text\\\" : \\\"Sandbox Task 13\\\"}}\"){id}}"}'
test = output
print(test)


query = '"mutation{change_multiple_column_values(item_id:$myItemID, board_id:$myBoardID, column_values:\"{\\\"link9\\\" : {\\\"url\\\" : \\\"https://acep-space.monday.com/boards/$myBoardID/pulses/$myItemID\\\", \\\"text\\\" : \\\"Sandbox Task 13\\\"}}\"){id}}"'
test = query
print(test)

query = "mutation{change_multiple_column_values(item_id:$myItemID, board_id:$myBoardID, column_values:\"{\\\"link9\\\" : {\\\"url\\\" : \\\"https://acep-space.monday.com/boards/$myBoardID/pulses/$myItemID\\\", \\\"text\\\" : \\\"Sandbox Task 13\\\"}}\"){id}}"
data = {"query": query}
test = (data)
print(test)

The top format is what is expected by Monday, the middle format was used to test various implementations of passing the query, the final format is what I’m using so that I can also pass variables into the final request in this the escape characters are formatted incorrectly.

Hi @NeelPi!

It looks like you have more slashes than necessary. The column values should be formatted like this, for example:

column_values: "{\"status\": \"Done\"}"

Specifically with the link column type what would be the best way pass variables into the post request?

I have tried multiple ways of providing column values and the json parser in python seems to not be able to understand what is being sent.

I ask because this is what the API is looking for

query: mutation {change_multiple_column_values(item_id: 2973227073, board_id: 2973227049, column_values: "{\"link\" : {\"url\" : \"\", \"text\" : \"\"}}") { id} }

however I want to use mutations since the values for columnID, url, and text are variable and determined by my code from other queries so my final query would look something like

query: mutation ($boardID:Int!,$itemID:Int!,$colID:String!,$itemURL:String!,$itemName:String!,$linkVal: JSON!) {change_multiple_column_values(item_id:[$itemID], board_id:[$boardID],column_values:[$linkVal]){id}}, variables: {'boardID': boardID, 'itemID': itemID, 'colID': colID, 'itemURL': itemURL, 'itemName': itemName, 'linkVal': "{ [$colID] : {"url" : [$itemURL], "text": [$itemName]}}"}

or

query: mutation ($boardID:Int!,$itemID:Int!,$colID:String!,$itemURL:String!,$itemName:String!) {change_multiple_column_values(item_id:[$itemID], board_id:[$boardID],column_values:"{[$colID] : {\"url\" : [$itemURL], \"text\" : [itemName]}}"){id}}, variables: {'boardID': boardID, 'itemID': itemID, 'colID': colID, 'itemURL': itemURL, 'itemName': itemName}

However with either solution I am unable to understand where to place the escape characters so that the json parser understands and I get to use my variables as inputs.

Solution: I ended up having to remove the square brackets around my variables in the mutation and passed the value for linkVal through JSON.dumps.

For anyone curious this was how the POST request query had to be set up:

    vars = {
        'boardID':boardID,
        'itemID':itemID,
        'linkVal': json.dumps({
            colID: {'url': itemURL, 'text': itemName}
        })
    }

And this is the full code:

import requests
import json

apiKey = "myAPIKey” 
piUrl = https://api.monday.com/v2
headers = {"Authorization": apiKey}

boardID = myBoardID

query = 'query ($boardID:Int!) {boards(ids:[$boardID]){items{id name}}}'
vars={
    'boardID': boardID
}

data = {'query': query, 'variables': vars}
r = requests.post(url=apiUrl, json=data, headers=headers)  # make request
itemsInjest=r.json()
itemsVal=itemsInjest['data']['boards']
boardID=str(boardID)
itemsURL=sum(([
    [
        int(itemsIDs['id']),
        itemsIDs['name'],
        f'https://acep-space.monday.com/boards/{boardID}/pulses/{itemsIDs["id"]}'
    ]
    for itemsIDs in items['items']] for items in itemsVal),[])

boardID = int(boardID)

query = 'query ($boardID:Int!) {boards(ids:[$boardID]){items(limit:1){column_values{id title type}}}}'
vars = {
    'boardID': boardID
}

data = {'query': query, 'variables': vars}
r = requests.post(url=apiUrl, json=data, headers=headers)  # make request

colInjest = r.json()
colData=colInjest['data']['boards'][0]['items'][0]['column_values']
colID = [colVal['id'] for colVal in colData if colVal['type'] == 'link' and colVal['title'] == 'Email Link'][0]

for itemID,itemName,itemURL in itemsURL:
    query = 'mutation ($boardID:Int!,$itemID:Int!,$linkVal: JSON!) { change_multiple_column_values(item_id: $itemID, board_id: $boardID, column_values: $linkVal){id}}'
    vars = {
        'boardID':boardID,
        'itemID':itemID,
        'linkVal': json.dumps({
            colID: {'url': itemURL, 'text': itemName}
        })
    }

    data = {'query': query, 'variables': vars}
    r = requests.post(url=apiUrl, json=data, headers=headers)  # make request