Skip to navigation

Decide whether an inbound webhook is authentic from plain request primitives, with no framework request or response objects. Returns null to let the request through, or a [status, headers, body] triple to write back verbatim. webhookValidationMiddleware() is a thin Hono wrapper over this function; use validate() directly from any other server.

validate(method: string, url: string, headers: Record<string, string>, body: string, signingKey: string): WebhookRejection | null

Parameters

method
stringRequired

The HTTP method. Informational; the signature does not cover it.

url
stringRequired

The full public URL SignalWire POSTed to, including scheme, host, path, and query. Reconstruct it behind a proxy or tunnel before calling.

headers
Record<string, string>Required

Request headers. The signature header is looked up case-insensitively under X-SignalWire-Signature, or the legacy X-Twilio-Signature alias.

body
stringRequired

The raw request body as a string, before any JSON or form parsing.

signingKey
stringRequired

Your SignalWire Signing Key. An empty or non-string value throws an Error.

Returns

WebhookRejection | null — null when the signature is valid. Otherwise a [403, {}, 'Forbidden'] triple, whether the header is missing, empty, or wrong. The body never says which check failed.

Example

import { createServer } from 'node:http';
import { validate } from '@signalwire/sdk';
createServer(async (req, res) => {
const chunks: Buffer[] = [];
for await (const chunk of req) chunks.push(chunk as Buffer);
const body = Buffer.concat(chunks).toString();
const rejection = validate(
req.method ?? 'POST',
`https://${req.headers.host}${req.url}`,
req.headers as Record<string, string>,
body,
process.env.SIGNALWIRE_SIGNING_KEY!,
);
if (rejection) {
const [status, headers, text] = rejection;
res.writeHead(status, headers);
res.end(text);
return;
}
// handle the authentic webhook
res.end('ok');
}).listen(3000);