Hey there, fellow developer! Ready to dive into the world of SimplyBook.me API integration? You're in for a treat. This nifty API allows you to tap into a powerful booking system, and we're going to walk through how to make it dance with Python. Let's get cracking!
Before we jump in, make sure you've got:
requests
library installed (pip install requests
)First things first, let's get you authenticated:
import requests import json def get_token(company_login, api_key): url = f"https://{company_login}.simplybook.me/admin/auth" payload = { "company": company_login, "login": "api", "password": api_key } response = requests.post(url, json=payload) return response.json()['token'] token = get_token('your_company_login', 'your_api_key')
Pro tip: This token expires, so consider implementing a refresh mechanism.
Now that we're in, let's make some noise:
def make_request(endpoint, method='GET', data=None): url = f"https://api.simplybook.me/v2/{endpoint}" headers = { "X-Company-Login": "your_company_login", "X-Token": token } if method == 'GET': response = requests.get(url, headers=headers) elif method == 'POST': response = requests.post(url, headers=headers, json=data) if response.status_code != 200: raise Exception(f"API request failed: {response.text}") return response.json()
Let's explore some crucial endpoints:
# List services services = make_request('services') # Create a booking booking_data = { "client_id": 123, "service_id": 456, "start_datetime": "2023-06-01 10:00:00" } new_booking = make_request('bookings', method='POST', data=booking_data) # Get client info client_info = make_request(f'clients/123')
Time to build some meat on these bones:
def get_available_slots(service_id, date): return make_request(f'schedule/available_slots/{service_id}/{date}') def create_booking(client_id, service_id, start_datetime): booking_data = { "client_id": client_id, "service_id": service_id, "start_datetime": start_datetime } return make_request('bookings', method='POST', data=booking_data) def update_client(client_id, data): return make_request(f'clients/{client_id}', method='POST', data=data)
Here's a simple script to tie it all together:
# Authenticate token = get_token('your_company_login', 'your_api_key') # Get available slots for a service service_id = 123 date = "2023-06-01" slots = get_available_slots(service_id, date) # Create a booking if slots: booking = create_booking(456, service_id, slots[0]['start_datetime']) print(f"Booking created: {booking}") # Update client info update_client(456, {"name": "John Doe", "email": "[email protected]"})
Unit testing is your friend. Here's a quick example:
import unittest class TestSimplyBookAPI(unittest.TestCase): def test_get_available_slots(self): slots = get_available_slots(123, "2023-06-01") self.assertIsInstance(slots, list) if slots: self.assertIn('start_datetime', slots[0]) if __name__ == '__main__': unittest.main()
If you're hitting walls, double-check your API credentials and endpoint URLs. The SimplyBook.me API is pretty friendly, but it's not a mind reader!
And there you have it! You're now armed and dangerous with SimplyBook.me API integration skills. Remember, this is just the tip of the iceberg. There's a whole world of functionality to explore, so don't be shy - dive in and see what you can create!
Happy coding, and may your bookings always be on time! 🚀