Hey there, fellow Javascript dev! Ready to supercharge your Acuity Scheduling integration with some real-time goodness? Let's dive into the world of webhooks and get you set up in no time.
Webhooks are like the cool kids of the API world - they don't wait around, they come to you with updates. With Acuity Scheduling, webhooks are your ticket to building responsive, real-time integrations that'll make your users go "Wow!"
Before we jump in, make sure you've got:
Got all that? Great! Let's get our hands dirty.
First things first, we need somewhere for Acuity to send those sweet, sweet webhook events. Let's whip up a quick Express server:
const express = require('express'); const app = express(); app.use(express.json()); app.post('/webhook', (req, res) => { console.log('Webhook received:', req.body); res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook server running on port 3000'));
Easy peasy, right? This little server will catch all the webhook events and log them for now.
Now, let's tell Acuity where to send those events. We'll use axios to make this a breeze:
const axios = require('axios'); const acuityApiUrl = 'https://acuityscheduling.com/api/v1/webhooks'; const webhookUrl = 'https://your-server.com/webhook'; axios.post(acuityApiUrl, { target: webhookUrl, event: '*' }, { auth: { username: 'YOUR_USER_ID', password: 'YOUR_API_KEY' } }) .then(response => console.log('Webhook registered successfully')) .catch(error => console.error('Error registering webhook:', error));
Replace 'https://your-server.com/webhook'
with your actual webhook URL, and don't forget to use your real Acuity credentials!
Trust, but verify! Let's make sure these events are legit:
const crypto = require('crypto'); function verifyWebhook(req, secret) { const signature = req.headers['x-acuity-signature']; const hash = crypto.createHmac('sha256', secret) .update(JSON.stringify(req.body)) .digest('hex'); return signature === hash; } app.post('/webhook', (req, res) => { if (!verifyWebhook(req, 'YOUR_WEBHOOK_SECRET')) { return res.status(401).send('Unauthorized'); } // Process the webhook... });
Now, let's handle those events like a pro:
app.post('/webhook', (req, res) => { // Verification code here... switch(req.body.action) { case 'scheduled': handleNewAppointment(req.body); break; case 'rescheduled': handleRescheduledAppointment(req.body); break; case 'canceled': handleCanceledAppointment(req.body); break; default: console.log('Unhandled event type:', req.body.action); } res.sendStatus(200); });
Here are some quick examples of what you might do with these events:
function handleNewAppointment(data) { // Send a welcome email sendEmail(data.email, 'Welcome! Your appointment is confirmed.'); } function handleRescheduledAppointment(data) { // Update your database updateAppointmentInDB(data.id, data.time); } function handleCanceledAppointment(data) { // Free up the slot in your system removeAppointmentFromDB(data.id); }
When things go wrong (and they will), be ready:
function processWebhook(data, attempt = 1) { try { // Your processing logic here } catch (error) { if (attempt < 5) { const delay = Math.pow(2, attempt) * 1000; setTimeout(() => processWebhook(data, attempt + 1), delay); } else { console.error('Failed to process webhook after 5 attempts', error); } } }
This little gem will retry failed webhooks with exponential backoff. Nice!
Acuity's got your back with test events. Head to your Acuity dashboard, find the webhook section, and fire away!
For local testing, ngrok is your best friend. Just run:
ngrok http 3000
And use the generated URL as your webhook endpoint. Magic!
Remember:
And there you have it! You're now a webhook wizard, ready to create real-time, responsive integrations with Acuity Scheduling. Remember, the Acuity API docs are your friend if you need more details.
Now go forth and webhook all the things! 🚀