Verify webhook signatures
Verify the signature against the exact raw HTTP request body before parsing JSON. Reject stale timestamps and compare signatures in constant time.
Every delivery includes these headers:
| Header | Meaning |
|---|---|
x-cpq-event-id | Stable domain-event identifier. Use this for event deduplication. |
x-cpq-event-type | Event type from the event catalogue. |
x-cpq-event-version | Payload schema version. |
x-cpq-delivery-id | Identifier for this delivery attempt. |
x-cpq-timestamp | Unix timestamp in seconds when Nambikk signed the request. |
x-cpq-signature | Versioned signature in the form v1=<hex digest>. |
Nambikk computes the signature as:
signed_payload = x-cpq-timestamp + "." + raw_request_body
signature = "v1=" + hex(HMAC-SHA256(endpoint_secret, signed_payload))
- Read the timestamp and signature headers.
- Reject timestamps outside the accepted tolerance.
- Compute the HMAC with the endpoint secret.
- Compare signatures in constant time.
- Parse and validate the event schema.
- Deduplicate by event id.
- Return
2xxafter durable acceptance.
This Node.js example preserves the raw body and uses a timing-safe comparison:
import { createHmac, timingSafeEqual } from "node:crypto";
export function verifyNambikkWebhook(
secret: string,
timestamp: string,
rawBody: string,
receivedSignature: string,
nowSeconds = Math.floor(Date.now() / 1000)
): boolean {
const parsedTimestamp = Number(timestamp);
if (!Number.isSafeInteger(parsedTimestamp)) return false;
if (Math.abs(nowSeconds - parsedTimestamp) > 300) return false;
const digest = createHmac("sha256", secret)
.update(`${timestamp}.${rawBody}`, "utf8")
.digest("hex");
const expected = Buffer.from(`v1=${digest}`, "utf8");
const received = Buffer.from(receivedSignature, "utf8");
return expected.length === received.length && timingSafeEqual(expected, received);
}
Use the event id, not the delivery id, to deduplicate business processing. A retry has a new delivery id but represents the same event. Never log endpoint secrets or complete credentials.