Hey there, fellow developer! Ready to supercharge your app with appointment scheduling capabilities? Look no further than the Setmore Appointments API. In this guide, we'll walk through building a robust integration that'll have you managing appointments like a pro in no time.
Before we dive in, make sure you've got:
requests
library installed (pip install requests
)First things first, let's get you authenticated:
import requests API_KEY = 'your_api_key_here' headers = { 'Authorization': f'Bearer {API_KEY}', 'Content-Type': 'application/json' }
Pro tip: Keep that API key safe! Consider using environment variables in production.
Let's start with a simple GET request:
response = requests.get('https://api.setmore.com/v1/bookingapi/services', headers=headers) print(response.json())
Easy peasy, right? This will fetch all your services.
Now for the good stuff. Here's how to handle the bread and butter of appointment scheduling:
def get_available_slots(service_key, staff_key, date): endpoint = f'https://api.setmore.com/v1/bookingapi/slots?service_key={service_key}&staff_key={staff_key}&selected_date={date}' response = requests.get(endpoint, headers=headers) return response.json()
def create_appointment(appointment_data): endpoint = 'https://api.setmore.com/v1/bookingapi/appointment/create' response = requests.post(endpoint, json=appointment_data, headers=headers) return response.json()
You've got the idea now - updating and canceling appointments follow a similar pattern.
Don't let those pesky errors catch you off guard:
try: response = requests.get('https://api.setmore.com/v1/bookingapi/services', headers=headers) response.raise_for_status() except requests.exceptions.RequestException as e: print(f"Oops! Something went wrong: {e}")
JSON is your friend here. Parse those responses like a champ:
import json def parse_slots(response_data): slots = json.loads(response_data) return [slot['start_time'] for slot in slots['data']['slots']]
Want to level up? Try implementing pagination:
def get_all_appointments(limit=10, offset=0): all_appointments = [] while True: endpoint = f'https://api.setmore.com/v1/bookingapi/appointments?limit={limit}&offset={offset}' response = requests.get(endpoint, headers=headers) data = response.json() appointments = data['data']['appointments'] if not appointments: break all_appointments.extend(appointments) offset += limit return all_appointments
Remember to play nice with the API:
Don't forget to test your integration:
import unittest from unittest.mock import patch class TestSetmoreIntegration(unittest.TestCase): @patch('requests.get') def test_get_available_slots(self, mock_get): mock_get.return_value.json.return_value = {'data': {'slots': []}} slots = get_available_slots('service_key', 'staff_key', '2023-06-01') self.assertEqual(slots, {'data': {'slots': []}})
When you're ready to go live:
And there you have it! You're now equipped to build a killer Setmore Appointments API integration. Remember, practice makes perfect, so don't be afraid to experiment and expand on what we've covered here. Happy coding!