# Webhook delivery

> Five attempts, then `DLQ`. While the circuit is open, events wait; they are not dropped. Any delivery can be replayed.

Each delivery row has `maxAttempts` 5. The queue job is added with `attempts: 5` and BullMQ backoff `{ type: "exponential", delay: 10000 }`. That backoff is what delays the next HTTP try.

On a failure the worker also writes `nextRetryAt` as `min(10000 * 3^(attempt - 1), 900000)` milliseconds: 10s, 30s, 90s, 270s, 810s. Nothing in the worker sleeps until `nextRetryAt`. The row is a record. The retry clock is BullMQ’s.

After the last attempt, `status` is `DLQ`.

A successful delivery sets `status` to `DELIVERED` and, if `failCount` was non-zero, resets it to 0.

Ten consecutive failed attempts open the circuit for 5 minutes (`circuitOpen`, `circuitUntil` on `GET /webhook-config`). A success resets the counter, so the ten are consecutive. Saving webhook config closes the circuit immediately.

While the circuit is open:

- New events are stored and queued with a delay until `circuitUntil` (`nextRetryAt` shows it). Nothing is dropped.
- A delivery that was already queued is moved back to delayed until `circuitUntil`. Waiting on the circuit does not spend an attempt.
- When the cooldown ends, the next run closes the circuit and posts.

## Replay

Scope `webhooks:manage`. A replay resets the row (`PENDING`, `attempts` 0) and sends it again with the same `X-Vitral-WhatsApp-Delivery-Id` and an extra `X-Vitral-WhatsApp-Replay: true` header. A receiver that dedupes on the delivery id should let a replay through when it wants to re-process it.

```http
POST /api/v1/webhook-config/deliveries/:id/replay
```

Any status, including `DELIVERED`. `202` `{ "data": { "replayed": 1, "ids": ["…"] } }`. Unknown id: `404`.

```http
POST /api/v1/webhook-config/deliveries/replay
Content-Type: application/json

{ "statuses": ["DLQ"], "eventType": "message.received", "from": "2026-10-20T00:00:00Z", "to": "2026-10-21T00:00:00Z", "limit": 500 }
```

Pass `ids` (up to 1000) or at least one filter. Oldest first, at most `limit` (default 100, max 1000) per call. Repeat the call to drain more.

`GET /api/v1/webhook-config/deliveries` accepts `status` and `eventType` filters next to `page` and `pageSize`.

Inbound Meta retries are deduped before your URL is called: the same payload hash is stored once, and inbound messages upsert on Meta’s message id. Your handler can still see the same `X-Vitral-WhatsApp-Delivery-Id` twice if we retry after your server timed out but had already processed the body. Deduplicate on `X-Vitral-WhatsApp-Delivery-Id` and on `data.value.messages[].id` / `data.value.statuses[].id`.

## Routing a business

```text
webhook data.value.metadata.phone_number_id
  → PhoneNumber.waPhoneNumberId
  → PhoneNumber.customerId
  → Customer.externalCustomerId
```

`phone.disconnected` includes Whitelabel WhatsApp Cloud API `phoneNumberIds`. `referral.received` includes Whitelabel WhatsApp Cloud API `phoneNumberId` and does not overwrite it. `message.received` and `message.status` add Whitelabel WhatsApp Cloud API `phoneNumberId` on the envelope and on `data` when that key was absent. `data.value.metadata.phone_number_id` stays Meta's id. Use that value for the lookup above.
