import express from 'express';
import { Webhook } from 'svix';
const secret = process.env.SENDPILOT_WEBHOOK_SECRET;
if (!secret) throw new Error('Set SENDPILOT_WEBHOOK_SECRET');
const verifier = new Webhook(secret);
const app = express();
// Register before express.json(): verification needs the original bytes.
app.post('/webhooks/sendpilot', express.raw({ type: 'application/json' }), (req, res) => {
let event;
try {
event = verifier.verify(req.body.toString('utf8'), req.headers);
} catch {
return res.status(400).send('Invalid webhook');
}
console.info('Verified SendPilot event', event.eventId, event.eventType);
return res.sendStatus(204);
});
app.listen(3000);