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
appSecretkey entirely means no signature enforcement: WebhookHandler.parse and WebhookHandler.verifyOrThrow accept unsigned bodies. Only do this when something else authenticates the request. - Passing
appSecretorverifyTokenwith anundefinedor empty value (typically an unset environment variable) throws WhatsAppWebhookError instead of silently disabling verification.
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new WebhookHandler(
config?):WebhookHandler
Defined in: src/resources/webhooks.ts:199
Parameters
Section titled “Parameters”config?
Section titled “config?”WebhookHandlerConfig = {}
Returns
Section titled “Returns”WebhookHandler
Throws
Section titled “Throws”WhatsAppWebhookError when appSecret or verifyToken is
present in config but is not a non-empty string.
Methods
Section titled “Methods”handleVerification()
Section titled “handleVerification()”handleVerification(
query):WebhookVerificationResult
Defined in: src/resources/webhooks.ts:229
Runs the GET verification handshake using the configured verify token.
Parameters
Section titled “Parameters”Record<string, string | undefined>
Raw query map with hub.* keys.
Returns
Section titled “Returns”Throws
Section titled “Throws”WhatsAppWebhookError when no verify token was configured.
isValid()
Section titled “isValid()”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.
Parameters
Section titled “Parameters”rawBody
Section titled “rawBody”The raw, unparsed request body (string, Uint8Array/Buffer or ArrayBuffer).
signatureHeader
Section titled “signatureHeader”string | undefined
The X-Hub-Signature-256 header value.
Returns
Section titled “Returns”boolean
parse()
Section titled “parse()”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.
Parameters
Section titled “Parameters”rawBody
Section titled “rawBody”The raw, unparsed request body (string, Uint8Array/Buffer or ArrayBuffer).
signatureHeader?
Section titled “signatureHeader?”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.
Returns
Section titled “Returns”Throws
Section titled “Throws”WhatsAppWebhookError on an oversized body, failed verification or malformed payload.
verifyOrThrow()
Section titled “verifyOrThrow()”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).
Parameters
Section titled “Parameters”rawBody
Section titled “rawBody”The raw, unparsed request body (string, Uint8Array/Buffer or ArrayBuffer).
signatureHeader
Section titled “signatureHeader”string | undefined
The X-Hub-Signature-256 header value.
Returns
Section titled “Returns”void
Throws
Section titled “Throws”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.