Hey there, fellow Javascript devs! Ready to supercharge your CompanyCam integration with some webhook magic? Let's dive right in and get those real-time updates flowing!
Webhooks are like the cool kids of API integrations - they notify your app instantly when something happens in CompanyCam. No more constant polling or outdated data. Sweet, right?
Before we start, make sure you've got:
First things first, let's create an endpoint to catch those webhook events. Fire up your favorite code editor and let's get cracking:
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 server ready to receive webhooks. Easy peasy, right?
Now, let's tell CompanyCam where to send those juicy events. We'll use axios because, well, it's awesome:
const axios = require('axios'); axios.post('https://api.companycam.com/v2/webhooks', { url: 'https://your-server.com/webhook', events: ['photo.created', 'project.updated'] }, { headers: { 'Authorization': 'Bearer YOUR_API_TOKEN' } }) .then(response => console.log('Webhook registered:', response.data)) .catch(error => console.error('Oops:', error));
Just replace YOUR_API_TOKEN
with your actual token, and you're golden!
When those webhooks start rolling in, you'll want to do something cool with them. Here's a quick example:
app.post('/webhook', (req, res) => { const { event, data } = req.body; switch(event) { case 'photo.created': console.log('New photo:', data.id); break; case 'project.updated': console.log('Project updated:', data.name); break; default: console.log('Unknown event:', event); } res.sendStatus(200); });
Security is sexy, so let's add some! CompanyCam signs each webhook, and you should verify it:
const crypto = require('crypto'); function verifyWebhookSignature(payload, signature, secret) { const hmac = crypto.createHmac('sha256', secret); const digest = hmac.update(payload).digest('hex'); return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(digest)); } app.post('/webhook', (req, res) => { const signature = req.headers['x-companycam-signature']; const isValid = verifyWebhookSignature(JSON.stringify(req.body), signature, 'YOUR_WEBHOOK_SECRET'); if (!isValid) { return res.sendStatus(401); } // Process the webhook... });
Sometimes things go wrong. No biggie! Let's handle it like pros:
app.post('/webhook', async (req, res) => { try { // Process webhook... res.sendStatus(200); } catch (error) { console.error('Webhook processing failed:', error); res.sendStatus(500); } });
CompanyCam will retry failed webhooks, so you're covered!
Testing locally? No problem! Use ngrok to expose your local server:
ngrok http 3000
Then use the ngrok URL when registering your webhook. Instant local testing - you're welcome!
And there you have it! You're now a CompanyCam webhook wizard. Remember, with great power comes great responsibility (and awesome real-time updates).
Want to dive deeper? Check out the CompanyCam API docs for more webhook goodness.
Now go forth and webhook all the things! 🚀