Back

Step by Step Guide to Building an Acuity Scheduling API Integration in JS

Aug 11, 20245 minute read

Hey there, fellow developer! Ready to dive into the world of Acuity Scheduling API integration? Let's get cracking!

Introduction

Acuity Scheduling's API is a powerhouse for managing appointments programmatically. Whether you're looking to automate bookings, sync calendars, or create a custom scheduling flow, this guide will set you on the right path.

Prerequisites

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

  • An Acuity Scheduling account (duh!)
  • API credentials (we'll cover this)
  • Node.js installed on your machine

Setting Up the Project

First things first, let's get our project off the ground:

mkdir acuity-integration cd acuity-integration npm init -y npm install axios dotenv

Authentication

Alright, time to get those API keys:

  1. Log into your Acuity account
  2. Head to API in the left sidebar
  3. Grab your User ID and API Key

Now, create a .env file:

ACUITY_USER_ID=your_user_id
ACUITY_API_KEY=your_api_key

Making API Requests

Let's set up our basic request structure:

require('dotenv').config(); const axios = require('axios'); const acuityClient = axios.create({ baseURL: 'https://acuityscheduling.com/api/v1/', auth: { username: process.env.ACUITY_USER_ID, password: process.env.ACUITY_API_KEY } });

Core API Functionalities

Retrieving Available Appointment Types

async function getAppointmentTypes() { try { const response = await acuityClient.get('appointment-types'); return response.data; } catch (error) { console.error('Error fetching appointment types:', error); } }

Fetching Calendar Availability

async function getAvailability(date) { try { const response = await acuityClient.get('availability/times', { params: { date, appointmentTypeID: 1 } // Replace 1 with your actual appointment type ID }); return response.data; } catch (error) { console.error('Error fetching availability:', error); } }

Booking Appointments

async function bookAppointment(appointmentData) { try { const response = await acuityClient.post('appointments', appointmentData); return response.data; } catch (error) { console.error('Error booking appointment:', error); } }

Implementing Webhook Support

Want to stay in the loop? Set up a webhook:

const express = require('express'); const app = express(); app.post('/webhook', express.json(), (req, res) => { const event = req.body; console.log('Received webhook event:', event); // Handle the event res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook server running on port 3000'));

Error Handling and Best Practices

Remember to:

  • Implement proper error handling
  • Respect rate limits (max 100 requests per minute)
  • Use async/await for cleaner code

Testing the Integration

Don't forget to test! Here's a quick example using Jest:

test('fetches appointment types', async () => { const types = await getAppointmentTypes(); expect(types).toBeDefined(); expect(Array.isArray(types)).toBe(true); });

Conclusion

And there you have it! You're now equipped to integrate Acuity Scheduling into your JavaScript projects. Remember, this is just scratching the surface - there's a whole world of possibilities with the Acuity API. Happy coding, and may your calendars always be in sync!

For more details, check out the official Acuity API docs. Now go forth and schedule like a pro!