Hey there, fellow JavaScript dev! Ready to supercharge your Keap 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 Keap. Instead of constantly asking "Any updates?", Keap will ping your app whenever something interesting happens. Cool, right?
Make sure you've got:
First things first, let's create a simple Express server to catch those incoming webhooks:
const express = require('express'); const crypto = require('crypto'); const app = express(); app.use(express.json()); app.post('/webhook', (req, res) => { const signature = req.headers['x-keap-signature']; // We'll verify this signature in a bit console.log('Webhook received:', req.body); res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook server running on port 3000'));
Don't forget to verify that signature! It's like checking ID at the door:
const isValidSignature = (payload, signature, secret) => { const hmac = crypto.createHmac('sha256', secret); const digest = hmac.update(JSON.stringify(payload)).digest('hex'); return signature === digest; };
Time to tell Keap where to send those juicy updates. We'll use axios for this:
const axios = require('axios'); const createWebhook = async () => { try { const response = await axios.post('https://api.infusionsoft.com/crm/rest/v1/hooks', { eventKey: 'contact.add', hookUrl: 'https://your-app.com/webhook', status: 'active' }, { headers: { 'Authorization': 'Bearer YOUR_ACCESS_TOKEN' } }); console.log('Webhook created:', response.data); } catch (error) { console.error('Error creating webhook:', error.response.data); } }; createWebhook();
When a webhook hits your endpoint, it's party time:
app.post('/webhook', (req, res) => { const { event_type, data } = req.body; switch(event_type) { case 'contact.add': console.log('New contact added:', data); // Do something awesome with the new contact break; // Handle other event types default: console.log('Unhandled event type:', event_type); } res.sendStatus(200); });
Webhooks can fail. It's a fact of life. But we're prepared:
const MAX_RETRIES = 3; const RETRY_DELAY = 5000; // 5 seconds const processWebhook = async (data, attempt = 1) => { try { // Process the webhook data } catch (error) { if (attempt <= MAX_RETRIES) { console.log(`Retry attempt ${attempt} in ${RETRY_DELAY}ms`); setTimeout(() => processWebhook(data, attempt + 1), RETRY_DELAY); } else { console.error('Max retries reached. Webhook processing failed.'); } } };
Keap's got your back with some nifty testing tools. But you can also whip up a quick local test:
const testWebhook = { event_type: 'contact.add', data: { id: 123, email: '[email protected]' } }; axios.post('http://localhost:3000/webhook', testWebhook) .then(response => console.log('Test webhook sent')) .catch(error => console.error('Error sending test webhook:', error));
Remember:
As your app grows, you might need to level up:
And there you have it! You're now armed and ready to implement webhooks in your Keap integration. Remember, the key is to start simple and iterate. Before you know it, you'll be handling webhooks like a pro.
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! 🚀