Skip to main content

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:

HeaderMeaning
x-cpq-event-idStable domain-event identifier. Use this for event deduplication.
x-cpq-event-typeEvent type from the event catalogue.
x-cpq-event-versionPayload schema version.
x-cpq-delivery-idIdentifier for this delivery attempt.
x-cpq-timestampUnix timestamp in seconds when Nambikk signed the request.
x-cpq-signatureVersioned 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))
  1. Read the timestamp and signature headers.
  2. Reject timestamps outside the accepted tolerance.
  3. Compute the HMAC with the endpoint secret.
  4. Compare signatures in constant time.
  5. Parse and validate the event schema.
  6. Deduplicate by event id.
  7. Return 2xx after 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.