Back

Step by Step Guide to Building a Todoist API Integration in PHP

Aug 11, 20246 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your productivity with Todoist? Let's dive into building a slick PHP integration using the Todoist API. We'll cover everything you need to know to get up and running quickly.

Prerequisites

Before we jump in, make sure you've got:

  • A PHP environment (7.4+ recommended)
  • Composer installed
  • Your Todoist API token handy

Got all that? Great! Let's get coding.

Setting up the project

First things first, let's set up our project:

mkdir todoist-integration cd todoist-integration composer init composer require guzzlehttp/guzzle

Authentication

Alright, time to authenticate. Grab your API token from Todoist settings and let's set up our client:

<?php require 'vendor/autoload.php'; use GuzzleHttp\Client; $apiToken = 'YOUR_API_TOKEN_HERE'; $client = new Client([ 'base_uri' => 'https://api.todoist.com/rest/v2/', 'headers' => [ 'Authorization' => 'Bearer ' . $apiToken ] ]);

Basic API Operations

Now for the fun part - let's interact with Todoist!

Fetching tasks

$response = $client->get('tasks'); $tasks = json_decode($response->getBody(), true);

Creating a new task

$response = $client->post('tasks', [ 'json' => [ 'content' => 'Buy milk', 'due_string' => 'tomorrow at 12:00' ] ]);

Updating a task

$taskId = '12345'; $response = $client->post("tasks/$taskId", [ 'json' => [ 'content' => 'Buy almond milk instead' ] ]);

Deleting a task

$taskId = '12345'; $client->delete("tasks/$taskId");

Advanced Features

Want to level up? Let's tackle some advanced features.

Working with projects

// Get all projects $response = $client->get('projects'); $projects = json_decode($response->getBody(), true); // Create a new project $response = $client->post('projects', [ 'json' => [ 'name' => 'My Awesome Project' ] ]);

Managing labels

// Get all labels $response = $client->get('labels'); $labels = json_decode($response->getBody(), true); // Create a new label $response = $client->post('labels', [ 'json' => [ 'name' => 'urgent' ] ]);

Error Handling and Rate Limiting

Don't forget to handle those pesky errors and respect rate limits:

try { $response = $client->get('tasks'); } catch (\GuzzleHttp\Exception\ClientException $e) { // Handle 4xx errors } catch (\GuzzleHttp\Exception\ServerException $e) { // Handle 5xx errors } // Respect rate limits sleep(1); // Simple but effective for small scripts

Testing the Integration

Always test your code! Here's a quick unit test example:

public function testCreateTask() { // ... setup code ... $response = $this->client->post('tasks', [ 'json' => [ 'content' => 'Test task' ] ]); $this->assertEquals(200, $response->getStatusCode()); // ... more assertions ... }

Optimization and Best Practices

To keep your integration running smoothly:

  • Cache responses when possible
  • Batch operations where you can
  • Use webhook notifications for real-time updates

Conclusion

And there you have it! You've just built a robust Todoist integration in PHP. Remember, this is just the beginning - there's so much more you can do with the Todoist API. Keep exploring, keep building, and most importantly, keep getting things done!

For more details, check out the Todoist API documentation.

Happy coding!

Code Repository

Find the full source code for this integration on GitHub.