# Webhooks

> Receive every change of a delivery at your URL.

Set `options.webhook_url` when you create a delivery, and REMI calls it at every step. Without a URL there are no webhooks.

## The call

- `POST` with `Content-Type: application/json`.
- The body is the [Delivery](https://docs.remi.robot.com/delivery/reference#type-Delivery), as `GET /delivery/{id}` returns it, not wrapped in `data`.
- `event_type` names the event that sent it. `bot.control` says who drives the bot (webhooks only).
- Every header in `options.webhook_headers` is sent too. Calls are not signed: put your own secret in a header and check it.

```json
{ "id": "ES-5867", "state": "at_pickup", "event_type": "node_arrived", "paused": false, "bot": { "id": "4T001" } }
```

## Events

| `event_type` | When |
| --- | --- |
| `job_created` | The delivery was created. |
| `job_bot_assigned` | A bot was assigned. |
| `bot_taken` | The bot started serving it. |
| `job_started` | It started. |
| `job_state_changed` | Any change of state. |
| `node_started` | The bot left for the pickup or the dropoff. |
| `node_arriving` | The bot is close. |
| `node_arrived` | The bot arrived. |
| `node_eta_changed` | An arrival time moved. Only with `options.eta_updates`. |
| `job_completed` | Delivered or returned. |
| `job_canceled` | Canceled. |
| `job_failed` | It could not be completed. |

Pickup-only orders also send `node_phase_completed` (ready for pickup), `node_completed` (picked up) and, per station, `action_completed`.

Decide what to do from `state`, not from `event_type`.

## Order and retries

- Calls for one delivery come one at a time, in order. Different deliveries are independent.
- A `5xx`, a connection error or no answer within 30 seconds is retried with backoff, up to 5 attempts. Later calls for that delivery wait.
- After 5 failed attempts the call is dropped and the next one goes out. Treat webhooks as at least once, with possible gaps, and read `GET /delivery/{id}` to reconcile.
- A retry rebuilds the body: live fields such as `state` and the bot's position are current, and `event_type` stays the original event.

Answer `2xx` quickly and do the work afterwards: a slow endpoint delays every later call for that order.

## Arrival time updates

With `options.eta_updates: true`, `node_eta_changed` also arrives while the bot drives to a stop: when the arrival time moved 90 seconds or more, or after 5 minutes without one, at most once a minute. The new time is in `estimates.pickup.arrival_at` or `estimates.dropoff.arrival_at`.
