Docs menu

Distribute by area

Send one set of stops and get back one optimized route per driver, each driver taking the stops in the areas they serve. Darb places every stop in its area from its own coordinates; a stop no chosen driver serves is held with its reason, never dropped. The journey is list areas, list drivers, create a route group, poll it, then read each driver's route and the held stops.

Before you start

  • Mint a test key in Settings → API keys.
  • Give your drivers service areas in the app's Drivers page. Areas are read from the API, but a driver's areas are set in the app.
  • Set your base URL and key as shell variables so the examples below run as-is. Every request carries Authorization: Bearer dk_test_....
bash
# Your Darb API base URL (the hosted portal wires this from PUBLIC_API_URL)
export DARB_API_BASE="https://<your-darb-api-host>"
# A test key minted in Settings -> API keys
export DARB_KEY="dk_test_..."
python
import os

import httpx

DARB_API_BASE = os.environ["DARB_API_BASE"]
HEADERS = {"Authorization": f"Bearer {os.environ['DARB_KEY']}"}

1. List your areas

/api/v1/areas (GET) returns the areas of your account's country, grouped by governorate, each with its English and Arabic names and display labels. An area's code, such as OM-0105, is its only identifier on this API, and it is stable across data updates. If your country has no area data, the answer is still 200, with available: false and an unavailable_reason.

bash
curl -sS "$DARB_API_BASE/api/v1/areas" \
  -H "Authorization: Bearer $DARB_KEY"
python
areas = httpx.get(f"{DARB_API_BASE}/api/v1/areas", headers=HEADERS).json()
if not areas["available"]:
    raise SystemExit(f"no areas: {areas['unavailable_reason']}")
credit = areas["attribution"]["text"]  # show this wherever you show an area name
json
{
  "country_code": "OM",
  "available": true,
  "unavailable_reason": null,
  "source_version": "2026-10-25T09:00:00Z",
  "attribution": {
    "text": "© OpenStreetMap contributors",
    "licence": "ODbL-1.0",
    "licence_url": "https://opendatacommons.org/licenses/odbl/1-0/",
    "copyright_url": "https://www.openstreetmap.org/copyright",
    "data_url": "https://<your-darb-api-host>/public/areas/OM"
  },
  "level_labels": {
    "area": { "en_one": "wilayat", "en_other": "wilayats", "...": "..." },
    "group": { "en_one": "governorate", "en_other": "governorates", "...": "..." }
  },
  "governorates": [
    {
      "code": "OM-MA",
      "name_en": "Muscat",
      "name_ar": "مسقط",
      "aliases_en": ["Masqat", "Muscat Governorate"],
      "aliases_ar": ["محافظة مسقط"],
      "areas": [
        {
          "code": "OM-0105",
          "level": 2,
          "name_en": "Seeb",
          "name_ar": "السيب",
          "aliases_en": ["As Sib", "Al Seeb"],
          "aliases_ar": [],
          "label_en": "Seeb · Muscat",
          "label_ar": "السيب · مسقط"
        }
      ]
    }
  ]
}

2. List your drivers

/api/v1/drivers (GET) returns every driver in your registry, oldest first, as a bare array: id, name, is_active and area_codes. Inactive drivers are listed too, and are never given stops. A driver's phone number is never returned. GET /api/v1/drivers/{id} reads one driver.

bash
curl -sS "$DARB_API_BASE/api/v1/drivers" \
  -H "Authorization: Bearer $DARB_KEY"
python
drivers = httpx.get(f"{DARB_API_BASE}/api/v1/drivers", headers=HEADERS).json()
working = [d["id"] for d in drivers if d["is_active"] and d["area_codes"]]
json
[
  {
    "id": "5d0e8a4c-7b1f-4e2a-9c3d-1f2e3d4c5b6a",
    "name": "Salim",
    "is_active": true,
    "area_codes": ["OM-0104", "OM-0105"]
  },
  {
    "id": "9a8b7c6d-5e4f-4a3b-8c2d-1e0f9a8b7c6d",
    "name": "Hamad",
    "is_active": false,
    "area_codes": ["OM-0102"]
  }
]

3. Create a route group

POST /api/v1/route-groups takes the same start, stops (2–100), end, round_trip and traffic fields as a route, plus an optional driver_ids: the drivers working today, each id once, at most 50. Omit driver_ids and the run uses every active driver who serves at least one area. When you send it, it names at least one driver: an empty list is refused with 422 validation_error, so leave the field out instead. A chosen driver who is inactive or serves no area is left out and listed in drivers_without_route; the request is not refused for it. No field takes an area: the server places each stop itself.

Send an Idempotency-Key header so a retry within 48 hours of the first request never creates a second run. A replay with the same key and body creates nothing and costs nothing: it answers 202 with the same run as it stands now. After 48 hours the key may expire, and the same key then creates a new run. The same key with a different body answers 409 idempotency_key_conflict, and a refused request does not use up its key.

bash
curl -sS -X POST "$DARB_API_BASE/api/v1/route-groups" \
  -H "Authorization: Bearer $DARB_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: group-2026-11-02-0001" \
  -d '{
    "start": { "lat": 23.58, "lng": 58.38, "label": "Depot" },
    "stops": [
      { "lat": 23.67, "lng": 58.19, "label": "Seeb customer" },
      { "lat": 23.59, "lng": 58.42, "label": "Bawshar customer" },
      { "lat": 22.93, "lng": 57.53, "label": "Nizwa customer" }
    ],
    "driver_ids": ["5d0e8a4c-7b1f-4e2a-9c3d-1f2e3d4c5b6a"]
  }'
python
body = {
    "start": {"lat": 23.58, "lng": 58.38, "label": "Depot"},
    "stops": [
        {"lat": 23.67, "lng": 58.19, "label": "Seeb customer"},
        {"lat": 23.59, "lng": 58.42, "label": "Bawshar customer"},
        {"lat": 22.93, "lng": 57.53, "label": "Nizwa customer"},
    ],
}
# Optional. Omit it to use every active driver who serves an area.
# Never send an empty list: "driver_ids": [] is a 422 validation_error.
if working:
    body["driver_ids"] = working

resp = httpx.post(
    f"{DARB_API_BASE}/api/v1/route-groups",
    headers={**HEADERS, "Idempotency-Key": "group-2026-11-02-0001"},
    json=body,
)
resp.raise_for_status()
group_id = resp.json()["id"]

The 202 response is the poll seed: one unassigned route (driver: null) carrying every routed stop, which is then split into one route per driver, and the held stops already listed:

json
{
  "id": "0c4a2f6e-8d1b-4b7a-a3e5-6f9d2c8b1a07",
  "status": "optimizing",
  "created_at": "2026-11-02T08:00:00Z",
  "members": [
    {
      "id": "7e3f1a9b-2c4d-4e6f-8a0b-1c2d3e4f5a6b",
      "status": "optimizing",
      "stops": [
        { "seq_input": 0, "seq_optimized": null, "eta": null, "label": "Depot", "lat": 23.58, "lng": 58.38, "area_code": "OM-0104" },
        { "seq_input": 1, "seq_optimized": null, "eta": null, "label": "Seeb customer", "lat": 23.67, "lng": 58.19, "area_code": "OM-0105" },
        { "seq_input": 2, "seq_optimized": null, "eta": null, "label": "Bawshar customer", "lat": 23.59, "lng": 58.42, "area_code": "OM-0104" }
      ],
      "total_distance_m": null,
      "total_duration_s": null,
      "polyline": null,
      "handoff_url": null,
      "driver": null,
      "area_codes": ["OM-0104", "OM-0105"]
    }
  ],
  "held_stops": [
    {
      "seq_input": 2,
      "label": "Nizwa customer",
      "lat": 22.93,
      "lng": 57.53,
      "reason": "no_driver_today",
      "area": { "code": "OM-0501", "name_en": "Nizwa", "name_ar": "نزوى" }
    }
  ],
  "drivers_without_route": []
}

4. Poll the run

Poll GET /api/v1/route-groups/{id} until status is optimized and no member is optimizing. The status is optimized when the run has no route, optimizing while any route is optimizing, failed when every route failed, and otherwise optimized, each route keeping its own status.

The poll shows the run as it stands now, including what a dispatcher does in the app afterwards. A group can return to optimizing when a dispatcher sends a held stop to a driver: that driver's route is optimized again with the stop in it. Keep polling until no route is optimizing.

bash
# GROUP_ID is the id from the create response (group_id in the Python tab).
# Poll until "status" is "optimized" and no member is "optimizing".
curl -sS "$DARB_API_BASE/api/v1/route-groups/$GROUP_ID" \
  -H "Authorization: Bearer $DARB_KEY"
python
import time

while True:
    group = httpx.get(
        f"{DARB_API_BASE}/api/v1/route-groups/{group_id}", headers=HEADERS
    ).json()
    busy = any(m["status"] == "optimizing" for m in group["members"])
    if group["status"] != "optimizing" and not busy:
        break
    time.sleep(2)

5. Read each driver's route and the held stops

json
{
  "id": "0c4a2f6e-8d1b-4b7a-a3e5-6f9d2c8b1a07",
  "status": "optimized",
  "created_at": "2026-11-02T08:00:00Z",
  "members": [
    {
      "id": "3b9d7f1e-6a2c-4d8e-b0f4-2a6c8e0d4f1b",
      "status": "optimized",
      "stops": [
        { "seq_input": 0, "seq_optimized": 0, "eta": "2026-11-02T08:00:00Z", "label": "Depot", "lat": 23.58, "lng": 58.38, "area_code": "OM-0104" },
        { "seq_input": 1, "seq_optimized": 2, "eta": "2026-11-02T08:41:00Z", "label": "Seeb customer", "lat": 23.67, "lng": 58.19, "area_code": "OM-0105" },
        { "seq_input": 2, "seq_optimized": 1, "eta": "2026-11-02T08:09:00Z", "label": "Bawshar customer", "lat": 23.59, "lng": 58.42, "area_code": "OM-0104" }
      ],
      "total_distance_m": 61200,
      "total_duration_s": 4380,
      "polyline": "yz{|@_ulpE...",
      "handoff_url": "https://<your-darb-app-host>/d/8f3c1a2b",
      "driver": { "id": "5d0e8a4c-7b1f-4e2a-9c3d-1f2e3d4c5b6a", "name": "Salim" },
      "area_codes": ["OM-0104", "OM-0105"]
    }
  ],
  "held_stops": [
    {
      "seq_input": 2,
      "label": "Nizwa customer",
      "lat": 22.93,
      "lng": 57.53,
      "reason": "no_driver_today",
      "area": { "code": "OM-0501", "name_en": "Nizwa", "name_ar": "نزوى" }
    }
  ],
  "drivers_without_route": []
}

Members: one route per driver

Each entry of members is a route with the route fields you know from Optimize a route, plus its driver (id and name), its area_codes (the areas its delivery stops lie in, ascending; the start and end are not counted) and each stop's area_code. A member's stops are numbered per route with the start at seq_input 0, so match them to your request by label and coordinates.

Held stops

held_stops lists the stops still waiting to be routed, ascending by seq_input, each with its label, coordinates, the area it lies in (code, name_en, name_ar, or null) and a reason:

  • no_area: the stop lies in no area.
  • no_driver_today: none of the chosen drivers serves its area.
  • late_arrival: the customer's location arrived after the routes were made (a run built by a campaign).

For a run you created with POST /route-groups, a held stop's seq_input is the 0-based index into your request's stops array, with the start and end not counted. That is unlike a route stop's seq_input, where the start is 0. In the example above the held Nizwa stop is stops[2] of the request.

A run built by a campaign has no request stops array. There, seq_input is only a stable ordinal within the run: it is not an index into the campaign's contacts, and a late_arrival stop is numbered after every other stop. Correlate a campaign's held stops by label (the contact's name, or its number when it has none) and coordinates.

Held stops are acted on in the app, not through the API: a dispatcher can send a held stop to a driver, dismiss it, or restore it, and the next poll shows the result.

Drivers without a route

drivers_without_route names the chosen drivers who have no route, idle drivers first, then the drivers left out, each with a reason:

  • idle: the driver has nothing to deliver in the run right now.
  • inactive: the driver is deactivated and was left out.
  • no_areas: the driver serves no area and was left out.
python
for member in group["members"]:
    driver = member["driver"]["name"] if member["driver"] else "(unassigned)"
    print(driver, member["area_codes"], member["handoff_url"])

for held in group["held_stops"]:
    # seq_input is the index into YOUR request's stops array.
    print("held:", held["seq_input"], held["label"], held["reason"])

for entry in group["drivers_without_route"]:
    print("no route:", entry["driver"]["name"], entry["reason"])

Refusals

A run that cannot be made is refused before anything is charged or saved, with one of five codes in the usual error envelope. Darb never trims your drivers for you: when there are too many, pass driver_ids to choose fewer. One run can split a connected group of areas between at most 8 drivers. Two areas are connected when one chosen driver serves both, and only drivers with stops in the run are counted.

StatuscodeWhat to do
409 component_over_cap More than 8 of the chosen drivers serve one connected group of areas. details carries max and the over-cap groups ({area_codes, driver_ids, driver_count}). Pass driver_ids to choose fewer.
402 driver_limit_exceeded More drivers than your plan allows. details carries limit and requested. Pass driver_ids to choose fewer, or upgrade the plan.
409 no_eligible_drivers driver_ids was omitted and no active driver serves an area yet. Give a driver a service area in the app first.
422 invalid_drivers driver_ids names ids that are not drivers of this account (unknown and foreign ids get the same answer). The ids are under details.driver_ids.
409 areas_unavailable Areas are not available for your account's country yet. details carries reason and country_code.

The usual refusals apply too: 422 validation_error for a malformed body (for example a repeated id in driver_ids), and 402 quota_exceeded when your routes quota is used up, when more stops would be held than your plan's per-route stop limit, or when the stops routed in the run exceed that limit — always on a test key, and on a live key when your account's overage limit is 0. On a live key with an overage limit above 0, the routed stops above the limit are charged in credits instead, and the run is refused with 402 credits_exhausted or 402 overage_cap_reached when your credit balance or this period's overage limit cannot cover them.

Creating runs has its own limit per account, on top of each key's: at most 30 runs per minute with live keys, and 30 with test keys, however many keys you use. Past that the request answers 429 rate_limited with Retry-After, before anything is charged or saved. Polling a run is not affected.

Cost

A run costs one routes unit, however many drivers it is split between. On a live key, the routed stops above your plan's per-route stop limit are also charged in credits, as on a single route (see Refusals). A run whose every stop is held costs nothing: it is saved with no route and reads optimized at once.

Test mode

A dk_test_ key creates a test group: it is split on a simulated travel-time matrix, draws from the test caps (100 routes, 200 WhatsApp, 100 SMS per month) and is visible only to test keys. A live key polling a test group, or the reverse, gets 404 not_found. Both modes read the same driver registry.

Next: run a campaign that distributes the collected locations by area, or read the conventions shared by every endpoint.