Testing a WhatsApp webhook before production
Test event, replay, signature tests and local tunnels: how to validate your webhook receiver without waiting for a real customer message.
Waiting for a real customer to write in order to discover the receiver is wrong is the most expensive way to test. Webhooks have traits that make early testing easy: the event has a known format, the signature is a pure function and the Hub offers a synthetic event and a replay. This article assembles those pieces into a four-level testing routine, from cheapest to closest to production.
The Hub's test event
The test event, webhook.test, travels the same path as any real delivery and respects the endpoint's payload_mode. You trigger it with a POST to the webhook's test route, and the response is 202: the event was queued, not delivered. The outcome shows up later in the delivery list, with the status, attempt count and your receiver's last HTTP status.
There are limits to know. It is 10 tests per hour per endpoint, which discourages using the test as a development loop. And only an active endpoint accepts a test; paused or disabled answers 404. If you just hit the failure limit and the endpoint paused, reactivate it before testing.
The test event validates the whole path: URL accepted by the destination rules, DNS, TLS, signature and response time. What it does not validate is your handling of real content, because the payload is synthetic. That is why it is the first level, not the only one.
Signature tests: the pure function
Signature verification is a pure function of body, headers and secret, and it is the component that most deserves automated testing. Three cases cover almost everything: the correct signature is accepted, a body altered by a single byte is rejected, and a malformed signature is rejected without throwing.
import { test } from 'node:test'
import assert from 'node:assert'
import { createHmac } from 'node:crypto'
import { verificar } from './verificar.js'
const SEGREDO = 'whsec_TEST_SECRET'
const corpo = '{"id":"evt-1","event":"message.received"}'
const ts = '1766000000'
const entrega = 'c2d3e4f5-6a7b-4c8d-9e0f-1a2b3c4d5e6f'
const assinar = (c) => 'sha256=' + createHmac('sha256', SEGREDO)
.update(`${ts}.${entrega}.${c}`).digest('hex')
test('accepts the correct native signature', () => {
assert.ok(verificar({ ts, entrega, assinatura: assinar(corpo), corpo }, SEGREDO))
})
test('rejects a body altered by one byte', () => {
assert.ok(!verificar({ ts, entrega, assinatura: assinar(corpo), corpo: corpo + ' ' }, SEGREDO))
})
test('rejects a malformed signature without throwing', () => {
assert.ok(!verificar({ ts, entrega, assinatura: 'sha256=zz', corpo }, SEGREDO))
})The third test exists for a common defect. A constant-time comparison throws if buffers differ in length, and a header with 64 non-hex characters makes a short buffer. Without the format check before conversion, the handler crashes with 500 instead of rejecting. A test that feeds garbage to the verifier catches that before an attacker does.
Add a fourth case if you accept both Hub formats: the native format signs timestamp, delivery id and body, and evohub signs only the body. The test should feed each format the other's signature and expect rejection. That way a mix-up of the two formulas fails in your suite, not in production.
A concrete example: real payload fixtures
Save payload samples in files and use them as fixtures in handler tests. Meta's documentation has payload examples for messages and statuses, and the Hub's shows the normalized envelope with fields such as message_id, from, text and content. Instead of inventing JSON from memory, copy one of those examples and vary one field at a time.
- Text message: the happy path.
- Media message: content.media filled in, text coming from the caption.
- Event with errors filled in: content.available false, to test the path where you know the customer wrote but not what.
- Statuses delivered, read and failed, in different orders, to exercise the state-advance rule.
- The same event twice, to prove deduplication.
The duplicate point is decisive. Replay the same list of events twice and assert that the second pass changes nothing: that single test catches most idempotency defects. Pair it with a concurrency test, processing the same event in two workers at the same time.
Replay and the local tunnel
Replay resends a delivery that already finished, by the identifier the delivery list returns. Only finished deliveries (succeeded, dead or canceled) can be replayed; one in pending, processing or retry answers 404. Replay creates a new delivery, with a new delivery id and the same body, and the event keeps the same id in the body.
That makes replay an excellent testing tool: take a real event your code mishandled, fix it, and replay until it passes. Remember that if your receiver deduplicates on the event id, the replay of a successful delivery will be discarded, so clear the event's deduplication record first, or use a delivery that ended up dead.
To develop on your machine, the URL must be public HTTPS, on port 443, with no redirect: localhost and private IPs are rejected. Use a tunnel with a public domain and remember the destination check repeats on every delivery.
End-to-end test with a real number
The last level uses a real test number: you send a message from your phone and follow the event from start to finish. Do this at least once before going live, and again after every infrastructure change. If the Meta app is in development mode, remember the documentation warns that some webhooks are not sent in that mode; switch it to Live to test production behavior.
During the end-to-end test, deliberately break something. Stop the receiver for a minute and send a message; watch the delivery move to retry and then to succeeded when you restart it. Send the same message from the replay route and confirm your deduplication drops it. Rotate the secret and confirm the new value is the one in your environment. Each of these rehearses a real incident while the stakes are zero, and each takes only a few minutes.
Record the result of each level in a short checklist: synthetic test passed, signature suite passed, fixtures passed, replay of a dead event worked, real message arrived. The commands are in the CRPRO Hub webhooks reference, and the quickstart covers the first authenticated calls.
Frequently asked questions
Does the test event replace testing with a real message?
No. It validates URL, signature and response time, but the payload is synthetic. Also run fixture tests and at least one end-to-end test.
How many tests can I trigger per endpoint?
Ten per hour per endpoint. The test answers 202, because the event is queued, and the outcome appears in the delivery list.
Can I replay any delivery to test?
Only finished ones: succeeded, dead or canceled. In-flight deliveries answer 404. A replay keeps the event id and changes the delivery id.
Can I receive webhooks on localhost?
Not directly: the Hub rejects localhost and private IPs. Expose the port through a tunnel with a public HTTPS domain on port 443.
Fontes
Documentação oficial da Meta, conferida em 31 de outubro de 2026.