# HTTP API

The CLI covers everything below; use the API directly only when you cannot run the CLI. Address and authorization: [access.md](/docs/access.md).

## Endpoints

| Request | What it does |
|---|---|
| `POST /runs` | Starts a run from `{ "spec": <execution-spec>, "secrets": { ... }, "tags": ["release", "v1.4.2"] }` (body up to 2 MB) with an `Idempotency-Key` header. `capacitylab-load compile` produces the spec. `tags` is optional: up to 10 different tags (repeats collapse), each up to 64 letters, digits and `. _ : = / + -`, starting with a letter or digit; case matters. The same key with the same body returns the same run without charging twice. |
| `GET /runs?tag=<t>&limit=<n>` | The key's runs, newest first, as `{ "runs": [...] }`; each `tag` (repeatable) keeps only runs carrying it; `limit` 1-100, default 20. Percent-encode tags in the query: `+` is `%2B` (a bare `+` reads as a space), `=` is `%3D`. |
| `GET /runs/<id>` | `status` (`starting` → `running` → `done`, `failed` or `stopped`), `price`, `tags`, `report_url` and `summary` ([results.md](/docs/results.md#summary-fields)). |
| `POST /runs/<id>/stop` | Stops an active run and its generator; `Ctrl+C` in the CLI sends it. |
| `POST /runs/<id>/baseline` | Pins that finished run as the key's baseline for its target: later runs without `--baseline` compare with it. Returns `target`, `run_id`, `pinned_at`; pinning again replaces it. |
| `GET /reports/<id>` | The HTML report; no key needed. |
| `GET /me` | The key's balance, operator-listed hosts, host proof line, ceilings, pinned `baselines`, test `windows` (with `open_now`), last runs and ledger. |
| `PUT /me/windows/<host>` | Sets the host's test windows: `{ "tz": "Europe/Moscow", "windows": [{ "days": "mon-fri", "from": "01:00", "to": "05:00" }] }` (`tz` optional, default UTC; 1-8 windows). Returns them normalized with `open_now`. |
| `DELETE /me/windows/<host>` | Removes the host's test windows: `{ "host", "removed" }`. |
| `POST /feedback` | `{ "message": "...", "run_id": "..." }` (`run_id` optional, one of this key's runs) → `201` with `id` (`fb_...`), `status` and `status_url`. |
| `GET /feedback/<id>`, `GET /feedback` | One item with its status (`open`, then `answered` or `closed`) and the operator's `reply`; or all of this key's feedback, including the generator's own reports of aborted runs. |

## Refusals

Every refusal is `{"error": {"code": "...", "message": "..."}}` and none is charged. Rely on `code`: `message` is a human-readable explanation in Russian. Do not retry a refusal with another host or a higher rate.

| Status and code | Meaning | What to do |
|---|---|---|
| `401 invalid_key` | the key is missing, wrong or revoked | ask your user |
| `402 insufficient_credit` | not enough credits | tell your user |
| `403 host_not_allowed` | a target host is neither in the key's list nor proved (`host_proof` names the file URL and TXT name, `checked` says what failed); or it is proved but the target is `http://` or the run sets `--insecure`; or it is an internal address | publish the line, use `https` without `--insecure` ([access.md](/docs/access.md#hosts)), then retry |
| `403 outside_test_window` | a target host's test window is closed now, or the run would not fit into it; `next_window_start` (ISO) is the next start that fits, absent when the run fits into no window | wait for it (`--wait-for-window`), shorten the run, or change the windows |
| `400 invalid_window` | the windows or the time zone could not be read | fix the days, times or zone |
| `409 run_in_progress` | the key already has an active run | find it in `GET /me` (`starting` or `running`), wait for it or stop it |
| `409 baseline_not_eligible` | the run cannot be a baseline: it has not finished, is a smoke run, or the generator cut it short | pin a finished full run |
| `409 idempotency_key_reused` | the same `Idempotency-Key` with a different body | use a new one |
| `422 rps_over_limit`, `duration_over_limit`, `vus_over_limit` | above the key's or the service's ceilings | lower the schedule or ask for a higher ceiling |
| `426 cli_outdated` | the CLI is too old for the service | update it ([access.md](/docs/access.md#the-cli)) and run again |
| `400 missing_secret`, `400 invalid_request` | a declared secret was not passed, or the spec (or a tag in `tags`) is invalid | pass the secret / fix the spec or the tag |
| `400 invalid_tag`, `400 invalid_limit` | `GET /runs`: a `tag` breaks the tag rule (or more than 10), or `limit` is not 1-100 | fix the query; percent-encode `+` and `=` |
| `413 body_too_large` | the spec is over 2 MB, usually large data files | shrink the data files |
| `404 run_not_found`, `feedback_not_found`, `baseline_not_found` | no such run, feedback or finished baseline run for this key | check the id |
| `502 launch_failed` | the generator did not start; the whole price is already refunded | retry with a new `Idempotency-Key` |

Every run has `price` (charged when it was accepted) and `refunded` (what came back when it closed): a run pays for its machine's minutes ([access.md](/docs/access.md#ceilings-and-price)). A generator that fails to return a summary leaves the run `failed` with `error: "no_result"`, charged in full; the operator is notified and decides on compensation.
