Hey there, fellow JavaScript dev! Ready to supercharge your Calendly integration with webhooks? Let's dive right in and get those real-time updates flowing.
Webhooks are like the cool kids of the API world - they notify your app instantly when something happens in Calendly. No more constant polling or refreshing. We'll be using Calendly's API to set this up, so buckle up!
Before we start, make sure you've got:
First things first, let's create a simple Express server to catch those webhook events. It's like setting up a net to catch some coding butterflies:
const express = require('express'); const app = express(); app.use(express.json()); app.post('/webhook', (req, res) => { console.log('Received webhook:', req.body); res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook server running on port 3000'));
Now, let's tell Calendly where to send those sweet, sweet notifications. Grab your API key and let's make it happen:
const axios = require('axios'); const createWebhook = async () => { try { const response = await axios.post('https://api.calendly.com/webhook_subscriptions', { url: 'https://your-server.com/webhook', events: ['invitee.created', 'invitee.canceled'] }, { headers: { 'Authorization': 'Bearer YOUR_API_KEY', 'Content-Type': 'application/json' } }); console.log('Webhook created:', response.data); } catch (error) { console.error('Error creating webhook:', error.response.data); } }; createWebhook();
Time to do something with those events! Let's parse them and react accordingly:
app.post('/webhook', (req, res) => { const { event, payload } = req.body; switch(event) { case 'invitee.created': console.log('New invitee:', payload.invitee.name); // Do something cool here break; case 'invitee.canceled': console.log('Invitee canceled:', payload.invitee.name); // Handle the cancellation break; default: console.log('Unhandled event:', event); } res.sendStatus(200); });
Don't forget to verify those webhooks! It's like checking IDs at a club, but for your API:
const crypto = require('crypto'); const verifyWebhookSignature = (req, res, next) => { const signature = req.headers['calendly-webhook-signature']; const body = JSON.stringify(req.body); const hash = crypto.createHmac('sha256', process.env.WEBHOOK_SECRET) .update(body) .digest('hex'); if (hash === signature) { next(); } else { res.status(401).send('Invalid signature'); } }; app.use('/webhook', verifyWebhookSignature);
Calendly's got your back with a webhook tester. Use it to simulate events and debug like a pro. It's like a sandbox, but for webhooks!
Running into trouble? Here are some quick fixes:
And there you have it! You're now a Calendly webhook wizard. Remember, the Calendly API docs are your new best friend for diving deeper.
Now go forth and build some awesome integrations! Your users will love the real-time goodness you're about to unleash. Happy coding!