Skip to content

WebhookHandler

Defined in: src/resources/webhooks.ts:189

Framework-agnostic webhook handler binding a verify token and/or App Secret.

It imports no web framework; the caller wires it into their HTTP layer, passing the raw request body for signature verification. See the README for a complete Express example.

Configuration rules:

  • Omitting the appSecret key entirely means no signature enforcement: WebhookHandler.parse and WebhookHandler.verifyOrThrow accept unsigned bodies. Only do this when something else authenticates the request.
  • Passing appSecret or verifyToken with an undefined or empty value (typically an unset environment variable) throws WhatsAppWebhookError instead of silently disabling verification.

new WebhookHandler(config?): WebhookHandler

Defined in: src/resources/webhooks.ts:199

WebhookHandlerConfig = {}

WebhookHandler

WhatsAppWebhookError when appSecret or verifyToken is present in config but is not a non-empty string.

handleVerification(query): WebhookVerificationResult

Defined in: src/resources/webhooks.ts:229

Runs the GET verification handshake using the configured verify token.

Record<string, string | undefined>

Raw query map with hub.* keys.

WebhookVerificationResult

WhatsAppWebhookError when no verify token was configured.


isValid(rawBody, signatureHeader): boolean

Defined in: src/resources/webhooks.ts:244

Returns whether the signature is valid for the raw body using the configured App Secret. Returns false (rather than throwing) when no App Secret was configured.

WebhookRawBody

The raw, unparsed request body (string, Uint8Array/Buffer or ArrayBuffer).

string | undefined

The X-Hub-Signature-256 header value.

boolean


parse(rawBody, signatureHeader?): ParsedWebhook

Defined in: src/resources/webhooks.ts:283

Verifies (when an App Secret is configured) and parses a webhook delivery.

When an App Secret was configured, a missing or invalid signature throws a WhatsAppWebhookError before parsing. The raw body is used both for signature verification and for parsing.

WebhookRawBody

The raw, unparsed request body (string, Uint8Array/Buffer or ArrayBuffer).

string

The X-Hub-Signature-256 header value, if present. A body larger than maxBodyBytes (default 3 MiB) is rejected before it is hashed or parsed.

ParsedWebhook

WhatsAppWebhookError on an oversized body, failed verification or malformed payload.


verifyOrThrow(rawBody, signatureHeader): void

Defined in: src/resources/webhooks.ts:259

Verifies the signature and throws on failure. A no-op when no App Secret was configured (nothing to verify against).

WebhookRawBody

The raw, unparsed request body (string, Uint8Array/Buffer or ArrayBuffer).

string | undefined

The X-Hub-Signature-256 header value.

void

WhatsAppWebhookError when verification fails.

Independent open-source project. Not affiliated with, endorsed by or sponsored by Meta or WhatsApp. Released under the MIT licence. Source on GitHub.