Skip to main content

Example Webhooks

This page contains example code for implementing webhooks in your application using Harlyy.

When an event occurs that triggers a webhook, our system sends an HTTP POST request to your configured endpoint. The request body contains an event object, and the request headers include X-Harlyy-Signature for verification using your webhook secret.

warning

Always verify the signature of incoming webhook requests to ensure they are from Harlyy and not forged by a third party. Use a secure method to store your webhook secret and never expose it in client-side code.

We always sign webhook requests using HMAC with SHA-256, and the signature is included in the X-Harlyy-Signature header. The signature is a base64-encoded HMAC of the request body using your webhook secret.

NodeJS Example
const express = require('express');
const crypto = require('crypto');
const app = express();

// Middleware to store raw body for HMAC verification
app.use(express.json({
verify: (req, res, buf) => {
req.rawBody = buf;
}
}));

// Your shared webhook secret (store securely in real applications)
const WEBHOOK_SECRET = 'xxxxx';

function verifySignature(req) {
const signatureHeader = req.headers['x-harlyy-signature'];

if (!signatureHeader || !signatureHeader.startsWith('sha256=')) {
return false;
}

const receivedSignature = signatureHeader.slice(7); // Remove 'sha256='

const hmac = crypto.createHmac('sha256', WEBHOOK_SECRET);
hmac.update(req.rawBody);
const expectedSignature = hmac.digest('base64');

// Constant-time comparison to prevent timing attacks
return crypto.timingSafeEqual(
Buffer.from(expectedSignature, 'utf8'),
Buffer.from(receivedSignature, 'utf8')
);
}

app.post('/webhook', (req, res) => {
if (!verifySignature(req)) {
console.log('❌ Invalid signature');
return res.status(401).send('Invalid signature');
}

console.log('✅ Valid webhook received:', req.body);
res.status(200).send('Webhook accepted');
});

const PORT = 3000;
app.listen(PORT, () => {
console.log(`🚀 Webhook listener running on http://localhost:${PORT}`);
});