Ok, next challenge! I’d like to be able to upload a file via the Monday API from google app script (Googles javascript environment). I have basic infrastructure in place in google app script that is working well to create and modify Monday items. (Thanks to all that helped with some of the questions I had figuring this all out over the last few days!)
Looking at the Monday docs, I do see a javascript example that I don’t totally understand for uploading a file to an item. It looks like it uses
require(‘fs’);
require(‘node-fetch’)
which doesn’t exist in google’s javascript environment as fas as I can tell. So, anybody else written a complete app script example of how to upload a file? Any help appreciated!
Struggling through this by referencing and piecing together what I can. Any thoughts on this so far? compiles at least now without dependencies above that are not available in google app script. Returns {“error_message”:“Unsupported query”,“status_code”:400}.
function testMondayFileUpload() {
var files = DriveApp.getFilesByName("test.rtf");
var file = files.next();
// set filename
var upfile = 'test.rtf';
var query = 'mutation ($file: File!) { add_file_to_column (file: $file, item_id: 1234567890, column_id: "files") { id } }';
var boundary = "xxxxxxxxxx";
var data = "";
// construct query part
data += "--" + boundary + "\r\n";
data += "Content-Disposition: form-data; name=\"query\"; \r\n";
data += "Content-Type:application/json\r\n\r\n";
data += "\r\n" + query + "\r\n";
// construct file part
data += "--" + boundary + "\r\n";
data += "Content-Disposition: form-data; name=\"variables[file]\"; filename=\"" + upfile + "\"\r\n";
data += "Content-Type:application/octet-stream\r\n\r\n";
var payload = Utilities.newBlob(data).getBytes()
.concat(file.getBlob().getBytes())
.concat(Utilities.newBlob("\r\n--" + boundary + "--").getBytes());
var response = UrlFetchApp.fetch("https://api.monday.com/v2/file", {
method: 'post',
headers: {
'Content-Type': "multipart/form-data; boundary=" + boundary,
'Authorization': API_KEY,
'API-Version' : '2024-01'
},
payload: JSON.stringify({
'body': payload,
})
})
Logger.log(response);
}
I am having issues as well when using add_file_to_column… 2 days ago it was working perfectly, suddenly it stopped working and it returns “400 Unsupported query” everytime. If you actually find any solution, I would appreciate you sharing it, I’ll do my best too.
Yeah, a few seconds ago, I was able to make it work using Node.js! It will prompt you for a image path, and for a ID. Here is the complete file, just to make the call and send a simple image to a Monday table:
Now, I’m trying to use it on my original API, if I fix it, I’ll say something! But yeah, that code that I just sent you works, just replace it with your key!
thanks for posting. I’m not really able to translate most of that to google app script. So still just shooting in the dark here, which no real strategy on how to debug from here…
Well, if you want, you can leave here a little bit of your code or send it to me, I can try to help you debugging it, but never messed with Google App Scripts Maybe try giving ChatGPT or some free models around a try, maybe some can help you!
thats a nice offer. The key parts are all above. I believe that query is correct because it seems to be consistent with your example and the others that I have found in the documentation.
I don’t think the body payload is correct. It doesn’t seem like the examples are consistent.
Were you able to fix it?
All I know is that I used Postman, because Monday has an official collection to it, where they have the add_file_to_column API call working, and they have that “map” thing included, I guess it’s what they use to map correctly the uploaded file to the file variable on the query!
Its doesn’t seem like muteHttpExceptions : true does anything different…. So all I am getting is this result. Ran again just now, so this is from 4:50:19 PM pacific time today.
Exception: Request failed for https://api.monday.com returned code 400. Truncated server response: {“error_message”:“Unsupported query”,“status_code”:400} (use muteHttpExceptions option to examine full response)
Here is my full test code, based off of the javascript example from the docs:
var files = DriveApp.getFilesByName(“test.txt”);
var file = files.next();
Logger.log(file.getBlob().getDataAsString());
// set filename
var upfile = ‘./test.txt’;
var query = ‘mutation ($file: File!) { add_file_to_column (file: $file, item_id: 5668458954, column_id: “files”) { id } }’;
var boundary = “xxxxxxxxxx”;
var data = “”;
var map = {“file”:“variables.file”};
// construct query part
data += "--" + boundary + "\r\n";
data += "Content-Disposition: form-data; name=\"query\"; \r\n";
data += "Content-Type:application/json\r\n\r\n";
data += "\r\n" + query + "\r\n";
// construct map part
data += "--" + boundary + "\r\n";
data += "Content-Disposition: form-data; name=\"map\"; \r\n";
data += "Content-Type:application/json\r\n\r\n";
data += "\r\n" + JSON.stringify(map)+ "\r\n";
data += "--" + boundary + "\r\n";
data += "Content-Disposition: form-data; name=\"file\"; filename=\"" + upfile + "\"\r\n";
data += "Content-Type:application/octet-stream\r\n\r\n";
var payload = Utilities.newBlob(data).getBytes()
.concat(file.getBlob().getBytes())
.concat(Utilities.newBlob("\r\n--" + boundary + "--\r\n").getBytes());
@dlicc – couldn’t find anything interesting in our logs, sorry!
I do want to reproduce it in my account though. I haven’t gotten to it yet, I have been working on this big API migration we have. Will try and poke around today.