Back

Step by Step Guide to Building an Any.do API Integration in Python

Aug 11, 20248 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your productivity with Any.do? Let's dive into building a Python integration that'll have you managing tasks like a pro. We'll be working with the Any.do API to create, read, update, and delete tasks programmatically. Exciting stuff, right?

Prerequisites

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

  • A Python environment (3.6+ recommended)
  • An Any.do account (duh!)
  • Your Any.do API key (we'll cover how to get this)

Setting up the project

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

mkdir anydo_integration cd anydo_integration python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate` pip install requests

Great! We've got our virtual environment ready and the requests library installed. We'll be using this to make API calls.

Authentication

Alright, time to get our hands on those API credentials:

  1. Log into your Any.do account
  2. Head to the API settings (you might need to poke around a bit)
  3. Generate a new API key

Now, let's use that key in our Python script:

import requests API_KEY = 'your_api_key_here' BASE_URL = 'https://sm-prod2.any.do/api/v2' headers = { 'Authorization': f'Bearer {API_KEY}', 'Content-Type': 'application/json' }

Basic API Operations

Let's start with the bread and butter of task management: CRUD operations.

GET: Retrieving tasks

def get_tasks(): response = requests.get(f'{BASE_URL}/me/tasks', headers=headers) return response.json() tasks = get_tasks() print(tasks)

POST: Creating new tasks

def create_task(title): payload = {'title': title} response = requests.post(f'{BASE_URL}/me/tasks', json=payload, headers=headers) return response.json() new_task = create_task("Learn Any.do API") print(new_task)

PUT: Updating existing tasks

def update_task(task_id, updates): response = requests.put(f'{BASE_URL}/me/tasks/{task_id}', json=updates, headers=headers) return response.json() updated_task = update_task('task_id_here', {'title': 'Master Any.do API'}) print(updated_task)

DELETE: Removing tasks

def delete_task(task_id): response = requests.delete(f'{BASE_URL}/me/tasks/{task_id}', headers=headers) return response.status_code == 204 success = delete_task('task_id_here') print(f"Task deleted: {success}")

Advanced Features

Now that we've got the basics down, let's spice things up a bit!

Working with task priorities

def set_priority(task_id, priority): updates = {'priority': priority} # 'HIGH', 'MEDIUM', or 'LOW' return update_task(task_id, updates)

Managing task categories

def assign_category(task_id, category_id): updates = {'categoryId': category_id} return update_task(task_id, updates)

Handling due dates and reminders

from datetime import datetime, timedelta def set_due_date(task_id, days_from_now): due_date = (datetime.now() + timedelta(days=days_from_now)).isoformat() updates = {'dueDate': due_date} return update_task(task_id, updates)

Error Handling and Best Practices

Let's wrap our API calls with some error handling to make our integration more robust:

def api_call(method, endpoint, **kwargs): try: response = requests.request(method, f'{BASE_URL}{endpoint}', headers=headers, **kwargs) response.raise_for_status() return response.json() if response.text else None except requests.exceptions.RequestException as e: print(f"API call failed: {e}") return None

Don't forget about rate limiting! Any.do might have restrictions on how many requests you can make in a given time period. Be a good API citizen and implement some rate limiting in your code.

Building a Simple CLI Tool

Let's put it all together in a simple command-line interface:

import argparse def main(): parser = argparse.ArgumentParser(description='Any.do Task Manager') parser.add_argument('action', choices=['list', 'add', 'update', 'delete']) parser.add_argument('--title', help='Task title') parser.add_argument('--id', help='Task ID') args = parser.parse_args() if args.action == 'list': tasks = get_tasks() for task in tasks: print(f"{task['id']}: {task['title']}") elif args.action == 'add': new_task = create_task(args.title) print(f"Created task: {new_task['title']}") elif args.action == 'update': updated_task = update_task(args.id, {'title': args.title}) print(f"Updated task: {updated_task['title']}") elif args.action == 'delete': success = delete_task(args.id) print(f"Task deleted: {success}") if __name__ == '__main__': main()

Testing the Integration

Don't forget to test your code! Here's a quick example using unittest:

import unittest class TestAnyDoIntegration(unittest.TestCase): def test_create_and_delete_task(self): task = create_task("Test task") self.assertIsNotNone(task['id']) success = delete_task(task['id']) self.assertTrue(success) if __name__ == '__main__': unittest.main()

Conclusion

And there you have it! You've just built a Python integration with the Any.do API. You can now manage your tasks programmatically, opening up a world of automation possibilities. Why not try extending this integration to sync with your calendar or create tasks based on your email inbox?

Remember, the key to mastering any API is practice and exploration. Don't be afraid to dive into the documentation and try out new endpoints and features.

Resources

Happy coding, and may your tasks always be organized!