How To Create a Trello Card from Apps Script
All without using a 3rd party application like Zapier or IFTTT
If you are a Google Workspace and Trello power user like me, I’m sure the thought has come across your mind of connecting your Google Apps to Trello, but every time you search for a solution you get an ad for Zapier or IFTTT. I try to avoid these solutions because they are great at making an initial connection, but if you ever want to do anything advanced, it will require you to sign up for a premium subscription. After searching and not finding answers, I decided just to try and build it for myself.
What you will be able to do by the end of this tutorial
- Create a Trello Card from Apps Script
Prerequisites
- Familiarity with JavaScript
- Understanding of APIs
- Google Account
- Trello Account
Get Trello Key & Token
In order to start programmatically interfacing with Trello, you are going to need to prove that you are who your computer says you are, Trello does this by forcing all of your API requests to come with a Key and an Auth Token.
To get the Key and your Auth Token, you need to go to https://trello.com/app-key you will want to copy the key into a notes app on your computer, you will need it later.
Next, you need to generate a token. On the screenshot above you click on the link that says Token. This will bring you to a new page.
This will then open another page that has your Token. Like with your Key, you will want to copy this onto a Notes app on your computer for a later step.
Set up Apps Script Environment
Open a new Google Sheet, and in the top toolbar, select Extensions and select Apps Script (image below). This will open a new tab which is your Apps Script Environment.
In the top left of this screen, you will see an auto-generated name Untitled Project, you can change that to Trello Integration — Script. Then on the left side of the screen, you will see a toolbar, and on the bottom, there is a gear icon. Select that, and on the new page, scroll to the bottom where you will see Script Properties. This is where we are going to store your Key and Token we got from the last step.
Now, let’s ensure that we can access our key & token securely in our script. Go to the Script Editor section by selecting the < > on the left toolbar.
const key = ScriptProperties.getProperty(‘key’);const token = ScriptProperties.getProperty(‘token’);function myFunction() { console.log(key); console.log(token)}
In the top toolbar you will see a ▷Run, click this and you should expect to see your key and token in the console at the bottom of the screen.
Creating our URL
If you go to Trello’s API Docs they list out how to add a new card. It requires a URL with the listID, key, and token. You can also pass other information in the body of your request that will further customize the card, but we will get to that later.
https://api.trello.com/1/cards?idList=5abbe4b7ddc1b351ef961414&key=APIKey&token=APIToken
We have the key and token, but we are missing the idList. The way we can get the idList is by:
- Getting a card that is in the list that we want to add new cards to
- On the bottom right part of the card, there is a button that says Share, click that and then select Export JSON
When you click that, you will get a bunch of text that looks overwhelming. I recommend you install JSON Formatter from the Chrome App Store, it will make it a lot easier to read through. You can do ⌘ + F or Ctrl + F and type idList and you will want to copy that value.
let url = `https://api.trello.com/1/cards?idList=${idList}&key=${key}&token=${token}`;
Build our Request
So we will use this URL in our request to Trello’s API, but to get it to do anything useful we need to pass some data into our request to make something happen. We will do this by creating an options variable and setting some values. As you can see, we are making a POST request (API jargon).
let options = { method: ‘post’, payload:{ name:’Card Added from Apps Script’, desc: ‘Description from Card added in Apps Script’, }}
Use URLFetchApp to Make Request
Apps Script has a built-in function to make API calls and we will use that to call Trello’s API. We will pass in the URL & options that we created in earlier steps and set the value equal to response. This will allow us to see what is returned when we make the request. Then I am going to log the response code (expect 200 or 201) and the Content of the API Response.
let response = UrlFetchApp.fetch(url,options);console.log(response.getResponseCode())console.log(response.getContentText())
Full Code
const key = ScriptProperties.getProperty(‘key’);const token = ScriptProperties.getProperty(‘token’);const idList = ‘633703c47e355102f3436482’function addCard(){let url = `https://api.trello.com/1/cards?idList=${idList}&key=${key}&token=${token}`;let options = { method: ‘post’, payload:{ name:’Card Added from Apps Script’, desc: ‘Description from Card added in Apps Script’, } } let response = UrlFetchApp.fetch(url,options); console.log(response.getResponseCode()) console.log(response.getContentText())}
Make the Request
On the top of the toolbar you are going to select ▷Run and once you do this a bunch of permission screens are going to pop-up. You will:
- Select your Gmail
- A screen will say Google hasn’t verified the app (which is correct, we did not submit it for Google Approval)
- Click Advanced
- Click Go to Trello Integration — Script (unsafe)
- It will tell you what the script has the ability to do Connect to an external service
- Click Allow
Now you should be able to check your Trello Board and see a Card with the name Card Added from Apps Script and a Description of Description from Card added in Apps Script.
If you enjoyed this tutorial, make sure to bookmark my GitHub Repo TrelloGAS for more ways to integrate Google Apps with Trello!
If you want to build automations with Google Apps feel free to reach out to me at huskdoes@gmail.com!