iTranslated by AI
[GAS] Fetching Tasks Using the Asana API
Introduction
At my current company, we use Asana as a task management tool.
Since my team previously used Trello, the user experience feels like a more advanced version of that.
Though I suppose there are pros and cons depending on how much you value ease of extension.
However, with the existing UI, I feel it is slightly difficult to reference only "tasks I am involved in" or "tasks within the project I belong to."
Therefore, in this article, I would like to introduce an example of how to write code in GAS (Google Apps Script) using the Asana API to retrieve "tasks within a specific project."
What is Asana?
- Reference: Official Asana Website
Quote below:
Asanaorganizes all of your work, from small tasks to the big picture. It helps teams stay clear on what work needs to be done, why it matters, and how to get it finished.
It is a service that allows you to manage task progress in various formats, such as "Kanban boards," "Lists," and "Gantt charts."
Competitors include Trello and Redmine, as well as Jooto, which has been running many ads on YouTube lately.
What is the Asana API?
- Reference: Asana API
This is a REST API provided by Asana.
The official documentation is very helpful, even including introductions on how to integrate with popular services, such as Slack and Asana.
Preparation for Using the Asana API
Next, let's prepare to use the Asana API.
Specifically, we will obtain an Access Token.
Access the Developer Console
First, access the developer console with your Asana account.
The URL is as follows:
- Reference: Asana Developer Console
From the [Personal Access Tokens] section in the menu, select [Create New Token] (see the figure below).
*The parts hidden in red are tokens that have already been created, so they will not be displayed the first time.
You will be asked for a token name, so enter any value you like.
Check the box for [I agree to the API Terms of Service] and click [Create Token].
If the token is successfully created, it will be displayed as shown below, so copy it.
This completes the preliminary preparation.
Executing Asana API from GAS
Next, we will implement this in GAS.
The endpoint is https://app.asana.com/api/1.0/projects/[PROJECT_ID]/tasks.
- Reference: Asana | Get tasks from a project
This "Project ID" is a roughly 15-digit number included in the URL when you display an Asana project in your browser.
The tasks retrieved here are just a list, so to reference more detailed information, you need to use a different endpoint.
Alternatively, you can specify the items you want to retrieve by adding opt_fields as a parameter.
In this case, we will specify name,due_on,modified_at,completed to retrieve the "task name, due date, last modified date, and completion status."
Don't forget to add the "Access Token" you just obtained to the Authorization header of the request.
/**
* Retrieve tasks from Asana
*/
function getAsanaTasks() {
// Create request options
var options = {
// GET method
'method': 'get',
// JSON format
'contentType': 'application/json',
// Headers
'headers': {
// Authentication information
'Authorization': 'Bearer ' + [ACCESS_TOKEN]
},
}
// Execute Asana API and retrieve tasks
// Specify task name, due date, last modified date, and completion status using ?opt_fields=
var response = UrlFetchApp.fetch(`https://app.asana.com/api/1.0/projects/[PROJECT_ID]/tasks?opt_fields=name,due_on,modified_at,completed`, options);
// Parse the retrieval result as JSON
var result = JSON.parse(response);
// Store the results in an array
var list = [];
if(result.data){
for(var i = 0; i < result.data.length; i++){
list.push(result.data[i]);
}
}
console.log(list);
return list;
}
The retrieved data (list) will be displayed in the console.
The content looks like the following:
[
{
git: "1234567890",
completed: false,
due_on: "2020-11-30",
modified_at: "2020-11-30T00:00:00.000Z",
name: "Task ①"
},
// ...
]
Summary
In this article, I introduced GAS code for retrieving task data from the task management tool Asana using the Asana API.
In practice, I process this data further to make it usable within my team.
Since it hasn't been long since we introduced Asana, I can't say we are using it to its full potential yet. However, so far, the usability is good, and I think it has a full set of features for managing tasks within a team.
I didn't cover the detailed features of Asana this time, so I would like to do that eventually.
Discussion