Verify a delivery
Check the DocuDetect-Signature header before you trust a webhook body.
Early access
The REST API, webhooks and embedded signing are open to accounts in early access. If Settings, Developers is not in your account yet, write to contact@docudetect.com. What these pages describe can still change; breaking changes are announced on this site first.
Every delivery carries a DocuDetect-Signature header:
DocuDetect-Signature: t=1765033445,v1=5f2b1c...e9tis the time of this attempt, in Unix seconds.v1is the hex HMAC-SHA256 of<t>.<raw body>with your endpoint's signing secret as the key. For 24 hours after a rotation there are twov1values, one per secret; accept the delivery when either matches.
To verify:
- Read the body as raw text, before any JSON parsing. Re-serialized JSON won't match.
- Compute the HMAC of
t + "." + bodyand compare it with eachv1in constant time. - Refuse a
tmore than 300 seconds from your clock, so an old delivery can't be replayed.
Node.js and Bun
This is the function the app shows under Settings, Developers. Our delivery tests run it against a real signed delivery.
import { createHmac, timingSafeEqual } from "node:crypto";
// rawBody: the request body exactly as received (a string), before any JSON parsing.
// header: the DocuDetect-Signature header. secret: the webhook's signing secret (whsec_...).
export function verifyDocuDetectWebhook(rawBody, header, secret, toleranceSeconds = 300, nowSeconds = Math.floor(Date.now() / 1000)) {
let timestamp;
const signatures = [];
for (const part of String(header).split(",")) {
const [key, value] = part.trim().split("=");
if (key === "t") timestamp = Number(value);
if (key === "v1" && value) signatures.push(value);
}
if (!Number.isInteger(timestamp) || signatures.length === 0) return false;
if (Math.abs(nowSeconds - timestamp) > toleranceSeconds) return false;
const expected = createHmac("sha256", secret).update(timestamp + "." + rawBody).digest();
return signatures.some((signature) => {
const given = Buffer.from(signature, "hex");
return given.length === expected.length && timingSafeEqual(given, expected);
});
}Used in a handler:
import express from "express";
import { verifyDocuDetectWebhook } from "./verify-docudetect-webhook.mjs";
const app = express();
app.post("/webhooks/docudetect", express.text({ type: "application/json" }), (req, res) => {
if (!verifyDocuDetectWebhook(req.body, req.get("DocuDetect-Signature"), process.env.DOCUDETECT_WEBHOOK_SECRET)) {
return res.sendStatus(400);
}
const event = JSON.parse(req.body);
res.sendStatus(204);
queueEvent(event);
});Other languages
The check is the same anywhere: HMAC-SHA256 over the exact bytes <t>.<body>, hex-encoded, compared in constant time, plus the time window. Use your platform's constant-time comparison (hmac.compare_digest in Python, hash_equals in PHP, hmac.Equal in Go).