Automation: Creating Boards, Lists, and Cards Using Python and the Trello API
Joann is a software developer who specializes in automating repetitive tasks.
Introduction
In this article, I will guide you through creating boards, lists, and cards in Trello but instead of doing all of these manually from Trello's website or mobile application, we'll do this programmatically using Python and the Trello API.
This may not make sense to you if you don't use Trello that much or if you only need to create a few cards at a time but this allows you to integrate the creation of your Trello items with your other programs. To give a few examples, I will write a few articles to give you ideas on where you can apply this automation. The following articles will be published after this one:
- Exporting action items from Gmail emails to Trello
- Planning vacation schedules using Trello and BeautifulSoup
Requirements
Python
I'm using Python 3.6.8 but you can use other versions. Some of the syntax could be different especially for Python 2 versions.
Trello API Key And Token
You need the key and token to connect and make requests to your Trello account. Sign in to your Trello account from the browser and follow the instructions to get your key and token. Take note of the your key and token.
Creating Boards
Replace the "your_key" and "your_token" strings in the code below with the key and token for your Trello account. The create_board() method creates a board with the given name and returns the ID of the board upon its creation.
We are returning the ID of the board created because we will use it later on to create a list within the board.
import requests
key = "your_key"
token = "your_token"
def create_board(board_name):
url = "https://api.trello.com/1/boards/"
querystring = {"name": board_name, "key": key, "token": token}
response = requests.request("POST", url, params=querystring)
board_id = response.json()["shortUrl"].split("/")[-1].strip()
return board_id
Creating Lists
Add the method below to the same script. This one is for creating a list. As mentioned earlier, we will need the board ID to let the API know which board we want to create the list in so the method definition below takes the "board_id" as a parameter along with the "list_name".
This method will return the ID of the list created which will then be used later on to create cards within the list.
def create_list(board_id, list_name):
url = f"https://api.trello.com/1/boards/{board_id}/lists"
querystring = {"name": list_name, "key": key, "token": token}
response = requests.request("POST", url, params=querystring)
list_id = response.json()["id"]
return list_id
Creating Cards
Add the method below to the same script. This one is for card creation. It takes the "list_id" and "card_name" as parameters.
def create_card(list_id, card_name):
url = f"https://api.trello.com/1/cards"
querystring = {"name": card_name, "idList": list_id, "key": key, "token": token}
response = requests.request("POST", url, params=querystring)
card_id = response.json()["id"]
return card_id
Sample Automation
You can test each method out and try simple tasks like creating boards, lists, and cards but that's a bit boring. Let's try doing a simple automation based on the script we created. First, save the script as "trello.py" and create two text files of tasks that you want to appear on your board.
Below are some sample files including the script we created earlier.
trello.py
import requests
key = "your_key"
token = "your_token"
def create_board(board_name):
url = "https://api.trello.com/1/boards/"
querystring = {"name": board_name, "key": key, "token": token}
response = requests.request("POST", url, params=querystring)
board_id = response.json()["shortUrl"].split("/")[-1].strip()
return board_id
def create_list(board_id, list_name):
url = f"https://api.trello.com/1/boards/{board_id}/lists"
querystring = {"name": list_name, "key": key, "token": token}
response = requests.request("POST", url, params=querystring)
list_id = response.json()["id"]
return list_id
def create_card(list_id, card_name):
url = f"https://api.trello.com/1/cards"
querystring = {"name": card_name, "idList": list_id, "key": key, "token": token}
response = requests.request("POST", url, params=querystring)
card_id = response.json()["id"]
return card_id
chores.txt
Wash the dishes
Throw out the trash
Pick-up laundry
Buy groceries
Cook dinner
work.txt
Review the code for <some task>
Test the code for <some project>
Write documentation
Tasks to Trello
Copy the code below to a file called "tasks_to_trello.py".
In this code, the following things are happening:
- The "os" module is imported
- The "trello.py" file is imported together with its methods
- The board "Tasks" is created
- The "os" module's listdir() method is used to list the files in the current directory
- The list of files are filtered in with those ending with ".txt"
- The filename is retrieved excluding its file extension so it can be used as the list name
- The list is created within the board, the title() method is called to capitalize the list name (i.e. "work" becomes "Work")
- The file is accessed and each line in the file is created as cards in their specific list
tasks_to_trello.py
import os
from trello import create_board, create_list, create_card
board_id = create_board("Tasks")
for filename in os.listdir():
if filename.endswith(".txt"):
filename = os.path.splitext(filename)[0]
list_name = create_list(board_id, filename.title())
with open(f"{filename}.txt", "r") as txt_file:
for card_name in txt_file.readlines():
create_card(list_name, card_name)
Finally
When you access your Trello, you will find the board, lists, and cards you created like in the screenshot below. There are so many things you can do with this simple program (trello.py) if you combine it with other programs that fetch information from several sources. As I mentioned earlier, I will be posting separate articles on the following:
- Exporting action items from Gmail emails to Trello
- Planning vacation schedules using Trello and BeautifulSoup
© 2019 Joann Mistica
Comments
someone on August 13, 2020:
Good article, thank you very much
Joann Mistica (author) on July 27, 2020:
Hi Leandro,
I'm not sure if I understand your question correctly but this is for the "Automation: Exporting Action Items from Gmail Emails to Trello" article, right? You can append the date to the action item (card) itself but if that's unpleasant to look at, you can add labels for them so that even if you move them across different lists and boards, you can still see which date they were from.
You can do this using Python as well, more information can be found here: https://developer.atlassian.com/cloud/trello/rest/...
I hope this answers your question.
leandro mourĂŁo on July 26, 2020:
Very nice these examples! Congratulations!
Abou "Actions", how could I grasp their dates when a list is moved to another?