Hey there, fellow Javascript devs! Ready to supercharge your Woodpecker.co integration? Let's dive into the world of webhooks and see how we can leverage them to create some seriously cool user-facing integrations.
Webhooks are like the cool kids of API integrations. Instead of constantly polling for updates, they notify you when something interesting happens. In Woodpecker.co, this means real-time updates about your campaigns, prospects, and more. Pretty neat, right?
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 receiver up and running.
Now, let's tell Woodpecker.co where to send those webhooks. You could do this through their UI, but we're developers, so let's use the API:
const axios = require('axios'); axios.post('https://api.woodpecker.co/rest/v1/webhooks', { url: 'https://your-server.com/webhook', events: ['prospect.status_changed', 'campaign.finished'] }, { headers: { 'X-API-KEY': 'your-api-key-here' } }) .then(response => console.log('Webhook created:', response.data)) .catch(error => console.error('Error:', error));
Woodpecker.co sends some pretty informative payloads. Let's parse and process them:
app.post('/webhook', (req, res) => { const { event, data } = req.body; switch(event) { case 'prospect.status_changed': handleProspectStatusChange(data); break; case 'campaign.finished': handleCampaignFinished(data); break; // Add more cases as needed } res.sendStatus(200); }); function handleProspectStatusChange(data) { console.log(`Prospect ${data.prospect_id} status changed to ${data.new_status}`); // Do something cool here } function handleCampaignFinished(data) { console.log(`Campaign ${data.campaign_id} finished`); // Maybe send a notification? }
Security is sexy, so let's verify those webhook signatures:
const crypto = require('crypto'); function verifySignature(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-woodpecker-signature']; if (!verifySignature(JSON.stringify(req.body), signature, 'your-webhook-secret')) { return res.status(401).send('Invalid signature'); } // Process the webhook... });
Sometimes webhooks fail. No biggie, let's implement a simple retry mechanism:
function processWebhook(payload, retries = 3) { try { // Process the webhook payload } catch (error) { if (retries > 0) { console.log(`Retrying... ${retries} attempts left`); setTimeout(() => processWebhook(payload, retries - 1), 5000); } else { console.error('Failed to process webhook after multiple attempts'); } } }
Woodpecker.co has a nifty webhook testing feature. Let's create a test endpoint:
app.post('/test-webhook', (req, res) => { console.log('Test webhook received:', req.body); res.json({ message: 'Test webhook received successfully' }); });
Now that you've got the basics down, the sky's the limit! You could:
And there you have it! You're now a Woodpecker.co webhook wizard. Remember, with great power comes great responsibility (and some really cool integrations).
Want to see all this in action? Check out our GitHub repo for a complete working example.
Now go forth and webhook all the things! 🚀