Complete guide to implementing webhooks for real-time event notifications
Receive real-time HTTP notifications when events occur in your CognexiaAI account
// Express.js example
app.post('/webhook', express.json(), (req, res) => {
const event = req.body;
console.log('Event received:', event.type);
console.log('Event data:', event.data);
// Process the event
processEvent(event);
// Respond quickly (within 5 seconds)
res.status(200).send('OK');
});curl -X POST https://api.cognexiaai.com/v1/webhooks \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"url": "https://yourapp.com/webhook",
"events": ["contact.created", "deal.closed"],
"description": "Production webhook"
}'const crypto = require('crypto');
function verifyWebhookSignature(payload, signature, secret) {
const hmac = crypto
.createHmac('sha256', secret)
.update(JSON.stringify(payload))
.digest('hex');
return crypto.timingSafeEqual(
Buffer.from(signature),
Buffer.from(hmac)
);
}CRM Events
contact.created, contact.updated, deal.created, deal.closedFinance Events
invoice.created, invoice.paid, payment.receivedHR Events
employee.created, attendance.logged, payroll.processedconst express = require('express');
const crypto = require('crypto');
const app = express();
// Webhook endpoint
app.post('/webhook',
express.raw({ type: 'application/json' }),
async (req, res) => {
const signature = req.headers['x-cognexia-signature'];
const secret = process.env.WEBHOOK_SECRET;
// Verify signature
const isValid = verifySignature(
req.body,
signature,
secret
);
if (!isValid) {
return res.status(401).send('Invalid signature');
}
const event = JSON.parse(req.body);
// Process event asynchronously
processEventAsync(event).catch(console.error);
// Respond immediately
res.status(200).send('OK');
}
);
async function processEventAsync(event) {
switch (event.type) {
case 'contact.created':
await handleContactCreated(event.data);
break;
case 'deal.closed':
await handleDealClosed(event.data);
break;
default:
console.log('Unhandled event:', event.type);
}
}
app.listen(3000);Respond Quickly
Return 200 within 5 seconds, process asynchronously
Idempotency
Use event ID to prevent duplicate processing
Retry Logic
We retry up to 3 times with exponential backoff
# Expose local server with ngrok
ngrok http 3000
# Register webhook with ngrok URL
curl -X POST https://api.cognexiaai.com/v1/webhooks \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"url": "https://abc123.ngrok.io/webhook"}'