WhatsApp Cloud API webhook in a Next.js App Router route handler
In a Next.js App Router route handler, pass the Request to handleRequest from createWebhookHandler. It reads the raw bytes itself, so do not call request.json() first. The example imports from the /web entry point and keeps the default ackMode, so the handlers finish before the response. To answer first, set ackMode: 'before' and pass waitUntil; see Edge runtimes.
// Next.js App Router route handler: app/api/webhook/route.tsimport { createWebhookHandler } from '@solvejet/whatsapp-cloud-api/web';import { echoText, requireEnv } from '../../../../shared/reply.js';
// The default ackMode ('after') runs the handlers before answering, so the work// finishes inside the invocation. To answer first, set ackMode: 'before' and// pass waitUntil from @vercel/functions (or use after() from next/server).const webhook = createWebhookHandler({ appSecrets: requireEnv('WHATSAPP_APP_SECRET'), verifyToken: requireEnv('WHATSAPP_VERIFY_TOKEN'),});
webhook.on('message', ({ message }) => echoText(message));
// Verify-token handshake and signed deliveries both go through handleRequest.export const GET = (request: Request): Promise<Response> => webhook.handleRequest(request);export const POST = (request: Request): Promise<Response> => webhook.handleRequest(request);The full runnable project is in examples/nextjs. Each step is explained in verify the signature and the hub.challenge handshake. Meta’s own description of the two request types is in the webhook endpoint guide.
Independent open-source project. Not affiliated with, endorsed by or sponsored by Meta or WhatsApp. Released under the MIT licence. Source on GitHub.