Hey there, fellow Javascript devs! Ready to supercharge your OnceHub integration with some webhook magic? Let's dive right in and get those real-time updates flowing!
Webhooks are like the cool kids of the API world - they don't wait around, they come to you with the latest gossip (aka data). In OnceHub, webhooks are your ticket to instant updates about bookings, cancellations, and more. We'll be using the OnceHub API to set these 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 falling stars, except the stars are data and they're very predictable.
const express = require('express'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.json()); app.post('/webhook', (req, res) => { console.log('Received webhook:', req.body); res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook receiver running on port 3000'));
Easy peasy, right? This little server is now ready and waiting for OnceHub to start throwing data at it.
Now, let's tell OnceHub where to send those juicy updates. We'll use the OnceHub API to set this up. First, you'll need to authenticate (I know, I know, but security is important).
Here's how you can create a webhook subscription:
const axios = require('axios'); const createWebhook = async () => { try { const response = await axios.post('https://api.oncehub.com/v2/webhooks', { name: 'My Awesome Webhook', url: 'https://your-server.com/webhook', events: ['booking.created', 'booking.canceled'] }, { headers: { 'Authorization': 'Bearer YOUR_API_KEY' } }); console.log('Webhook created:', response.data); } catch (error) { console.error('Error creating webhook:', error.response.data); } }; createWebhook();
Replace 'YOUR_API_KEY'
with your actual API key, and 'https://your-server.com/webhook'
with your webhook endpoint URL.
Now that we're all set up, let's handle those incoming events like a pro. OnceHub will send events for things like new bookings, cancellations, and more. Here's a simple way to parse and process these:
app.post('/webhook', (req, res) => { const { event, payload } = req.body; switch(event) { case 'booking.created': console.log('New booking:', payload.booking_id); // Do something with the new booking break; case 'booking.canceled': console.log('Booking canceled:', payload.booking_id); // Update your system for the cancellation break; default: console.log('Unhandled event:', event); } res.sendStatus(200); });
To keep your webhook implementation as robust as your coffee addiction:
Want to test your webhook without exposing your local setup to the world? Enter ngrok, your new best friend for local testing. It's like having a secret tunnel from the internet to your machine.
npm install -g ngrok
ngrok http 3000
Now you can simulate events from OnceHub and watch them hit your local server. It's like being a spy, but for APIs.
Running into problems? Don't sweat it, we've all been there. Here are some common issues and their fixes:
And there you have it! You're now a OnceHub webhook wizard. Remember, webhooks are all about real-time communication, so use this power wisely (and build some cool stuff while you're at it).
Want to see all of this in action? Check out our GitHub repo for a complete working example. Fork it, star it, make it your own!
Now go forth and webhook all the things! Happy coding! 🚀