Mikael
Mikael

Reputation: 151

How to make Queries and Post to a board in Monday.com from R? (with answer)

I had this question and I couldn't find solutions here. So I decided to share the basics that I found.

Upvotes: 1

Views: 29

Answers (1)

Mikael
Mikael

Reputation: 151

I'll asumme you already have your token.

First of all you need httr and jsonlite to handle queries and posts, and json responses.

An example board from Monday.com with emphasis in the board id

Then, from the board you are interested, for example as the one in the image above, you need the id.


Then we need the id from each column (besides the item column), we use the following query

query <- '{
boards(ids: [7417519601]) {
name
columns {
  title
  id
  type
}
groups {
  title
  id
 }
}
}'

Then we send the POST (and I'll assume the response is successful)

response <- POST(
  url = "https://api.monday.com/v2",
  add_headers(Authorization = apiKey),
  body = list(query = query),
  encode = "json"
)

After converting the response, we check the column types and their id's

data <- content(response, as = "text")
data_json <- fromJSON(data, flatten = TRUE)
data_json$data$boards$columns |> as.data.frame() |> View()

View of the column types and id

Checking the queries in the API Playground is useful.


We also need the group id, which can be found with the query

query <- '
{
  boards (ids: 7417519601) {
    groups {
      id
      title
    }
  }
}
'

After the same response code and json handling as before, we check the group id

data_json$data$boards$groups |> as.data.frame() |> View()

id from existing groups in the board

Our group is called "Principal", but it's id is "topics"


Now we will post an item. Some types are initially tricky, so checking in the API Playground for existing items (I could add an example later if someone needs to) can be helpful too.

For this we need a mutation:

mutation <- sprintf('
mutation {
  create_item (
    board_id: 7417519601,
    group_id: "topics", 
    item_name: "An item 1",   #can be sent as parameter too
    column_values: \"{
    \\\"text__1\\\":\\\"%s\\\", 
    \\\"numbers__1\\\":\\\"%d\\\",
    \\\"date4\\\":\\\"%s\\\",
    \\\"email__1\\\": {\\\"text\\\": \\\"%s\\\" , \\\"email\\\": \\\"%s\\\" },
    \\\"status\\\":{\\\"index\\\":%s}
    }\") 
   {
    id
    name
  }
}
', "Neo", 42, "2024-09-11", "[email protected]", "[email protected]", 0
)

and its POST

response <- POST(
  url = "https://api.monday.com/v2",
  add_headers(Authorization = apiKey),
  body = list(query = mutation),
  encode = "json"
)

And here we have it successful post from R

  • We could have sent only some of the values. In that case it will show empty in the item
  • Email needs two parameters: the actual email and the text of how it will appear in the item's line. Here I'm sending the email twice,
  • Dates should be "YYYY-MM-DD"
  • Sometimes numbers can be sent as %s too, as long as is a number format (whereas a number or character variable), the column type will handle it as it is; And sometimes you'll be forced to use %s because R will tell you to...
  • Status. Here we used {\"index\":%s} and the 0 parameter that shows as "Dev" in the group. We could have also used {\"label\":\"%s\"} and "Dev" as the parameter

If the query has a typo or something, the response will be successful, but it won't reach the group.


Maybe this is too basic, but I wanna think there is someone out there that could use this as a start, even with chat-gpt around us.

Upvotes: 1

Related Questions