Skip to navigation

validateWebhookSignature

View as MarkdownOpen in Claude

Verify that an inbound request was genuinely signed by SignalWire. This is the primary entry point for webhook signature validation. Compute it over the raw request body before any JSON or form parsing.

validateWebhookSignature(signingKey: string, signature: string, url: string, rawBody: string): boolean

This function checks the SHA-1 X-SignalWire-Signature header. When a request also carries X-SignalWire-Sha256-Signature, prefer validateWebhookSignatureSha256().

rawBody must be the raw request body as a UTF-8 string. Passing a parsed object throws a TypeError. For pre-parsed form params, use validateRequest() instead.

Parameters

signingKey
stringRequired

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

signature
stringRequired

The signature header value (X-SignalWire-Signature or X-Twilio-Signature). A missing or empty value returns false.

url
stringRequired

The full URL SignalWire POSTed to.

rawBody
stringRequired

The raw request body as a UTF-8 string, before JSON or form parsing.

Returns

boolean — true when the signature matches; false otherwise. The comparison is constant-time.

Example

import { validateWebhookSignature } from '@signalwire/sdk';
import { Hono } from 'hono';
const app = new Hono();
app.post('/webhook', async (c) => {
const rawBody = await c.req.text();
const signature = c.req.header('x-signalwire-signature') ?? '';
const url = c.req.url;
const ok = validateWebhookSignature(
process.env.SIGNALWIRE_SIGNING_KEY!,
signature,
url,
rawBody,
);
if (!ok) return c.text('Forbidden', 403);
return c.json({ received: JSON.parse(rawBody) });
});