Hey there, fellow JavaScript dev! Ready to supercharge your TeamUp integration with webhooks? Let's dive right in and get those real-time updates flowing.
Webhooks are like your app's personal news feed from TeamUp. Instead of constantly polling for changes, TeamUp will ping your app whenever something interesting happens. Cool, right?
Before we jump in, make sure you've got:
First things first, let's create a simple Express server to receive those juicy webhook payloads:
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'));
Boom! You've got a basic webhook endpoint up and running.
Now, let's tell TeamUp where to send those webhooks. Grab your API credentials and let's make it happen:
const axios = require('axios'); const registerWebhook = async () => { try { const response = await axios.post('https://api.teamup.com/webhooks', { url: 'https://your-server.com/webhook', events: ['calendar.created', 'event.created', 'event.updated'] }, { headers: { 'Teamup-Token': 'YOUR_API_KEY' } }); console.log('Webhook registered:', response.data); } catch (error) { console.error('Error registering webhook:', error); } }; registerWebhook();
When those webhooks start rolling in, you'll want to verify they're legit:
const crypto = require('crypto'); app.post('/webhook', (req, res) => { const signature = req.headers['teamup-signature']; const payload = JSON.stringify(req.body); const hmac = crypto.createHmac('sha256', 'YOUR_WEBHOOK_SECRET'); const digest = hmac.update(payload).digest('hex'); if (signature === digest) { // It's authentic! Process the webhook processWebhook(req.body); res.sendStatus(200); } else { res.sendStatus(401); } });
Now for the fun part - doing something with those events:
const processWebhook = (payload) => { switch (payload.event) { case 'calendar.created': // Handle new calendar break; case 'event.created': // Handle new event break; case 'event.updated': // Handle updated event break; default: console.log('Unhandled event type:', payload.event); } };
TeamUp's pretty persistent, but sometimes things go wrong. Let's be good citizens and handle retries:
app.post('/webhook', (req, res) => { try { // Process webhook res.sendStatus(200); } catch (error) { console.error('Error processing webhook:', error); res.status(500).send('Please retry later'); } });
Want to test locally? ngrok is your new best friend:
ngrok http 3000
Then update your webhook URL in the TeamUp dashboard with the ngrok URL. Easy peasy!
A few quick tips to keep your webhook game strong:
And there you have it! You're now a TeamUp webhook wizard. Remember, webhooks are powerful but they require care and feeding. Keep an eye on your logs, handle errors gracefully, and your integration will be smooth sailing.
Happy coding, and may your events always be in sync! 🚀