Skip to content

Handle the WhatsApp webhook verification request (hub.challenge)

When you set or edit the Callback URL or Verify token in the App Dashboard, Meta sends a GET request to your endpoint. Per Meta’s webhook endpoint guide, the query string has three parameters:

  • hub.mode, always subscribe.
  • hub.challenge, a string Meta generates.
  • hub.verify_token, the verify token you typed into the dashboard.

To pass, compare hub.verify_token with the string stored on your server. If it matches, respond with HTTP 200 and the hub.challenge value. Otherwise respond with a 400-level status or anything other than 200. If your endpoint answers anything else, Meta treats it as unverified and sends no webhooks. The Graph API webhooks guide adds that PHP rewrites dots in query names to underscores, which matters only if you run PHP.

The verify token is a string you choose. It is separate from your app secret and your access token.

website/snippets/hub-challenge.ts
const handler = new WebhookHandler({ verifyToken: process.env.WHATSAPP_VERIFY_TOKEN! });
// Returns { statusCode: 200, body: <hub.challenge> } when hub.mode is "subscribe" and the
// token matches. Otherwise returns { statusCode: 403, body: 'Forbidden' }.
const { statusCode, body } = handler.handleVerification(query);
// Send `body` as plain text with status `statusCode`. Do not wrap it in JSON.

On success, handleVerification returns { statusCode: 200, body: <hub.challenge> }. On any mismatch it returns { statusCode: 403, body: 'Forbidden' }. Return body as the raw response body with a text content type. Do not wrap it in JSON.

Behavior, as covered in the SDK’s tests:

  • The token comparison is constant-time.
  • hub.mode must be subscribe.
  • An empty or missing expected token always gives 403, so an unset environment variable can never accept a request.
  • The WebhookHandler constructor throws WhatsAppWebhookError when verifyToken is present but empty. handleVerification throws the same error when the handler was built without a verifyToken.
website/snippets/hub-challenge.ts
// The same check without a handler. Keys are the literal hub.* names.
const fromQuery = verifyWebhookQuery(query, process.env.WHATSAPP_VERIFY_TOKEN!);
// Or with the three values already extracted from hub.mode, hub.verify_token and hub.challenge.
declare const mode: string;
declare const token: string;
declare const challenge: string;
const fromParts = verifyWebhook({ mode, token, challenge }, process.env.WHATSAPP_VERIFY_TOKEN!);

verifyWebhookQuery takes the query map with the literal hub.* keys, and verifyWebhook takes the three values. Both return the same { statusCode, body } result.

Next, verify the signature of each delivery. Framework recipes: Express, Next.js, Hono, Fastify.

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