Using Asana API in Laravel/PHP

Recently I've started a new client project which uses API of a project management tool called Asana. So I decided to write up a short demo for anyone who wants to use it, it's really simple. With that, this could be a good example of a service having their own official PHP package, and how to use it. First, here's how Asana looks like. On a basic level, it's just a list of tasks. asana list So my job here was to take the task list via Asana API into Laravel. So let's do exactly that.

Step 1. Create Asana Token

To do that, go to Asana's My Profile Settings and make a few clicks to generate token, here are a few screenshots: Screen Shot 2018-06-12 at 5.03.48 PM Screen Shot 2018-06-12 at 5.03.35 PM Screen Shot 2018-06-12 at 5.05.07 PM

Step 2. Use Asana's Package

Asana have their own official PHP package: Asana/php-asana. Notice that it's not Laravel specific, it's for any PHP called, in other words it's framework-agnostic. So we install it into our Laravel app:
composer install asana/asana
And then inside of our Controller (or, I would recommend to create a separate AsanaService) we can do this:
use Asana\Client;

// ...

// Let's use the token from the Step 1
$asana_client = Client::accessToken(env('ASANA_PERSONAL_ACCESS_TOKEN'));

// Get the list of Asana's projects
$projects = $asana_client->get('/projects', []);

// Get the list of tasks of a certain project
$tasks = $asana_client->get('/projects/' . $projects[0] . '/tasks', []);
That's it. A simple example of using existing official package. You can also check the official documentation on other API methods, or how to use OAuth instead of the personal access token.

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month

Written by