Exploring Trello's Public API with JavaScript

Trello is a powerful organizational tool that allows teams to manage projects, tasks, and workflows. The platform offers a robust set of API endpoints that developers can leverage to access Trello's features programmatically.

In this blog post, we'll explore how to access Trello's Public API using JavaScript and provide examples to get you started.

Getting Started

Before we dive into the code, let's first obtain an API key and token.

  1. Head over to the Trello Developer site and sign up for a free account.

  2. Click on API Key and copy the key to your clipboard.

  3. Click on the Token tab and create a new token. Copy the token to your clipboard.

Now that we have our API key and token, let's move on to exploring some of Trello's API endpoints.

Retrieving a Board

To retrieve information about a specific board, we can use the following code:

const apiKey = "YOUR_API_KEY";
const token = "YOUR_API_TOKEN";

const boardId = "BOARD_ID";

const url = `https://api.trello.com/1/boards/${boardId}?key=${apiKey}&token=${token}`;

fetch(url)
  .then(response => {
    if (response.ok) {
      return response.json();
    }
    throw new Error("Error retrieving board");
  })
  .then(data => console.log(data))
  .catch(error => console.error(error));

Make sure to replace YOUR_API_KEY, YOUR_API_TOKEN and BOARD_ID with the relevant values. The fetch function is used to make the request. If the response is ok, we parse the response data using the json() method. Finally, we log the data to the console.

Creating a Card

To create a card on a board, we can use the following code:

const apiKey = "YOUR_API_KEY";
const token = "YOUR_API_TOKEN";

const boardId = "BOARD_ID";
const listId = "LIST_ID";

const url = `https://api.trello.com/1/cards?key=${apiKey}&token=${token}`;
const payload = JSON.stringify({
  name: "New Card",
  idList: listId,
  idBoard: boardId,
});

const headers = {
  "Content-Type": "application/json",
};

fetch(url, {
  method: "POST",
  headers: headers,
  body: payload,
})
  .then(response => {
    if (response.ok) {
      return response.json();
    }
    throw new Error("Error creating card");
  })
  .then(data => console.log(data))
  .catch(error => console.error(error));

Make sure to replace YOUR_API_KEY, YOUR_API_TOKEN, BOARD_ID and LIST_ID with the relevant values. We construct a payload object that contains the card's properties, including the name, listId, and boardId. We then use the fetch function to send a POST request, along with the payload and headers with the request's content type set to application/json. If the response is ok, we log the response data to the console.

Updating a Card

To update a card's properties, we can use the following code:

const apiKey = "YOUR_API_KEY";
const token = "YOUR_API_TOKEN";

const cardId = "CARD_ID";

const url = `https://api.trello.com/1/cards/${cardId}?key=${apiKey}&token=${token}`;
const payload = JSON.stringify({
  desc: "Updated description",
  name: "Updated name",
});

const headers = {
  "Content-Type": "application/json",
};

fetch(url, {
  method: "PUT",
  headers: headers,
  body: payload,
})
  .then(response => {
    if (response.ok) {
      return response.json();
    }
    throw new Error("Error updating card");
  })
  .then(data => console.log(data))
  .catch(error => console.error(error));

Make sure to replace YOUR_API_KEY, YOUR_API_TOKEN, and CARD_ID with the relevant values. The payload object contains the properties to be updated, including the name and desc. We use the fetch function to send a PUT request, along with the payload and headers with the request's content type set to application/json. If the response is ok, we log the response data to the console.

Conclusion

In this blog post, we covered the basics for using Trello's Public API with JavaScript. We showed how to retrieve boards, create cards, and update cards' properties. This is just the beginning as the Trello API offers an extensive functionality to explore. You can refer to Trello's developer documentation for more information, and use these examples as a starting point for building your own applications that use Trello's powerful organizational features.

Related APIs

Public APIs — A directory of free and public apis

Built by @mddanishyusuf