# HTTP API

Base URL: the address in `listen`, e.g. `http://127.0.0.1:8080`. Requests and responses are JSON; timestamps are Unix seconds; run IDs are 32 hex characters.

## Authentication

Send the API token as a bearer token:

```sh
-H "Authorization: Bearer $SHIPYARD_API_TOKEN"
```

Queuing, cancelling and retrying always need it. When `runtime.api_token_env` is set, reads need it too. `/healthz` and `/readyz` are always open.

## Queue a run

```sh
curl -X POST http://127.0.0.1:8080/api/v1/runs \
  -H "Authorization: Bearer $SHIPYARD_API_TOKEN" \
  -H 'Content-Type: application/json' \
  -H 'Idempotency-Key: nightly-check-2026-10-03' \
  -d '{"project_id":"my-app","kind":"pipeline","task":"project-check","input":""}'
```

| Field | Meaning |
|---|---|
| `project_id` | An enabled project. |
| `kind` | `diagnostic`, `pipeline` or `ai_text`. |
| `task` | Pipeline name for `pipeline`; AI role for `ai_text`. |
| `input` | Pipeline input or the prompt. Required for `ai_text`. |

`Idempotency-Key` (8–128 characters) is required. Sending the same key and body again returns the run already queued instead of a duplicate; the same key with a different body is `409`. Answer: `202` with `{"data": run}`; the run carries `policy_version` and `policy_reason` of the project policy that admitted it. A kind the policy does not allow is `403 policy_denied`; a used-up daily budget is `429 budget_exceeded`.

## Runs

| Request | Returns |
|---|---|
| `GET /api/v1/runs?limit=50` | Newest runs first, up to 100 per page, and `next_cursor`. |
| `GET /api/v1/runs?before=<cursor>` | The next page; `next_cursor` is `null` on the last one. |
| `GET /api/v1/runs/{id}` | One run. |
| `POST /api/v1/runs/{id}/cancel` | Cancels a queued run, asks a running one to stop. |
| `POST /api/v1/runs/{id}/retry` | Queues a failed or cancelled run again as a new run. Needs a new `Idempotency-Key`. |
| `PUT /api/v1/runs/{id}/hold` | Places a legal hold: retention keeps the run and its logs. `DELETE` lifts it. Both are audited. |
| `GET /api/v1/runs/{id}/artifacts` | Files the run's steps produced, without content: `changes.patch` and `pull-request.json` of an agent step with `pull_request`. |
| `GET /api/v1/runs/{id}/artifacts/{step}/{name}` | Downloads one artifact. |

A run:

```json
{
  "id": "4bb3a90cb8f0a3bbc59125a2d3a992f8",
  "project_id": "my-app",
  "kind": "pipeline",
  "task": "project-check",
  "state": "succeeded",
  "attempt": 1,
  "created_at": 1791057353,
  "updated_at": 1791057353,
  "finished_at": 1791057353,
  "logs_expired": false,
  "hold": false
}
```

`state` is one of `queued`, `waiting`, `running`, `cancelling`, `succeeded`, `failed`, `cancelled`. A failed run carries `error`; a waiting run carries the limit in `error` and its resume time in `resume_at`.

`GET /api/v1/limits` lists models and agents blocked by a usage limit: `key` (`provider/model` or `claude-code`), `until` and `reason`.

## Deployments

For projects with `deploy.enabled`. Each tagged release has one deployment: staging first, production after approval.

| Request | Result |
|---|---|
| `GET /api/v1/deployments?project_id=<id>` | Newest deployments first, with state, commit `sha` and artifact `digest`. |
| `GET /api/v1/deployments/{id}` | One deployment. |
| `POST /api/v1/deployments/{id}/approve` | Starts production. The body repeats what you approve: `{"sha":"...","digest":"..."}`. Anything else than the deployment's own values is `409 candidate_mismatch`. |
| `POST /api/v1/deployments/{id}/reject` | Ends a deployment that waits for approval. |
| `POST /api/v1/deployments/{id}/rollback` | Deploys the previous production release again. Needs an `Idempotency-Key`. |

## Logs

```sh
curl -H "Authorization: Bearer $SHIPYARD_API_TOKEN" \
  "http://127.0.0.1:8080/api/v1/runs/$ID/logs?offset=0"
```

Returns up to 100 events and `next_offset`; pass it back as `offset` to read on. `attempt=N` reads an earlier attempt. Expired logs answer `410`.

Each event has `time`, `level`, `run_id`, `attempt` and `message`, plus `source` (`worker` or `ship`), `project_id`, a unique `event_id` and, when tracing is on, the run's `trace_id`. Lines written by older versions lack the last four.

To download a whole attempt as JSON Lines, use `GET /api/v1/runs/{id}/logs/export?attempt=N` (same authorization as reading logs). A last line cut short by a crash or a full disk is left out; an attempt with no log yet is an empty file.

```json
{"data": [{"time": "2026-10-03T18:55:53Z", "level": "info", "run_id": "4bb3…", "attempt": 1, "message": "run started"}], "next_offset": 112}
```

## History

`GET /api/v1/runs/{id}/events` lists what happened to a run, oldest first: `queued`, `cancel_requested`, `deferred`, `succeeded`, `failed`, `cancelled`, `recovered_<state>`, `hold_placed`, `hold_lifted`, each with `at` (Unix seconds). Retention removes old entries.

```json
{"data": [{"action": "queued", "at": 1791057300}, {"action": "succeeded", "at": 1791057353}]}
```

## Projects and pipelines

`GET /api/v1/projects` and `GET /api/v1/pipelines` return the configured projects and pipelines; prompts are never returned. `read_only` in the projects answer is `true` when the configuration came from a URL.

`POST /api/v1/projects` adds a project and writes it to the configuration file:

```sh
curl -X POST http://127.0.0.1:8080/api/v1/projects \
  -H "Authorization: Bearer $SHIPYARD_API_TOKEN" \
  -d '{"id":"firmware","repository":"you/firmware","enabled":true,"issue_label":"agent-ready",
       "release_interval":"48h","runs_on":["arch=arm64"],
       "content":{"enabled":false,"interval":"168h","min_score":70}}'
```

`409 project_exists` for a duplicate ID, `409 config_read_only` for a URL-loaded configuration, `400 invalid_project` with a `message` naming the problem.

## Token usage

All usage routes take `from` and `to` (Unix seconds, `to` exclusive; default the last 7 days). Costs are estimates from the configured model prices; `cost_usd` is `null` when any call was unpriced or unreported (unknown, not zero). Workstation sessions carry no cost estimate. Only counts and labels are returned, never prompts, replies or file text.

| Request | Returns |
|---|---|
| `GET /api/v1/projects/{id}/usage?group=` | Tokens of every kind and cost per `source` (default), `model`, `category`, `session` (run or workstation session) or `ship`, most tokens first, with the `total`. |
| `GET /api/v1/projects/{id}/usage/sessions` | Runs and workstation sessions, most tokens first, with average and peak context; `limit` (1–200, default 50), `offset`, `count` and `next_offset`. |
| `GET /api/v1/projects/{id}/usage/findings` | The project's current usage findings: rule code, value against threshold, tokens, and the session, directory or tool, most tokens first. |
| `GET /api/v1/projects/{id}/usage/instructions` | Instruction files of the project's repositories and the user-scope files of the same ships, with `fixed_costs` per ship and directory. |
| `GET /api/v1/ships/{id}/usage` | The runs the ship executed, newest first, with tokens and cost per run and the period's `total`; paged like sessions. |

```sh
curl -H "Authorization: Bearer $SHIPYARD_API_TOKEN" \
  "http://127.0.0.1:8080/api/v1/projects/firmware/usage?group=model"
```

`404 project_not_found` for a project not in the configuration, `404 ship_not_found`, `400 invalid_range` (`from` not below `to`, or not a number), `400 invalid_group`, `400 invalid_page`.

## Phones

| Request | Returns |
|---|---|
| `GET /api/v1/info` | `name`, `version`, `yard_id` and whether pairing is on. |
| `GET /api/v1/devices` | Paired phones; keys are never returned. |
| `POST /api/v1/devices` | Pairs a phone: body `{"name": "Pixel 9"}`; returns the `bosun://pair` link and an SVG QR code once. |
| `POST /api/v1/devices/{id}/revoke` | Revokes a phone. |

The app itself sends end-to-end encrypted requests to `POST /bosun/v1/yards/{yard}/rpc`, directly or through a Lighthouse.

## Ships

| Request | Returns |
|---|---|
| `GET /api/v1/ships` | Ships with labels, platform, `online` and `revoked`; `enabled` tells whether the fleet endpoint is on. |
| `POST /api/v1/ships/tokens` | A join token and the `ship join` command. Body: `{"name": "garage-pi", "labels": ["arch=arm64"]}`; optional `uses` (1–1000, default 1) lets that many ships join with it, `ttl_minutes` (5–1440, default 60) sets its lifetime. |
| `POST /api/v1/ships/{id}/revoke` | Revokes a ship. |
| `GET /api/v1/sessions` | Active infrastructure token sessions with principal name, kind and task count; `enabled` tells whether `infra.enabled` is on. |
| `POST /api/v1/sessions/{id}/end` | Ends a session and revokes its token. |
| `GET /api/v1/ssh/sessions` | Brokered SSH sessions of the yard with ship and requester names; `enabled` tells whether `infra.ssh.enabled` is on. |
| `POST /api/v1/ssh/sessions/{id}/end` | Ends an SSH session and cuts its connection; `409 ssh_state_conflict` once it has ended. |
| `GET /api/v1/ssh/sessions/{id}/recording` | A recorded session's asciicast v2 file; always needs the API token. |

Ships themselves talk to Shipyard over gRPC with mutual TLS — see [Ships and docks](/ships/).

## Errors

Errors have a stable code and the request ID, which every response also carries in the `X-Request-Id` header. Quote it when you report a problem:

```json
{"error": {"code": "run_not_found"}, "request_id": "4f0c1e2d3b4a59687f8e9d0c1b2a3948"}
```

| Status | Codes |
|---|---|
| 400 | `invalid_input`, `invalid_project`, `invalid_name`, `invalid_label`, `invalid_cursor`, `invalid_attempt`, `invalid_offset`, `project_unavailable`, `pipeline_unavailable`, `model_or_input_unavailable`, `unsupported_kind`, `idempotency_key_required` |
| 401 | `unauthenticated` |
| 403 | `policy_denied` |
| 404 | `run_not_found`, `ship_not_found` |
| 409 | `idempotency_conflict`, `run_state_conflict`, `project_exists`, `config_read_only`, `fleet_disabled` |
| 410 | `logs_expired` |
| 429 | `budget_exceeded` |
| 500 | `storage_failed`, `enqueue_failed`, `logs_unavailable` |
| 503 | `storage_unavailable` |
| 507 | `log_budget_exceeded` |

Metrics and the Alertmanager webhook are described in [Monitoring](/monitoring/).
