Back

Reading and Writing Data Using the Acuity Scheduling API

Aug 11, 20246 minute read

Hey there, fellow JavaScript aficionados! Ready to dive into the world of Acuity Scheduling API? Let's get our hands dirty with some code and build a slick user-facing integration. Buckle up!

The Lowdown on Acuity Scheduling API

Acuity Scheduling's API is your ticket to programmatically managing appointments, clients, and time slots. We're talking seamless integration that'll make your users wonder how they ever lived without it.

Authentication: Your All-Access Pass

First things first, let's get you those API credentials. Head over to your Acuity account, navigate to the API settings, and grab your User ID and API Key. Got 'em? Great!

Now, let's set up authentication in JavaScript:

const axios = require('axios'); const base64 = require('base-64'); const userId = 'YOUR_USER_ID'; const apiKey = 'YOUR_API_KEY'; const api = axios.create({ baseURL: 'https://acuityscheduling.com/api/v1/', headers: { 'Authorization': 'Basic ' + base64.encode(userId + ':' + apiKey) } });

Reading Data: Time to Get Nosy

Let's fetch some appointments, shall we? Here's a nifty async function to grab recent appointments:

async function getRecentAppointments() { try { const response = await api.get('appointments', { params: { max: 10, direction: 'DESC' } }); return response.data; } catch (error) { console.error('Error fetching appointments:', error); } }

Writing Data: Leave Your Mark

Creating a new appointment is a breeze. Check this out:

async function createAppointment(appointmentType, datetime, firstName, lastName, email) { try { const response = await api.post('appointments', { appointmentTypeID: appointmentType, datetime, firstName, lastName, email }); return response.data; } catch (error) { console.error('Error creating appointment:', error); } }

Syncing Data: Stay in the Loop

Want to know when things change? Webhooks are your new best friend. Here's a quick Express.js endpoint to handle appointment updates:

const express = require('express'); const app = express(); app.post('/webhook', express.json(), (req, res) => { const { action, appointment } = req.body; switch(action) { case 'scheduled': console.log('New appointment scheduled:', appointment); break; case 'rescheduled': console.log('Appointment rescheduled:', appointment); break; case 'canceled': console.log('Appointment canceled:', appointment); break; } res.sendStatus(200); });

Error Handling and Rate Limiting: Play Nice

The API has limits, so let's be good citizens. Here's a simple retry mechanism:

async function apiCallWithRetry(apiCall, maxRetries = 3) { for (let i = 0; i < maxRetries; i++) { try { return await apiCall(); } catch (error) { if (error.response && error.response.status === 429) { const delay = Math.pow(2, i) * 1000; await new Promise(resolve => setTimeout(resolve, delay)); } else { throw error; } } } throw new Error('Max retries reached'); }

Best Practices: The Secret Sauce

  1. Cache data when you can. Your API and your users will thank you.
  2. Batch operations where possible to minimize API calls.
  3. Keep your local data in sync with webhooks to ensure consistency.

Wrapping Up

And there you have it! You're now armed and dangerous with the Acuity Scheduling API. Remember, with great power comes great responsibility – use it wisely, and your users will love you for it.

Need more details? The Acuity API documentation is your new favorite bedtime reading. Now go forth and schedule like a boss!