# Submit a plan to Coordigator

Submit a plan with crews and jobs, queue its optimisation, and hand the saved plan to a dispatcher. The optimisation runs in the background, so the submitting client can disconnect as soon as the API returns a run ID.

The public API base URL is `https://api.coordigator.io`. For local development, set `API_BASE` to `http://127.0.0.1:3001`. Send JSON bodies with `Content-Type: application/json` and credentials with `Authorization: Bearer <token>`.

| Step | Method and path | Bearer | Success |
|---|---|---|---|
| Prepare | `POST /customers/prepare-plan-import` | API key | `200` returns the ready-to-send `plan` and `solve` bodies, plus access details. |
| Save | `POST /plans` | API key | `201` creates the plan and returns its ID, revisions and expiry. |
| Submit | `POST /plans/{planId}/solves` | Plan edit token | `202` queues the solve and returns its run ID. |
| Retrieve | `GET /plans/{planId}/solves/{runId}` | Plan view or edit token | `200` returns the current status and, on completion, the encrypted result. |

## Example submission

Run the following in Bash with cURL and jq. Set `COORDIGATOR_API_KEY` to the account’s API key (the full value labelled **Client ID**). The example uses one crew and one service visit in Stockholm.

```bash
set -euo pipefail
API_BASE=${API_BASE:-https://api.coordigator.io}
customer="Authorization: Bearer ${COORDIGATOR_API_KEY:?Set your API key}"
json='Content-Type: application/json'

prepared=$(curl --fail-with-body -sS "$API_BASE/customers/prepare-plan-import" \
  -H "$customer" -H "$json" --data-binary @- <<'JSON'
{
  "expiryHours": 24,
  "file": {
    "format": "coordigator-demo-v1",
    "date": "2026-12-01",
    "crews": [{
      "name": "North", "driver": "Alex", "van": "Van 1", "capacity": 4,
      "start": "09:00", "end": "17:00",
      "breaks": [{"earliestStart": "12:00", "latestStart": "13:00", "duration": 30}],
      "origin": "sto-1259515079", "destination": "sto-1259515079"
    }],
    "jobs": [{
      "name": "Service visit", "kind": "Service", "site": "sto-3661237304",
      "open": "09:00", "close": "16:00", "duration": 5
    }]
  }
}
JSON
)

saved=$(jq -c '.plan' <<<"$prepared" | curl --fail-with-body -sS "$API_BASE/plans" \
  -H "$customer" -H "$json" --data-binary @-)
plan_id=$(jq -er '.planId' <<<"$saved")
editor="Authorization: Bearer $(jq -er '.editToken' <<<"$prepared")"

run=$(jq -c '.solve' <<<"$prepared" | curl --fail-with-body -sS \
  "$API_BASE/plans/$plan_id/solves" -H "$editor" -H "$json" --data-binary @-)
run_id=$(jq -er '.runId' <<<"$run")
echo "$run"
```

The crew’s 30-minute break can start between 12:00 and 13:00 Stockholm time. Crew capacity and job load in this file format are single quantities.

All location fields in this import format use Stockholm catalogue IDs. Times use the planning date and the `Europe/Stockholm` time zone. Visit duration is in minutes. For a shipment, send two job rows with `kind` set to `Pickup` and `Delivery`, the same nonempty `pair` ID and matching `load`. A file accepts 1–500 crews and up to 500 logical jobs. Plan expiry is 1, 24 or 48 hours and defaults to 24. [Location catalogue](https://github.com/niklaslagergren/vrp/blob/bb38a34/packages/api-client/src/stockholmAddresses.json)

## After submission

A typical `202 Accepted` response is:

```json
{
  "runId": "7ffb0ac0-249f-4f97-bf00-b3750abc81cd",
  "problemRevision": 1,
  "solutionRevision": 1,
  "status": "queued",
  "createdAt": "2026-12-01T08:00:00.000Z",
  "expiresAt": "2026-12-02T08:00:00.000Z"
}
```

**Handoff to Dispatcher.** Once saved, the plan can be opened with the `dispatcherUrl` returned by preparation. Give the dispatcher the `planKey` separately. The URL grants edit access. Keep the preparation response private.

**Retrieve the result.** Call the status endpoint again while the run is `queued` or `running`. Each poll counts toward the account quota.

```bash
viewer="Authorization: Bearer $(jq -er '.viewToken' <<<"$prepared")"
curl --fail-with-body -sS "$API_BASE/plans/$plan_id/solves/$run_id" -H "$viewer"
```

A `completed` response adds `result`. The shared client decrypts it with `decryptSolveResult(planKey, { planId, runId, problemRevision }, result)`. Applying the result to the saved plan is a separate action. A `failed` response adds `"error":"solve_failed"`. The completed result expires with the plan. [Result decryption](https://github.com/niklaslagergren/vrp/blob/bb38a34/packages/api-client/src/solveCrypto.ts)

**Retry a submission.** Keep the prepared bodies and tokens before saving. If the save response is lost, check `GET /plans/{planId}` with the original edit token. A successful read confirms creation. Retry a lost solve submission with the original `solve` body. Its `idempotencyKey` is the run ID. Changing the body while reusing the key returns `409 idempotency_conflict`. An invalid import returns `400`, an oversized payload `413`, an account run-size violation `422`, and an exhausted quota `429`.

[Implementation and integration contract · bb38a34](https://github.com/niklaslagergren/vrp/blob/bb38a34/services/portal-api/saved-plans.md#background-solves-for-saved-plans)
