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

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.

2 answers

1 vote

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!

Hi Caity,

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

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jun 20, 2018 in Trello

Get inspired with Trello & earn free Gold

A blank Trello board can be both a beautiful and intimidating thing. How many lists should I have? Is creating 10 labels overkill? How can I best track my progress? Is there a way...

16,766 views 0 14
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you