Build a webhook endpoint
Write the code that properly handles webhook notifications.
Return 2xx status code quickly
Test that your endpoint works
app.post("/webhook", (req, res) => {
const header = req.header("x-pf-signature");
const body = req.body;
const secret = process.env.PF_WEBHOOK_SECRET
const details = header.split(",").reduce(
(obj, item) => {
const kv = item.split("=");
if (kv[0] === "t") {
obj.timestamp = kv[1];
}
if (kv[0] === "s") {
obj.signature = kv[1];
}
return obj;
}, { timestamp: -1, signature: "" }
);
if (!details || details.timestamp === -1) {
throw new Error('Unable to extract timestamp and signature from header')
}
const payload = `${details.timestamp}.${JSON.stringify(body)}`
const expectedSignature = crypto
.createHmac('sha256', secret)
.update(payload, 'utf-8')
.digest('hex')
if (details.signature !== expectedSignature) {
throw new Exception('Signature mismatch')
}
});Last updated