# Deliveries

Create, retrieve, cancel and estimate deliveries. See the [API reference](https://docs.armadadelivery.com/v2/reference.md) for request fields and response schemas.

## Permissions and identifiers

| Operation                     | Permission        | Path identifier |
| ----------------------------- | ----------------- | --------------- |
| Create                        | `delivery:write`  | None            |
| Retrieve                      | `delivery:read`   | Delivery `code` |
| Cancel                        | `delivery:cancel` | Delivery `code` |
| Estimate fee (both endpoints) | `delivery:write`  | None            |

Store the delivery `code` returned by creation. Use it for lookup, cancellation and webhook matching.

## Create a delivery

Use `POST /v2/deliveries`. The [quickstart](https://docs.armadadelivery.com/v2/quickstart.md) includes a complete Node example.

Choose `origin_format` and `destination_format`, then supply their matching fields. Pickup from an existing branch uses `branch_format` and `origin.branch_id`. Location-based destinations require `contact_name`, `contact_phone`, `latitude` and `longitude`; `first_line` is optional.

Set `reference` to your order reference. Each successful create request creates a new delivery, including requests that reuse the same reference. After a timeout, confirm the outcome before submitting another request.

## Retrieve a delivery

Use `GET /v2/deliveries/{id}` with the code of a delivery created through this API. Statuses include `pending`, `dispatched`, `accepted`, `waiting_pack`, `en_route`, `completed`, `failed` and `canceled`. Other statuses can occur, including backup-driver states; handle unrecognized values without discarding the response.

```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"
  }
}
```

## Cancel a delivery

Use `POST /v2/deliveries/{id}/cancel` with the delivery **code**. Enable `delivery:cancel`. The JSON body is optional:

```json
{ "reason": "Customer requested cancellation" }
```

A successful cancellation returns `200` and the delivery representation with `status: "canceled"`. Repeating cancellation of a canceled delivery preserves its first reason. An already failed delivery is returned unchanged.

| Result | Meaning and next step                                                                                     |
| ------ | --------------------------------------------------------------------------------------------------------- |
| `200`  | Inspect `status` in the returned delivery.                                                                |
| `400`  | The current state or cancellation policy prevents the operation; a completed delivery cannot be canceled. |
| `409`  | A test delivery changed during cancellation. Read it and retry only if it still needs cancellation.       |

Live deliveries follow the merchant's cancellation and penalty policy. [Simulated test deliveries](https://docs.armadadelivery.com/v2/test-mode.md) can be canceled at `dispatched`, `accepted` and `en_route`; the simulator stops advancing a canceled delivery.

To receive the update, select **Canceled** in the key's saved webhook settings. The topic is `order.updated` with `status: "canceled"`. After a timeout or other error, retrieve the delivery to confirm its state before retrying.

## Estimate a delivery

Use `branch_format` with `origin.branch_id`, or `location_format` with origin coordinates. The destination must use `location_format`. Contact details are not required for estimates.

```json
{
	"origin_format": "branch_format",
	"origin": { "branch_id": "YOUR_BRANCH_ID" },
	"destination_format": "location_format",
	"destination": { "latitude": 29.3759, "longitude": 47.9774 }
}
```

| Endpoint                              | Response fields                                                |
| ------------------------------------- | -------------------------------------------------------------- |
| `POST /v2/deliveries/estimate`        | `delivery_fee`, `estimated_pickup_at`, `estimated_delivery_at` |
| `POST /v2/deliveries/estimate/static` | `deliveryFee`, `durationToPickup`, `totalDeliveryDuration`     |

The first endpoint returns timestamps, which can be `null` when an ETA is unavailable. The static endpoint returns durations in seconds using a fixed pickup allowance. Both calculate the fee using the route and merchant pricing. Estimates do not create a delivery.

## Failed deliveries

Read the delivery's current state before taking further action. Contact Armada to arrange another delivery after a failure.

For status notifications, configure [webhooks](https://docs.armadadelivery.com/v2/webhooks.md).
