Hey there, fellow JavaScript dev! Ready to supercharge your ActiveCampaign integration with webhooks? Let's dive right in and get those real-time updates flowing.
Webhooks are like the cool kids of the API world – they notify your app instantly when something interesting happens in ActiveCampaign. No more constant polling or waiting around. We're talking real-time, folks!
In this guide, we'll focus on setting up webhooks for user-facing integrations. Trust me, your users will love the snappy responsiveness.
Before we start coding, make sure you've got:
First things first, let's create a simple Express.js 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.
Time to cozy up to the ActiveCampaign API. Grab your API credentials from your account settings, and let's set up an API client:
const axios = require('axios'); const client = axios.create({ baseURL: 'https://your-account.api-us1.com/api/3', headers: { 'Api-Token': 'your_api_token_here' } });
Now for the fun part – creating a webhook! Here's how you do it:
async function createWebhook() { try { const response = await client.post('/webhooks', { webhook: { name: 'My Awesome Webhook', url: 'https://your-app.com/webhook', events: ['subscribe', 'unsubscribe'], sources: ['public', 'admin'] } }); console.log('Webhook created:', response.data); } catch (error) { console.error('Error creating webhook:', error.response.data); } } createWebhook();
Make sure to replace 'https://your-app.com/webhook'
with your actual endpoint URL.
Got webhooks? Let's manage 'em:
// List webhooks async function listWebhooks() { const response = await client.get('/webhooks'); console.log('Webhooks:', response.data); } // Update a webhook async function updateWebhook(webhookId, newData) { const response = await client.put(`/webhooks/${webhookId}`, { webhook: newData }); console.log('Updated webhook:', response.data); } // Delete a webhook async function deleteWebhook(webhookId) { await client.delete(`/webhooks/${webhookId}`); console.log('Webhook deleted'); }
When ActiveCampaign sends a webhook, it's showtime! Here's how to process that payload:
app.post('/webhook', (req, res) => { const { type, date, contact } = req.body; switch (type) { case 'subscribe': console.log(`New subscription on ${date} for contact ${contact.email}`); break; case 'unsubscribe': console.log(`Unsubscription on ${date} for contact ${contact.email}`); break; default: console.log('Unknown event type:', type); } res.sendStatus(200); });
Testing locally? ngrok is your best friend:
ngrok http 3000
This gives you a public URL to use in ActiveCampaign. Sweet!
Don't forget to check the webhook logs in your ActiveCampaign account for any hiccups.
And there you have it! You're now a webhook wizard. Your ActiveCampaign integration is about to get a whole lot snappier.
Remember, the ActiveCampaign API docs are your friend if you need more details. Now go forth and webhook all the things!
Happy coding! 🚀