Back

Step by Step Guide to Building a Setmore Appointments API Integration in Python

Aug 16, 20245 minute read

Introduction

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.

Prerequisites

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

  • A Python environment (3.6+ recommended)
  • The requests library installed (pip install requests)
  • Your Setmore API credentials (if you don't have them, grab them from your Setmore account)

Authentication

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.

Basic API Requests

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.

Core Functionalities

Now for the good stuff. Here's how to handle the bread and butter of appointment scheduling:

Retrieving available time slots

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()

Creating appointments

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.

Error Handling

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}")

Data Processing

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']]

Advanced Features

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

Best Practices

Remember to play nice with the API:

  • Implement rate limiting to avoid hitting API limits
  • Cache responses when possible to reduce API calls

Testing

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': []}})

Deployment Considerations

When you're ready to go live:

  • Use environment variables for API keys
  • Consider using a task queue for handling appointment creation in high-traffic scenarios

Conclusion

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!