Verify WhatsApp Cloud API webhook signatures in Node.js (X-Hub-Signature-256)
What Meta sends
Section titled “What Meta sends”Every webhook POST carries an X-Hub-Signature-256 header of the form sha256=<hash>. Per Meta’s webhook endpoint guide, the hash is an HMAC-SHA256 of the JSON payload using your app secret as the key. To validate, compute that HMAC over the payload, compare it with the part of the header after sha256=, and treat the request as invalid if they differ. Meta says to answer a valid request with 200 and an invalid one with a 400-level status or anything other than 200. Verifying is optional on Meta’s side. Do it anyway: without it, anyone who finds your URL can post fake events.
The HMAC is computed over the exact bytes Meta sent. If your framework parses the JSON first and you hash JSON.stringify(body), the bytes can differ and valid signatures fail. Keep the raw body. See Webhooks: raw body and the recipes for Express, Next.js, Hono and Fastify.
With the SDK
Section titled “With the SDK”// The function form: returns true only when the header is present, starts with// "sha256=" and matches the HMAC-SHA256 of the raw body keyed with the app secret.const ok = verifySignature(rawBody, signatureHeader, process.env.WHATSAPP_APP_SECRET!);if (!ok) { // Respond 401 and stop. Do not parse the body.}verifySignature(rawBody, signatureHeader, appSecret) returns a boolean and never throws on bad input. It accepts a string, Uint8Array (including Buffer) or ArrayBuffer. The comparison is constant-time. It returns false when the header is missing, does not start with sha256=, or does not match, and always false when appSecret is empty or not a string, so an unset environment variable cannot validate anything.
// The handler form: binds the secret once. The constructor throws// WhatsAppWebhookError when appSecret is present but empty or not a string.const handler = new WebhookHandler({ appSecret: process.env.WHATSAPP_APP_SECRET! });
handler.isValid(rawBody, signatureHeader); // boolean, never throwshandler.verifyOrThrow(rawBody, signatureHeader); // throws WhatsAppWebhookError when invalidconst { messages, statuses } = handler.parse(rawBody, signatureHeader); // verify, then parseWebhookHandler binds the secret once:
isValidreturns a boolean.verifyOrThrowthrowsWhatsAppWebhookErrorwhen the signature is wrong.parseverifies and then parses, so a bad signature throws before any parsing happens.- The constructor throws
WhatsAppWebhookErrorwhenappSecretorverifyTokenis present but empty or not a string. Omitting theappSecretkey disables signature enforcement, soparseaccepts unsigned bodies. Only do that when something else authenticates the request.
Body size
Section titled “Body size”Meta states that webhook payloads can be up to 3 MB. WebhookHandler.parse and parseWebhook reject a string or byte body above maxBodyBytes, which defaults to 3 MiB (3,145,728 bytes), before hashing or parsing. Pass maxBodyBytes to change the limit or false to turn it off:
// Default 3 MiB. Pass a number to lower it, or false to turn the check off.new WebhookHandler({ appSecret: process.env.WHATSAPP_APP_SECRET!, maxBodyBytes: 1024 * 1024 });An oversized body throws WhatsAppWebhookError.
Which status code to return
Section titled “Which status code to return”The recipes answer a bad signature with 401. They answer a validly signed but malformed payload with 200, because Meta retries non-200 deliveries for up to 7 days and a payload that cannot parse will never succeed. Meta can deliver the same event more than once, so deduplicate by message ID.
For the verification request that Meta sends when you save the webhook URL, see the hub.challenge handshake.
Independent open-source project. Not affiliated with, endorsed by or sponsored by Meta or WhatsApp. Released under the MIT licence. Source on GitHub.