File Uploads Failing

Hi All,

Having a nightmare here, I’ve tried in python, node.js and curl and I cannot upload a file via the API.

Here is a basic curl request I’m using: curl --location ‘https://api.monday.com/v2/file
–header ‘Authorization: Bearer REDACTED’
–form ‘variables[file]=@/path/to/your/file.jpg’
–form ‘query=mutation($file: File!) { add_file_to_column(item_id: 8123268638, column_id: “text”, file: $file) { id } }’

according to the docs this should work, but all I get is:

{
“status_code”: 500,
“error_message”: “Internal server error”,
“error_code”: “INTERNAL_SERVER_ERROR”,
“errors”: [
{
“message”: “Internal server error”,
“extensions”: {
“status_code”: 500,
“error_code”: “INTERNAL_SERVER_ERROR”
}
}
]
}

no matter what implementation I use, is there an issue with the API?

Hi @nocodecreative.io and welcome to the community!

I have created a Python example for you here (the asset is in the same folder as the Python file)(take into account you need the ID of a files column for this):

import requests

# Set file name
upfile = 'free-images.jpg'

# Set query
query = """
mutation ($file: File!) {
    add_file_to_column(file: $file, item_id: YOURITEMIDHERE, column_id: "YOURFILESCOLUMNIDHERE") {
        id
    }
}
"""

# Set URL and boundary
url = "https://api.monday.com/v2/file"
boundary = "xxxxxxxxxx"

try:
    # Read the file content
    with open(upfile, 'rb') as file:
        file_content = file.read()

    # Construct multipart/form-data payload
    data = [
        ('query', (None, query, 'application/json')),
        ('variables[file]', (upfile, file_content, 'application/octet-stream')),
    ]

    # Set headers
    headers = {
        "Authorization": "YOURAPITOKENHERE",
    }

    # Make the POST request
    response = requests.post(url, files=data, headers=headers)

    # Log the response
    print(response.json())

except FileNotFoundError:
    print(f"Error: The file {upfile} was not found.")
except requests.RequestException as e:
    print(f"Error: {e}")

Here is a JS example:

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

// set filename
var upfile = 'myImagesName.jpg';

// set query
var query = 'mutation ($file: File!) { add_file_to_column (file: $file, item_id: YOURITEMIDHERE, column_id: "YOURFILESCOLUMNIDHERE") { 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);
        process.exit(1); // Exit with error code if file read fails
    }

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

    // make request
    fetch(url, options)
      .then(res => res.json())
      .then(json => {
        console.log(json);
        process.exit(0); // Exit normally after logging the response
      })
      .catch(err => {
        console.error(err);
        process.exit(1); // Exit with error code if fetch fails
      });
});

I hope that helps!

Cheers,
Matias