Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trello API: get members, attachments, and card information in one call?

Tags:

php

trello

I can get data from the Trello API using this:

private function get_card_info($card_id) {
    $client =         new \GuzzleHttp\Client();
    $base =           $this->endpoint . $card_id;
    $params =         "?key=" . $this->api_key . "&token=" . $this->token;      
    $cardURL =        $base . $params;
    $membersURL =     $base . "/members" . $params;
    $attachmentsURL = $base . "/attachments" . $params;

    $response = $client->get($cardURL);
    $this->card_info['card'] = json_decode($response->getBody()->getContents());

    $response = $client->get($membersURL);
    $this->card_info['members'] = json_decode($response->getBody()->getContents());

    $response = $client->get($attachmentsURL);      
    $this->card_info['attachments'] = json_decode($response->getBody()->getContents());
}

However, this is broken into three calls. Is there a way to get card information, the member information, and the attachment information all in one call? The docs mention using &fields=name,id, but that only seems to limit what's returned from the base call to the cards endpoint.

It's absurd to have to hit the API 3 times every time I need card information, but I can't find any examples gathering all that's needed.

like image 488
Matthew Johnson Avatar asked Sep 23 '16 21:09

Matthew Johnson


People also ask

How do I view attachments in trello?

You can preview image and video attachments within Trello without having to download them by clicking on the thumbnail of the attachment from the card back to open the preview.

Does trello provide API?

Trello provides a simple RESTful web API where each type of resource (e.g. a card, a board, or a member) has a URI that you can interact with. The Trello API documentation is available at https://developer.atlassian.com/cloud/trello.

Can you download attachments from trello?

Just type in "card: <your card name>", select all using the checkbox at the top left then click the download button and you'll export all your files as a zip.


2 Answers

Try hitting the API with following parameters:

/cards/[id]?fields=name,idList&members=true&member_fields=all&& attachments=true&&attachment_fields=all

like image 173
Vladimir Cvetic Avatar answered Nov 09 '22 16:11

Vladimir Cvetic


Trello replied to me, and stated that they would have answered much like Vladimir did. However, the only response I got from that was the initial card data, sans attachments and members. However, they also directed me to this blog post that covers batching requests. They apparently removed it from the docs because of the confusion it created.

To summarize the changes, you essentially make a call to /batch, and append a urls GET parameter with a comma separated list of endpoints to hit. The working final version ended up looking like this:

private function get_card_info($card_id) {
    $client =         new \GuzzleHttp\Client();
    $params =         "&key=" . $this->api_key . "&token=" . $this->token;

    $cardURL = "/cards/" . $card_id;
    $members = "/cards/" . $card_id . "/members";
    $attachmentsURL = "/cards/" . $card_id . "/attachments";

    $urls = $this->endpoint . implode(',', [$cardURL, $members, $attachmentsURL]) . $params;

    $response = $client->get($urls);
    $this->card = json_decode($response->getBody()->getContents(), true);
}
like image 27
Matthew Johnson Avatar answered Nov 09 '22 16:11

Matthew Johnson