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.
Before we jump in, make sure you've got:
Got all that? Great! Let's get coding.
First things first, let's set up our project:
mkdir todoist-integration cd todoist-integration composer init composer require guzzlehttp/guzzle
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 ] ]);
Now for the fun part - let's interact with Todoist!
$response = $client->get('tasks'); $tasks = json_decode($response->getBody(), true);
$response = $client->post('tasks', [ 'json' => [ 'content' => 'Buy milk', 'due_string' => 'tomorrow at 12:00' ] ]);
$taskId = '12345'; $response = $client->post("tasks/$taskId", [ 'json' => [ 'content' => 'Buy almond milk instead' ] ]);
$taskId = '12345'; $client->delete("tasks/$taskId");
Want to level up? Let's tackle some advanced features.
// 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' ] ]);
// Get all labels $response = $client->get('labels'); $labels = json_decode($response->getBody(), true); // Create a new label $response = $client->post('labels', [ 'json' => [ 'name' => 'urgent' ] ]);
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
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 ... }
To keep your integration running smoothly:
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!
Find the full source code for this integration on GitHub.