Skip to content

Instantly share code, notes, and snippets.

@yuhgto

yuhgto/sample.md Secret

Last active December 12, 2023 08:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuhgto/edb5d96e088599c2a6ea44860df9117b to your computer and use it in GitHub Desktop.
Save yuhgto/edb5d96e088599c2a6ea44860df9117b to your computer and use it in GitHub Desktop.
Uploading a file to the monday.com API, from basics

Uploading a file to monday.com, the hard way

In this example script, I construct a multipart request to upload a file via the monday.com API. The content variable stores my file in memory, so you can use a similar pattern if implementing file uploads in a storage-free environment (eg MuleSoft or AWS lambda).

Dependencies: node-fetch or some other npm library to make an HTTP request

var fs = require('fs');
var fetch = require('node-fetch'); // requires node-fetch as dependency

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

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

// set auth token and query
var API_KEY = "MY_API_KEY"
var query = 'mutation ($file: File!) { add_file_to_column (file: $file, item_id: 148203425, 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" : API_KEY
        },
        body: payload,
    };

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

Hi!

What should I put in boundary?

Kind regards!

@yuhgto
Copy link
Author

yuhgto commented Nov 23, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment