# Webhooks

Receive delivery updates, driver locations and wallet balance notifications at your callback URLs.

## Configuration

1. Open [Automated Ordering](https://business.armadadelivery.com/automated-ordering) and select the key used to create the delivery.
2. Set its **Order update webhook** URL and select the statuses your receiver needs, including **Canceled** or **Completed** when testing those flows. Location and wallet callbacks have separate settings.
3. Click **Save changes**. Callback delivery and the test button use the saved settings.

Creation always sends an initial `pending` callback. The selectable statuses are Accepted, En route, Completed, Failed and Canceled. After creation, a non-empty selection filters updates; an empty selection sends every status, including `dispatched`.

Each key belongs to one merchant. Use `x-armada-key-id` to map a callback to your saved key configuration. Keep your delivery-code-to-branch mapping when creating an order.

## Events

| Topic                    | Payload                                           |
| ------------------------ | ------------------------------------------------- |
| `order.updated`          | Delivery response; inspect `code` and `status`    |
| `order.location.updated` | Delivery `code` and `location`                    |
| `wallet.balance_low`     | `balance`, `warning_balance_level` and `currency` |

### Order updated

```json
{
  "code": "D000000001",
  "status": "pending",
  "test_mode": false,
  "amount": 4.5,
  "created_at": "2026-04-16T18:12:03.117Z",
  "currency": "KWD",
  "delivery_fee": 2,
  "customer": {
    "name": "Example customer",
    "phone": "+96590000000",
    "address": "Salmiya, Kuwait",
    "latitude": 29.3759,
    "longitude": 47.9774
  },
  "driver": {
    "name": "",
    "phone": "",
    "latitude": null,
    "longitude": null
  },
  "logistics": {
    "estimated_distance": 4820,
    "estimated_duration": 743,
    "tracking_url": "https://example.invalid/tracking/customer-link",
    "pickup_qr_url": "https://example.invalid/pickup/qr"
  }
}
```

The same topic is used for all configured delivery statuses. Driver fields may be empty; retrieve the delivery for current details.

### Location updated

```json
{
  "code": "D000000001",
  "status": "en_route",
  "location": {
    "latitude": 29.3421,
    "longitude": 47.9558
  }
}
```

Save a location callback URL and interval to receive updates while a delivery is `en_route`. To disable updates for future deliveries, clear the URL and save. Contact Armada to stop location updates for active deliveries.

### Wallet balance low

```json
{
  "balance": 4.25,
  "warning_balance_level": 5,
  "currency": "KWD"
}
```

The payload uses the key's warning threshold when configured. Use `GET /v2/wallet` for the current balance and wallet warning threshold.

## Headers

| Header                   | Meaning                                     |
| ------------------------ | ------------------------------------------- |
| `x-armada-webhook-topic` | Event topic                                 |
| `x-armada-webhook-id`    | Header identifier beginning with `wh_`      |
| `x-armada-key-id`        | API key record ID, not the credential value |
| `x-armada-key-type`      | `v2`                                        |
| `x-armada-api-version`   | `v2`                                        |
| `x-armada-timestamp`     | ISO-8601 header creation time               |
| `User-Agent`             | `Armada Automated Ordering`                 |

Location callbacks can reuse their initial webhook ID and timestamp. Do not discard them solely because that ID has already been seen.

## Receiver authentication

Verify notifications through your signed API client before updating your application. For delivery and location notifications, fetch `GET /v2/deliveries/{id}` using the delivery code. For wallet notifications, fetch `GET /v2/wallet`. Apply the returned API state.

Callbacks are unsigned; their metadata headers identify the topic and key. [Request signing](https://docs.armadadelivery.com/v2/authentication.md) authenticates your API reads.

Enable `delivery:read` for delivery and location verification, and `wallet:read` for wallet verification.

## Processing

Validate and store the event, then return `2xx` promptly. Verify and process it asynchronously. When an order-update URL is saved, delivery creation waits for the initial `pending` callback response.

Route stored events by topic and make handlers safe to run more than once:

```javascript
// Run after payload validation, storage and HTTP acknowledgment.
async function routeWebhook(topic, body) {
	switch (topic) {
		case 'order.updated':
		case 'order.location.updated': {
			const { data } = await armada.deliveries.get(body.code);
			return onDeliveryState(data);
		}
		case 'wallet.balance_low': {
			const { data } = await armada.wallet.get();
			return onWalletBalance(data);
		}
		default:
			return;
	}
}
```

Use the signed client from the [quickstart](https://docs.armadadelivery.com/v2/quickstart.md) and supply `onDeliveryState` and `onWalletBalance` in your application.

Order-update callbacks use a single delivery attempt. Handle out-of-order events and use `GET /v2/deliveries/{id}` to reconcile the current state after a missing callback.

## Test your receiver

**Send delivery test** posts a fixed `TEST123` / `pending` sample to the saved delivery URL to check connectivity. Click **Save changes** before using it.

The connectivity sample has no `test_mode` field or associated delivery record. Create a test delivery to validate payload handling and API verification.

To test lifecycle events, [create a test-mode delivery](https://docs.armadadelivery.com/v2/test-mode.md) with that key:

| Test         | Action                                                                        | Expected callback                            |
| ------------ | ----------------------------------------------------------------------------- | -------------------------------------------- |
| Cancellation | Save **Canceled**, create a fresh test delivery and cancel before completion. | Topic `order.updated`, `status: "canceled"`  |
| Completion   | Save **Completed**, create a fresh test delivery and let it finish.           | Topic `order.updated`, `status: "completed"` |

If a callback is missing, check the saved URL and status selection, then read the delivery's current state.
