Has anyone been able to use the add_file_to_column to upload files?

Hello!
3 days ago my tool was working perfectly, when I uploaded the image to it, it used my own API that is connected to Monday, to send the image to a Monday table. It fetches automatically the ID of the table, and the ID of the column is always the same. I am running it like this, on my API:

app.post('/upload-file', upload.single('file'), async (req, res) => {
    if (!req.file) {
        return res.status(400).send('No file uploaded.');
    }

    // Extract partId from the query string
    const partId = req.query.peca; // This is the board / table ID, it is available on the URL
    if (!partId) {
        return res.status(400).send('Part ID is missing.');
    }

    const formData = new FormData();

    // GraphQL mutation with a variable for the file
    const mutation = `
        mutation ($file: File!) {
            add_file_to_column (item_id: ${partId}, column_id: "arquivos", file: $file) {
                id
            }
        }
    `;

    // Append the mutation and the file to the formData
    formData.append('query', mutation);
    formData.append('variables[file]', fs.createReadStream(req.file.path), {
        filename: req.file.originalname,
        contentType: req.file.mimetype,
    });

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

        const result = await response.json();
        if (result.data) {
            logWithTimestamp('File uploaded successfully to Monday.com', result);
            res.send('File uploaded successfully');
        } else {
            logWithTimestamp('Error uploading file to Monday.com', JSON.stringify(result.errors));
            res.status(500).send('Error uploading file to Monday.com');
        }
    } catch (error) {
        logWithTimestamp('Error in file upload', error.message); // Log the error message
        res.status(500).send('Error in file upload');
    } finally {
        fs.unlink(req.file.path, (err) => {
            if (err) logWithTimestamp('Error deleting file:', err);
        });
    }
});

It stopped working from a day to the other, did something change? Am I doing something wrong? It always returns “Unsupported query” now…

Thank you!

My guess here is that you built this some time ago and are using a version of the api which has been withdrawn.

I think you can ask for an extension of the old version of the api on your account while you update the code to the new version.

1 Like

Hello David, thank you very much for your answer, means a lot.

Makes a lot of sense, but even if I remove the version from there (like the Postman call from the Postman collection), it still has the same output, with or without version :frowning:

Thank you!

1 Like

It’s weird because there is another user here with problems using add_file_to_column… It was working just perfectly a few days ago, and suddenly, it stopped working… I really really would appreciate any help, and wouldn’t mind sharing more details if needed, after hours and hours and hours of debugging, I really don’t know what to do anymore.

Already created sample Node.js scripts to try to query it, making it every way possible, and everytime I just keep getting 400 Unsupported Query… :frowning:

Hello there @imjoaofernandes,

This is the script I use for uploading files. Perhaps it is of use:

import fs from 'fs';
import fetch from 'node-fetch';

// adapted from: https://gist.github.com/tanaikech/40c9284e91d209356395b43022ffc5cc

// set filename
var upfile = 'images.png';

// set auth token and query
var API_KEY = "MYAPITOKENHERE"
var query = 'mutation ($file: File!) { add_file_to_column (file: $file, item_id: 11111111, column_id: "files") { id } }';

// set URL and boundary
var url = "https://api.monday.com/v2/file";
var boundary = "xxxxxxxxxx";
var data = "";

fs.readFile(upfile, function(err, content){

    // simple catch error
    if(err){
        console.error(err);
    }

    // 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 = 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" : "MYAPITOKENHERE"
        },
        body: payload,
    };

    // make request
    fetch(url, options)
      .then(res => res.json())
      .then(json => console.log(json));
});

If you still have issues, please contact us via this form and our team will take a look into it!

Cheers,
Matias

1 Like

Hi, Martias
I tried your code, but it’s still returning error.


This is Error displayed as an HTML file.


This is error.

please teach me how to upload file.
Thank you.

What messages do you see in the html returned?

Perhaps there is a clue in the returned html.

That is odd @Bolt,

What happens if you use this cURL (changing the relevant information for your, obviously):

curl --location 'https://api.monday.com/v2/file/' \
--header 'Authorization: APITOKENHERE' \
--header 'API-version: 2023-10' \
--form 'query="mutation add_file($file: File!) {add_file_to_column (item_id: ITEMIDHERE, column_id:\"files\" file: $file) {id}}
"' \
--form 'map="{\"image\":\"variables.file\"}
"' \
--form 'image=@"/Users/matias/Downloads/myfile.png"'

Thank you for your reply, @Matias.Monday

I just used your command, it works,
but is that okay if I should remove the api-version?

Hello again @Bolt,

Yes! You can. No need for it actually.

Thanks, @Matias.Monday
Then how does it work when your api version is upgraded?
Maybe I should upgrade my command?

Hello again @Bolt,

I am not sure I understand the question. The API version in this case is irrelevant since the same mutation works for all present API versions and it works with the 2024-01 (current version):grin:

Really thank you for your kindness reply.
Once the api version is upgraded and if I have a problem with my query, I will ask you again.
Thank you

Hello again @Bolt,

You can avoid having issues with the new versions by checking our release notes here :grin:

This worked for me. Thank you!
Monday should link to your example.