How to attach file to Trello card without using html forms.

Gmanik446 June 29, 2017

The file parameter documentation for the POST /1/cards/[card id or shortlink]/attachments API call does not specify how the file should be encoded. For "Valid Values" it simply says "A file". If we are using html forms, then we can attach file using below code.

<form action="https://api.trello.com/1/cards/REPLACE_WITH_CARD_ID/attachments"
      method="POST" enctype="multipart/form-data">
   <input type="hidden" name="key" value="REPLACE_WITH_YOUR_KEY" />
   <input type="hidden" name="token" value="REPLACE_WITH_YOUR_WRITE_TOKEN" />
   <input type="file" name="file">
   <input type="submit" value="Upload">
</form>

But I am not using html forms. I need to call Trello card attachment API from server side of Node JS. I was wondering if anyone has a working example of how to get files into Trello cards using the API.

3 answers

1 vote
Caity
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 30, 2017

Hi Garaga!

We do have examples of a few different ways of attaching files here, although these mostly use form data.

 

You should be able to use the API to attach any kind of file that you would be able to attach using the Trello interface itself. If attaching a file from, say, your computer, then the value for "file=" would be the path to that file.

Let us know if you have any questions!

Gmanik446 July 11, 2017

Hi Caity,

Thank you for your answer. I used form-data node module to upload the file from server side of node.js.

0 votes
Mohammad Abdurraheem September 18, 2018

I used rest API to upload any kind of file from server side using nodejs

 

 var _attachFile = function(req,res){  
var _file = req.body.file_path;
var file_name = file.split('/')[file.split('/').length - 1];
var __fileData = fs.createReadStream(_file);
 var options = { 
method: 'POST',
url: 'https://api.trello.com/1/cards/' + jsonData.card_id + '/attachments/',
     qs:{       
         key : req.body.auth.key,
         token: req.body.auth.token            
},
        headers:{
                 mimeType: 'application/octet-stream', //means dont know what the hell is this content
                'Content-Type': 'application/octet-stream' 
            },
        formData:{
                file: { 
                       value: __fileData,                   
     options: {
                                filename: file_name, 
                                contentType: 'application/octet-stream'
                            }  
                  }  
          } 
   };  
  request(options, function (error, response, body) { 
       if (error) { 
           return res.send({ 'error': 'Fail' });
        } else if (response.statusCode != 200) {  
          return res.send({'error': body }) 
        } else {
          return res.send('success');   
      }  
  });
}

 

0 votes
Gmanik446 July 11, 2017

I used form-data node module to upload the file from server side of node.js.

var FormData = require('form-data');
var formData = new FormData();
var appkey = "Trello App key";
var token = "Trello Access Token";
var fileLocation = "Ypur file location";
var cardId = "Trello card id";

formData.append("key", appkey);
formData.append("token", token);
formData.append("file", fs.createReadStream(fileLocation));

var requestObj = request.post('https://trello.com/1/cards/' + cardId + '/attachments', attachmentCallback);
requestObj._form = formData;

function attachmentCallback(err, httpResponse, body) {
 if (httpResponse.statusCode == 200) {
   console.log("successfully attached the file " );
 } else {
    console.log('Could not attach the file to card:', httpResponse.statusMessage);
 }
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events