Receive and verify a webhook
An Express receiver that checks the signature on the raw body, answers fast and ignores repeats.
import express from "express";
import { constructEvent, WebhookSignatureError } from "@hevia/sdk";
const app = express();
const seen = new Set<string>(); // use your database in production
// express.raw keeps the body as bytes: the signature is over the raw body.
app.post("/hevia/webhook", express.raw({ type: "application/json" }), (req, res) => {
let event;
try {
event = constructEvent(req.body, req.headers["hevia-signature"], process.env.HEVIA_WEBHOOK_SECRET!);
} catch (err) {
if (err instanceof WebhookSignatureError) return res.status(400).end();
throw err;
}
res.status(200).end(); // answer first, work afterwards
if (!event.livemode || seen.has(event.id)) return;
seen.add(event.id);
if (event.type === "deal.won") console.log("won:", event.data.title, event.data.valueCents);
});
app.listen(3000);Check list
- Raw body, constant-time comparison, timestamps older than 5 minutes refused.
- Answer
2xxfast; Hevia makes 6 attempts per delivery. - Ignore an
idyou already handled. - Ignore
livemode: falsein production.
Tip
Test it with a sandbox: create a deal there and move it to a won stage.