# API conventions

What every route of `/api/v1` has in common. Agents see the same objects
through MCP tools, which return the same shapes.

## Versions and contract

The API is versioned in its path (`/api/v1`). Its OpenAPI document is the
contract: `python -m scripts.api_types` (in `backend/`) turns it into
`frontend/src/api/generated/schema.ts`, the interface's own types are
checked against that file when it is type-checked, and CI fails when the
file is stale. Fields may be added within a version; removing or
renaming one fails the contract check first.

## Identifiers

Every object has an integer `id`, stable for its whole life and never
reused for another object of the same kind. Objects that belong to a
vault are addressed under it (`/vaults/{vault_id}/...`); an id from
another vault answers `404`, never the other vault's object. Notes and
reports refer to objects as `[[kind:id]]` (`flow:12`, `finding:3`,
`file:7`), and the same pair names an object everywhere.

## Lists

A list is either **complete** or **paged**:

- **Complete lists** answer every item in a stable order, bounded per
  vault: vaults, targets, findings, scans, reports, tags, scope entries,
  engagement decisions (newest first, at most 500).
- **Paged lists** answer `items` and `next_cursor`, and continue when
  you pass that value back as `cursor`; a null `next_cursor` means the
  last page. Treat the cursor as opaque. New arrivals never shift a page
  that continues from a position. Each list also keeps the continuation
  it had before, so older clients work unchanged:

| Route | Continue with |
|---|---|
| Files | `offset` and `limit`, with `total` |
| HTTP history | `before_id` |
| Notes, assets | `next_cursor`, passed back as `cursor` |
| Intruder results | `next_cursor`, passed back as `cursor` (or `after`) |
| Repeater sends | `next_before_id` (also `next_cursor`) |
| Agent activity | `next_before` (also `next_cursor`) |
| WebSocket frames, Bridge messages | `next_after_seq` (also `next_cursor`) |
| Bridge conversations | `next_offset` (also `next_cursor`) |
| Payload items | `start` and `limit`, with `item_count` |

Files, HTTP history and payload items keep their own continuation
(`offset` with `total`, `before_id`, `cursor`). Each `limit` has a
maximum per route; asking for more is refused with `422`. A continuation
that is null or missing means the last page.

- **Filters** are query parameters named after the field (`status`,
  `severity`, `kind`, `tag`, `state`), repeatable where a field can take
  several values, and `search` for words in names or text. **Sorting** is
  `sort` (a field the route lists) and `order` (`asc` or `desc`) where a
  route offers it.

## Request bodies

Every request body is bounded: 32 MiB by default (`API_MAX_BODY_BYTES`),
a vault file's limit (`FILES_MAX_BYTES`, 100 MiB by default) for file
uploads and content replacement, 50 MiB for a HAR import, each with
1 MiB of room for multipart framing. A declared `Content-Length` over the
limit is refused before anything is read; a body sent without one is
counted as it arrives and refused the moment it passes the limit. Either
way the answer is `413 too_large` with `details.limit`. Features keep
their own smaller limits inside (a note body, a payload import).

## Errors

Every error has the same body, whatever its cause:

```json
{"error": {"code": "invalid_value", "message": "scope_rules.in.1: ...", "details": {...}}}
```

| Status | Codes |
|---|---|
| 401 | `unauthenticated`, `invalid_token`, `token_expired`, `token_revoked` (MCP and Bridge) |
| 403 | `permission_denied`, `forbidden_origin`, `engagement_refused`, `egress_refused` |
| 404 | `not_found` |
| 409 | `conflict`, `invalid_state`, `engagement_halted` |
| 413 | `too_large` |
| 422 | `invalid_value`, `out_of_scope`, `signature_invalid` |
| 426 | `unsupported_protocol` |
| 502 | `provider_error` |
| 503 | `unavailable` |

`code` is stable and meant for programs; `message` is for people and may
change. A malformed request (a wrong type, a missing field, a value out
of range) is `422 invalid_value` with every problem under
`details.errors`: `loc` (where, as FastAPI reports it), `field` (the
dotted path in the body or query), `message` and `type`.
