Hey there, fellow code wrangler! Ready to dive into the world of Acuity Scheduling API integration? You're in for a treat. This guide will walk you through the process of building a robust integration using Python. Acuity's API is a powerhouse for managing appointments, and we're about to harness that power. Let's get cracking!
Before we jump in, make sure you've got:
Got all that? Great! Let's move on.
First things first, let's get our project set up:
pip install requests mkdir acuity_integration cd acuity_integration touch acuity_api.py
Alright, time to get cozy with Acuity's API. Grab your User ID and API Key from your Acuity account. We'll use these to authenticate our requests:
import requests USER_ID = 'your_user_id' API_KEY = 'your_api_key' headers = { 'Authorization': f'Basic {USER_ID}:{API_KEY}' }
Let's start with a basic GET request to fetch appointments:
BASE_URL = 'https://acuityscheduling.com/api/v1' def get_appointments(): response = requests.get(f'{BASE_URL}/appointments', headers=headers) if response.status_code == 200: return response.json() else: response.raise_for_status()
Now, let's build out some core functions:
def create_appointment(appointment_data): response = requests.post(f'{BASE_URL}/appointments', headers=headers, json=appointment_data) return response.json() def update_appointment(appointment_id, update_data): response = requests.put(f'{BASE_URL}/appointments/{appointment_id}', headers=headers, json=update_data) return response.json() def cancel_appointment(appointment_id): response = requests.put(f'{BASE_URL}/appointments/{appointment_id}/cancel', headers=headers) return response.json()
Want to level up? Let's add some calendar and client management:
def get_calendars(): response = requests.get(f'{BASE_URL}/calendars', headers=headers) return response.json() def create_client(client_data): response = requests.post(f'{BASE_URL}/clients', headers=headers, json=client_data) return response.json()
Always be prepared for the unexpected:
import time def rate_limited_request(func): def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except requests.exceptions.HTTPError as e: if e.response.status_code == 429: time.sleep(60) # Wait for a minute before retrying return func(*args, **kwargs) else: raise return wrapper @rate_limited_request def get_appointments(): # ... (previous implementation)
Don't forget to test your code! Here's a quick example using unittest
:
import unittest class TestAcuityIntegration(unittest.TestCase): def test_get_appointments(self): appointments = get_appointments() self.assertIsInstance(appointments, list) if __name__ == '__main__': unittest.main()
And there you have it! You've just built a solid foundation for your Acuity Scheduling API integration. Remember, this is just the beginning – there's a whole world of possibilities to explore with this API. Keep experimenting, keep building, and most importantly, keep having fun with it!
For more in-depth information, check out the Acuity Scheduling API documentation. Happy coding!