# Delivery and retries

What Chatwise does with each answer, when it tries again, and how to handle an event more than once.

## Answer quickly

Answer with any **2xx** status within **10 seconds**. Only the status counts: Chatwise doesn't read the body.

Save the event or put it in a queue, answer, and do the work afterwards. An endpoint that does slow work before it answers times out, and gets the same event again.

## What each answer does

| Your answer | What happens |
| --- | --- |
| 2xx | Delivered. |
| 408, 425, 429 or 5xx | Retried. |
| No answer in 10 seconds, or a connection error | Retried. |
| [410 Gone](#410-gone) | Failed, and the endpoint is disabled. |
| Any other status, redirects included | Failed, without a retry. |

So answer 4xx only for a request you will never accept, such as one whose signature is wrong. Make sure nothing in front of your code does it for you: a framework or proxy that [limits bodies](https://chatwise.so/docs/webhooks/verify-signatures.md#in-a-framework) to less than 256 KiB answers 413 to large events, and those aren't retried. If a mistake of yours refused good events, fix it and [redeliver](https://chatwise.so/docs/webhooks/testing.md#redeliver) them.

## Retry schedule

A delivery is tried 8 times. After a failed attempt the next one comes after:

| Attempt | Waits after the one before |
| --- | --- |
| 1 | The first attempt |
| 2 | 10 seconds |
| 3 | 1 minute |
| 4 | 5 minutes |
| 5 | 30 minutes |
| 6 | 2 hours |
| 7 | 6 hours |
| 8 | 16 hours |

Each wait can be up to 20% shorter, so that many retries don't arrive together. When all 8 attempts failed, the delivery fails with `retry_exhausted`, and `lastStatusCode` has the last answer's status.

Every attempt sends the same event with the same `webhook-id`. The `webhook-timestamp` and the signature are new each time.

## Handle an event more than once

Delivery is **at least once**. The same event can arrive again, for example when your answer got lost on the way or someone redelivers it. Keep the `webhook-id` of every event you handled, and skip one you already have. If two endpoints lead to the same receiver, an event they both receive arrives twice with the same id:

```sql
CREATE TABLE handled_webhooks (id text PRIMARY KEY, handled_at timestamptz NOT NULL DEFAULT now());

-- In the transaction that does the work. No row inserted: it was already handled.
INSERT INTO handled_webhooks (id) VALUES ($1) ON CONFLICT DO NOTHING;
```

Keep the ids for at least 30 days, as long as an event can be redelivered.

## Don't rely on the order

Events can arrive in another order than they happened, above all after a retry. Sort by `data.message.sentAt` where the order matters, or [read the conversation's messages](https://chatwise.so/docs/api/list-conversation-messages.md).

## 410 Gone

Answer 410 to say that a URL doesn't want events any more. Chatwise then:

- marks the delivery as failed, without a retry;
- sets the endpoint's `status` to `DISABLED`, with the reason in `health.disabledReason`.

A disabled endpoint receives nothing. To turn it on again, [set its status](https://chatwise.so/docs/api/update-webhook-endpoint.md) to `ACTIVE`. Its deliveries that were still waiting are then sent, but events that happened in between aren't.

Nothing else disables an endpoint.

## What isn't sent

- **Events from before the endpoint** was created, or from while it was paused or disabled.
- **A message that was unsent, deleted or hidden** before its delivery went out. The delivery fails with `message_removed`, which doesn't count against the endpoint. If the event wasn't prepared yet, there is no delivery.
- **An event larger than 256 KiB.** The delivery fails with `payload_too_large`.
- **An event whose payload can't be built.** There is no delivery.

## Slow endpoints

Chatwise sends an endpoint only a few requests at a time, so one that answers slowly delays its own events, and can delay those of the workspace's other endpoints.

[Get the endpoint](https://chatwise.so/docs/api/get-webhook-endpoint.md) to see how it is doing: `health` has its last success, its last failure and how many attempts failed in a row.
