Test a WhatsApp webhook handler without Meta (signed requests in TypeScript)
The @solvejet/whatsapp-cloud-api/testing entry point builds valid deliveries and signs them the way Meta does, so you can test a handler in-process with no tunnel. It only uses Web Crypto and works on Node 20 and later, Bun, Deno and workerd. The main entry does not import it.
Send a signed delivery to your handler
Section titled “Send a signed delivery to your handler”const webhook = createWebhookHandler({ appSecrets: secret, verifyToken: 'test-token' });const texts: string[] = [];webhook.on('message', ({ message }) => { if (message.type === 'text') texts.push(message.text.body);});// A signed POST shaped like a Meta delivery.const request = await createTestRequest({ payload: buildWebhookPayload('text', { entry: [ { id: '102290129340398', changes: [ { field: 'messages', value: { messaging_product: 'whatsapp', metadata: { display_phone_number: '15550783881', phone_number_id: '106540352242922' }, messages: [ { from: '15551234567', id: 'wamid.TEST', timestamp: '1700000000', type: 'text', text: { body: 'hi' }, }, ], }, }, ], }, ], }), secret,});const response = await webhook.handleRequest(request);// response.status is 200 and texts is ['hi'].buildWebhookPayload(kind, overrides?) builds a complete delivery. The kinds are text, status, call, template_status, flow, account_update and group. Plain objects in overrides merge key by key. Arrays replace the default. Keys named __proto__, constructor or prototype throw WhatsAppWebhookError.
createTestRequest takes a payload or an exact body, plus an optional secret, signature, url and headers, and returns a POST Request. It is async because signing is.
Check that bad requests are rejected
Section titled “Check that bad requests are rejected”// A request with a wrong signature gets 401 and runs no handler.const forged = await createTestRequest({ payload: buildWebhookPayload('status'), signature: 'sha256=' + '0'.repeat(64),});const rejected = await webhook.handleRequest(forged);// rejected.status is 401.Sign a body yourself
Section titled “Sign a body yourself”// Sign a body yourself, for example to call a running server with fetch.const signature = await signWebhookBody('{"object":"whatsapp_business_account"}', secret);signWebhookBody returns sha256=<hex>, the value of the X-Hub-Signature-256 header.
Related: the webhook handler and edge runtimes.
Independent open-source project. Not affiliated with, endorsed by or sponsored by Meta or WhatsApp. Released under the MIT licence. Source on GitHub.