Verifying webhook signatures

Every webhook delivery carries an X-Nexiel-Signature header. Check it before you trust the body, with about ten lines of standard library code and no Nexiel package.

The signature scheme

The header looks like t=1763030000,v1=5f2b.... t is the Unix time, in seconds, when the delivery attempt was signed. v1 is the hex HMAC-SHA256 of the text {t}.{rawBody}, keyed with your endpoint's signing secret as UTF-8 text. rawBody is the request body exactly as received, before any JSON parsing.

  1. Read the raw body. Parsing and re-serialising JSON changes the bytes and breaks the signature.
  2. Recompute the HMAC over {t}.{rawBody} and compare it with each v1 value in constant time.
  3. Reject a timestamp more than 300 seconds from your clock, so a captured delivery cannot be replayed later.
  4. Answer with a 2xx status, then deduplicate on the event id. Each retry is signed again with a new timestamp.

While you rotate a signing secret, the header carries two v1 values, one per secret: t=1763030000,v1=<previous>,v1=<current>. The verifiers below accept a match on either, so you can deploy the new secret without missing an event. See rotating the signing secret.

The verifier

Both versions return true only for a genuine, recent delivery, and refuse to run without a signing secret, because an empty key would let anyone sign a delivery. They are tested against Nexiel's own signer for genuine, changed, expired, wrongly keyed and rotated deliveries.

verify-nexiel-signature
import { createHmac, timingSafeEqual } from 'node:crypto';

const TOLERANCE_SECONDS = 300;

// rawBody: the request body exactly as received (string or Buffer).
export function verifyNexielSignature(rawBody, header, secret, now = Math.floor(Date.now() / 1000)) {
  if (!secret) throw new Error('NEXIEL_WEBHOOK_SECRET is not set');
  const parts = header.split(',').map((part) => part.trim().split('='));
  const timestamp = Number(parts.find(([key]) => key === 't')?.[1]);
  if (!Number.isInteger(timestamp) || Math.abs(now - timestamp) > TOLERANCE_SECONDS) return false;
  const expected = createHmac('sha256', secret).update(`${timestamp}.${rawBody}`).digest();
  const signatures = parts.filter(([key]) => key === 'v1').map(([, value]) => Buffer.from(value ?? '', 'hex'));
  return signatures.some((signature) => signature.length === expected.length && timingSafeEqual(signature, expected));
}

Reading the raw body in your framework

Most frameworks parse JSON bodies before your handler runs. Each handler below reads the body unparsed, verifies it, and only then parses it.

Webhook endpoint
import express from 'express';
import { verifyNexielSignature } from './verify-nexiel-signature.js';

const app = express();

// express.raw keeps the body as the exact bytes Nexiel signed.
app.post('/webhooks/nexiel', express.raw({ type: 'application/json' }), (req, res) => {
  const header = req.get('X-Nexiel-Signature') ?? '';
  if (!verifyNexielSignature(req.body, header, process.env.NEXIEL_WEBHOOK_SECRET)) {
    return res.status(400).send('invalid signature');
  }
  const event = JSON.parse(req.body.toString('utf8'));
  // Deduplicate on event.id, then hand the event to a queue and answer quickly.
  res.sendStatus(200);
});

Keep the signing secret in a secret store or an environment variable, never in source code. Each endpoint has its own secret, shown once when you create the endpoint and readable again from the dashboard or POST .../secret/reveal.

Send yourself a test delivery

Press Send test event on the dashboard's Webhooks page, or call POST .../test on the endpoint. Nexiel sends a signed webhook.test event to that endpoint, and the delivery list shows your endpoint's response code.

curl -s -X POST https://compliance.nexiel.io/v1/verify/webhook-endpoints/{endpointId}/test \
  -H "Authorization: Bearer $NEXIEL_ACCESS_TOKEN" \
  -H "Idempotency-Key: $(uuidgen)"

SDK helpers

The TypeScript and Python SDKs, coming to npm and PyPI, wrap the same check as constructWebhookEvent and construct_webhook_event. See Client SDKs.