Skip to main content

Hi everyone, I’ve hit a snag while trying to upload a file to a column using the Monday SDK. Below is the snippet I’m working with:


import initMondayClient from 'monday-sdk-js';
import { Logger } from '@mondaycom/apps-sdk';
import path from 'path';
import fs from 'fs';
import FormData from 'form-data';

export async function changeColumnValue(token, itemId, columnId) {
const formData = new FormData();
const filePath = path.join(__dirname, 'test.txt');
formData.append('variablesbfile]', fs.createReadStream(filePath), 'test.txt');
formData.append(
'query',
`mutation ($file: File!) {
add_file_to_column (item_id: ${itemId}, column_id: "${columnId}", file: $file) {
id
}
}`,
);

try {
const response = await fetch('https://api.monday.com/v2/file', {
method: 'POST',
headers: {
Authorization: token,
...formData.getHeaders(),
},
body: formData,
});

const data = await response.json();
console.log(data);
} catch (error) {
console.error(error);
throw new Error(error.message);
}
}

When I run this, I encounter the following error:

{ “error_message”: “Unsupported query”, “status_code”: 400 }


I’m using Node v18.0.0 and have taken this code as a base from the quickstart-integrations-monday documentation, with minor adjustments to test some functionalities. The goal here is to craft a function that receives itemId, columnId, and a token, and uploads a file located on my server to the specified column/item.


Would appreciate any insights or suggestions on what I might be doing wrong or missing. Thanks in advance!

Hello there @ethanolle1,


I replied in the ticket you opened 😁


Cheers,

Matias


For anyone interested, here is a working function thanks to @Matias.Monday help.


export async function changeColumnValue(token, boardId, itemId, columnId) {
// make string item id and column id
itemId = JSON.stringify(itemId);
// columnId = JSON.stringify(columnId);
// set filename current path images.png
var upfile = path.join(__dirname, 'images.png');
// set query
var query = `mutation ($file: File!) { add_file_to_column (file: $file, item_id: "${itemId}", column_id: "${columnId}") { id } }`;
// set URL and boundary
var url = 'https://api.monday.com/v2/file';
var boundary = 'xxxxxxxxxx';

try {
const content = await fs.promises.readFile(upfile);
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';
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 = Buffer.concat([Buffer.from(data, 'utf8'), new Buffer.from(content, 'binary'), Buffer.from('\\r\\n--' + boundary + '--\\r\\n', 'utf8')]);

// construct request options
var options = {
method: 'post',
headers: {
'Content-Type': 'multipart/form-data; boundary=' + boundary,
Authorization: token,
},
body: payload,
};

// make request
const response = await fetch(url, options);
const json = await response.json();
console.log(json);
} catch (err) {
console.error(err);
}
}


Happy to help @ethanolle1 !!!


Reply