Python Trello API utility script

Python Trello API Util is a set of scripts to automate various Trello tasks like creating Trello Boards, assigning people to Board, creating cards, copying cads etc. It is built using Python Requests library and was part of a Hackathon held at Qxf2 Services.


Why this post?

In case you have used Trello, you will be familiar with the process of creating boards, cards, assigning people to cards etc using the Trello application. However, there are cases when you may need to repeat some of these tasks every week. The Trello util will help you to automate some of the repeating tasks. To follow along, we assume you have some familiarity with Trello and Requests API’s


Using Requests library

Trello provides a simple RESTful web API with which you can interact with. You can refer to Trello reference API docs for more details. We have used Python Requests module to make these REST calls.


What does Trello Util do?

You can find the complete Python Trello API utility script here. Below are some of the important utility methods along with the details.
1. Add Trello Boards: You can use this method to create new Trello board. The method takes a board name and returns success or failure based on the API response.

 def add_board(self,board_name):
        "Add board using board name"
        result_flag = False
        self.payload = self.auth.copy()
        self.payload['name'] = board_name
        self.payload['defaultLists'] = "false"
        url = self.url+"/boards/"
        response = requests.post(url=url,data=self.payload)
        if response.status_code == 200:
            result_flag = True
 
        return result_flag

2. Assign people to Board: You can use this method to assign team members to the Trello board. The method takes a board name and email ids of the members you want to add.

    def add_member_board(self,new_board_name,email_ids):
        " Add members to the board"
        result_flag = True
        board_id = self.get_board_id_by_name(new_board_name)
        url = self.url+"/boards/"+board_id +"/members"
        headers = self.headers
        for email_id in email_ids:
            self.payload = self.auth.copy()
            self.payload['email']= email_id
            response = requests.request("PUT", url, headers=headers, params=self.payload)
            if response.status_code == 200:
                result_flag &= True
            else:
                result_flag &= False
 
        return result_flag

3. Add list to board: All the cards will be added under a list or a swim lane. You can use this method to create a new list. The method takes a board name and list names to be added to the board.

 
    def add_list(self,board_name,list_names):
        "Add a list for a board"
        result_flag = True
        self.payload = self.auth.copy()
        board_id = self.get_board_id_by_name(board_name)
        for list_name in list_names:
            self.payload['name'] = list_name
            self.payload['idBoard'] = board_id
            url = self.url+"/lists/"
            response = requests.post(url=url,data=self.payload)
            if response.status_code == 200:
                result_flag &= True
 
        return result_flag

4. Copy cards: This method will be useful in case you want to copy cards from one board to other.

    def copy_card(self,org_board_name,org_card_names,new_board_name,new_list_name):
        "Copy card to board"
        result_flag = True
        self.card_payload = self.auth.copy()
        list_id  = self.get_list_id(new_board_name,new_list_name)
        self.card_payload['idList'] = list_id
        card_url = self.url+'/cards'
        for org_card_name in org_card_names:
            card_id = self.get_card_id(org_board_name,org_card_name)
            self.card_payload['idCardSource'] = card_id
            response = requests.post(url=card_url,params=self.card_payload)
            if response.status_code == 200:
                result_flag &= True
            else :
                result_flag &= False
 
        return result_flag

5. Change preference or visibility: This method can be used to set the preference level of the board.

    def change_preferences(self,board_name,pref):
        # change the visibility/preferences of a board
        result_flag = True
        board_id = self.get_board_id_by_name(board_name)
        url = self.url+"/boards/"+board_id
        self.payload = self.auth.copy()
        self.payload['prefs/permissionLevel'] = pref    
        response = requests.put(url=url,params=self.payload)
        if response.status_code == 200:
                result_flag &= True
        else:
            result_flag &= False
        return result_flag

Sample test

We have a sample test file which was used to automate some weekly tasks which we do like
1. Get the board names from conf
2. Get the existing Trello board names
3. Add a new board from the conf file by making sure the board was not added before
4. Add members to the board
5. Add a list (swim lane) to board
6. Copy card to the new board from a sample board
7. Assign member to the copied board
8. Change preference or visibility (public, private or org )


Setup and running test

Once you clone the repo, update the Trello API Key and Token in conf.py file. After that update the data like board names, card names you want to create etc in data_conf.py. Then run the test file using below command

python test_trello_util.py

What next?

Now you know how to use Requests library to automate some API Trello tasks. There are a lot more tasks which can be automated and in case you liked this project and want to contribute, you are free to make changes and create Pull Requests. Looking forward to your support…

One thought on “%1$s”

Leave a Reply

Your email address will not be published. Required fields are marked *