Webhook handler

Build a reliable ChatRail webhook handler

Verify every event, acknowledge it quickly and process delivery updates and replies without losing data or performing work twice.

Level
Intermediate
Time
15 minutes
Stack
Node.js · Webhooks
ChatRail webhook verification and processing pipeline
The production boundary used throughout this tutorial.
01

Create a narrow public endpoint

Accept only HTTPS POST requests, cap the request size and keep the raw body available for signature verification. Do not expose diagnostic details to callers.

02

Verify before parsing

Compute the signature over the exact raw bytes received and compare it in constant time. Reject missing, malformed, stale or invalid signatures before reading event fields.

Node.js
const expected = createHmac("sha256", process.env.WEBHOOK_SECRET)
  .update(rawBody)
  .digest("hex");

if (!timingSafeEqual(Buffer.from(signature), Buffer.from(expected))) {
  return res.status(401).end();
}
03

Acknowledge, then do the work

Return a success response after authentication and durable enqueueing. Database joins, AI calls and outbound requests belong in a bounded worker queue, not in the request path.

Target a fast acknowledgement.

Slow handlers encourage retries and make healthy traffic look like duplicate traffic.

04

Make processing idempotent

Insert the event ID into a table with a unique constraint before applying side effects. If the insert conflicts, acknowledge the replay and skip the work.

PostgreSQL
INSERT INTO webhook_events (workspace_id, event_id, received_at)
VALUES ($1, $2, now())
ON CONFLICT (workspace_id, event_id) DO NOTHING;
05

Route by event type

Handle inbound messages, delivery changes and connection changes independently. Unknown event types should be recorded safely and ignored rather than crashing the consumer.

06

Exercise replay and recovery

Replay a signed fixture, send events out of order, interrupt the worker and rotate the secret. Confirm that no event is lost and no customer action is repeated.

  • Invalid signature rejected
  • Replay has no second side effect
  • Queue failure is observable
  • Secret rotation is tested
STRAIGHT ANSWERS

Questions before you build?

Start with the documentation or review the practical answers below.

Is ChatRail the official Meta Cloud API?

No. ChatRail uses a WhatsApp Linked Devices session. Review the transport trade-offs before production use.

Can I use it without AI?

Yes. Sending, delivery tracking and webhooks work without enabling AI.

Where should I start?

Use the Quickstart for your first request, then choose a workflow tutorial.