node.jsimagetrello

Trello create card attachment error: 'File too large' with 8kb image


Attempting to use Trello's Create card attachment with a 8kb PNG file, I am getting a 'File too large' error in return.

code sample:

    const image = await sharp(
      "path/qrcode.png"
    )
      .resize(200, 200)
      .webp({ quality: 20 })
      .toFormat("png")
      .toBuffer();

    // * CREATE NEW CARD WITH LIST ID
    await axios
      .post(
        `https://api.trello.com/1/cards?idList={LISTKEYHERE}&key=${TRELLO_KEY}&token=${TRELLO_TOKEN}`,
        {
          name: "Create new card",
          pos: "top",
        }
      )
      .then((res) => {
        const id: any = res.data.id;

        axios.post(
          `https://api.trello.com/1/cards/${id}/attachments?key=${TRELLO_KEY}&token=${TRELLO_TOKEN}`,
          { file: image }
        );
      });

I've read from other injuries that the free version of trello allows a 10mb upload for attachments, the image being 8kb should be well under that limit.

I also tested to see if the front end of trello boards allows the image to be manually attached, the same file works fine from the UI end.

Notes: Instead of making the separate call for creating an attachment, I also attempted the key value parameters for the initial call for creating a card's (fileSource) as well. Same error.


Solution

  • As we have already talked about it on the Trello support ticket, please let me share here a nodejs code example to upload images to a Trello card using fetch API:

    import fetch from 'node-fetch';
    import FormData from 'form-data';
    import fs from 'fs';
    
    const TRELLO_KEY = "TRELLO_KEY_HERE"
    const TRELLO_TOKEN = "TRELLO_TOKEN_HERE"
    const CARD_ID = "CARD_ID_HERE"
    
    const form = new FormData();
    form.append('file', fs.createReadStream('qrc.png')); //set here the filename
    
    fetch(`https://api.trello.com/1/cards/${CARD_ID}/attachments?key=${TRELLO_KEY}&token=${TRELLO_TOKEN}`, {
      method: 'POST',
      headers: {
        'Accept': 'application/json'
      },
      body: form
    })
      .then(response => {
        console.log(
          `Response: ${response.status} ${response.statusText}`
        );
        return response.text();
      })
      .then(text => console.log(text))
      .catch(err => console.error(err));