# Base's API for its native app, v1

What Base's Mac and iPhone app may rely on. Everything here is pinned by
tests in `internal/webtest` (`native_test.go`, `api_test.go`,
`api_team_test.go`, `ask_native_contract_test.go`,
`ask_oficina_native_test.go`). The Ask routes are the same ones the web's
`ask.js` uses, so a change that breaks this document breaks the web too.

## Signing in

- **`POST /native/oficina-id`** is connect's native exchange
  (`connect.NativeExchange`; Home spec 2026-11-14-oficina-id-native.md §7).
  The kit sends `{assertion, verifier}` as JSON, with no `Origin`, no
  `Sec-Fetch-Site` and no cookie.
  - `200 {"state":"ok","name":"__Host-rastrillo_session","value":…,"expires_at":<unix>}`:
    the session, seven days from now, absolute.
  - `403 {"state":"unknown"}`: no member here is linked to this Oficina ID.
    Base admits only people who have signed in to Base on the web with
    Oficina ID once (slice 1).
  - `400 invalid`, `401 failed` (get a fresh assertion), `503 unavailable`,
    as connect documents. Base never answers `409 choose`.
- **The session** is the cookie `name=value`. Every other request sends it
  with `Origin: <Base's origin>`; a POST without that `Origin` is refused
  (403, Base's CSRF check).
- **`POST /signout`** ends the session it carries, and nothing else:
  `204` to a JSON request (`Content-Type` or `Accept` of
  `application/json`), `303 /signed-out` to a form.
- **Refusals.** Anything under `/api/`, and any request whose `Accept`
  names `application/json` or `text/event-stream`, hears
  `401 {"code":"unauthenticated"}` (no-store, never a redirect) when it has
  no session, or a session that names nobody who is a member now: unlinked,
  removed, or a native session more than seven days old. Mint a new session
  and try once more.

## Reads

### `GET /api/v1/me`

```json
{
  "member": {"id": "…", "name": "Ana Ruiz", "email": "ana@acme.test", "initials": "AR", "owner": false},
  "base": {"name": "Acme", "slug": "acme", "origin": "https://base.acme.example", "timezone": "Europe/Dublin"},
  "appearance": "system",
  "equipo_origin": "https://equipo.acme.example"
}
```

- `member.name` is the member's name, or their address's local part,
  capitalised. `member.id` is Base's member id, never the address.
- `base.timezone` is an IANA name.
- `appearance` is `system`, `light` or `dark`. Change it with the web's
  `POST /settings/appearance`, form field `theme` (`system`, `light` or
  `dark`), which answers `303 /settings`.
- `equipo_origin` is `null` when Ask Base is off. The app asks Oficina ID
  for an assertion addressed to it (Ask set-up, below).

### `GET /api/v1/home`

```json
{
  "greeting": "Good morning, Ana.",
  "lede": "One step until Acme's Base is ready.",
  "steps": [{"label": "Signed in", "done": true}, {"label": "Invite your team", "done": false}, {"label": "Install your first app", "done": true}],
  "apps": [
    {"id": "calls", "kind": "oficina", "name": "Calls", "url": "https://calls.oficina.works/", "icon_url": "/static/oficina/calls.<hash>.svg", "bundle_id": "works.oficina.calls", "scheme": "oficina-calls"},
    {"id": "docs", "kind": "oficina", "name": "Docs", "url": "https://docs.oficina.works/", "icon_url": "/static/oficina/docs.<hash>.svg", "bundle_id": "works.oficina.docs"},
    {"id": "scratchpad", "kind": "installed", "name": "Scratchpad", "url": "https://acme-scratchpad.example/", "tint": "rose", "icon_svg": "<svg …>"},
    {"id": "store", "kind": "own", "name": "Store", "url": "https://base.acme.example/store", "tint": "teal", "icon_svg": "<svg …>"}
  ]
}
```

- `apps` are in Home's order: the team's Oficina apps, the installed apps,
  then Base's own pages (`store`, `people`, `settings`).
- An Oficina app has `icon_url`, a path on Base's origin (content-hashed,
  immutable), its `bundle_id` (`works.oficina.<id>`), and a `scheme` only
  where its native app opens by one today (Calls, Equipo). The others open
  on the web until their apps take the kit's `…://open`.
- An installed app or own page has `tint` (a catalogue tint name: `amber`,
  `teal`, `violet`, `rose`, `mist`, …) and `icon_svg`, line art drawn in
  `currentColor`. Own pages' `url`s are Base's web pages.
- Optional keys are absent, never `null` or `""`.

## Store and People

What the Store and People pages show, from the same builders, behind the
same member gate: any member's session, the native one or the web's
cookie. Every answer is JSON and `no-store`. A refusal is
`{"code": "…"}`. Without a session, or for someone who is no longer a
member, every route answers `401 {"code":"unauthenticated"}`, as above.
An app that hears `404` from a read is talking to an older Base, and opens
the web page instead.

### `GET /api/v1/store`

```json
{
  "owner": true,
  "rows": [
    {"id": "welcome", "name": "Welcome", "summary": "…", "tint": "amber", "icon_svg": "<svg …>", "state": "installed", "error": ""},
    {"id": "scratchpad", "name": "Scratchpad", "summary": "…", "tint": "rose", "icon_svg": "<svg …>", "state": "installing", "error": ""},
    {"id": "contra", "name": "Contra", "summary": "…", "tint": "teal", "icon_svg": "<svg …>", "state": "failed", "error": "The app did not start answering"}
  ]
}
```

- `rows` is the whole catalogue, in its order. `state` is `""` (not
  installed), `installing`, `installed` or `failed`. `error` is a failed
  install's reason, and may be `""` (show "The install didn't finish.").
- `owner` is whether this member may install and uninstall. Every member
  sees the same rows.
- `icon_svg` is line art drawn in `currentColor` on `tint`, as in
  `/api/v1/home`.
- `503 {"code":"unavailable"}`: Base couldn't read its installs.

### `GET /api/v1/people`

```json
{
  "owner": true,
  "people": [
    {"id": "…", "initial": "O", "name": "", "email": "owner@acme.test", "role": "owner"},
    {"id": "…", "initial": "A", "name": "Ana Ruiz", "email": "ana@acme.test", "role": "member"},
    {"id": "…", "initial": "B", "name": "", "email": "bob@acme.test", "role": "invited"}
  ]
}
```

- `people` is everyone here, in the order they were invited, as the page
  lists them.
- `role` is `owner`, `member`, or `invited` (hasn't signed in yet).
- `name` is `""` for someone who has given none: show `email` then.
  `initial` is the first letter of the name, or else of the address.
- `owner` is whether this member may invite.
- `503 {"code":"unavailable"}`: Base couldn't read its members.
- Nothing removes a member: `DELETE /api/v1/people/{id}` isn't a route.

### The writes

The owner's alone. They are POSTs, inside Base's CSRF check, which the
kit's `Origin` passes. Install and uninstall take no body (one is
ignored); an invite takes JSON.

| Route | Answers |
|---|---|
| `POST /api/v1/store/{app}/install` | `202 {"store": …}` with the app installing: poll `GET /api/v1/store` every 2 s while any row is. An app already installing answers `202`, and one already installed `200`, and neither starts anything. A failed row may be installed again (the page's "Try again"). If the install couldn't be started, the store is read again: a row that now shows installing (another request started it) answers `202` as it stands; anything else is `503 unavailable`, and nothing was started. A row that still reads failed then shows an earlier failure, not this attempt. |
| `POST /api/v1/store/{app}/uninstall` | `200 {"store": …}`, also when it was uninstalled already. `409 {"code":"busy"}` while it is still installing ("This app is still installing. Try again in a moment."). `503 unavailable`. |
| `POST /api/v1/people` | Body `{"email": "…"}`, trimmed and lowercased. `200 {"person": …}`, a person as in `GET /api/v1/people`: invited now, or already here (nothing is sent then). `422 {"code":"bad_email"}`: not one plain address. `502 {"code":"mail_failed"}`: they are invited but the email wasn't delivered; invite them again to send it. `415 {"code":"json_only"}` for a body that isn't `application/json`, `413 {"code":"too_large"}` over 4 KiB, `400 {"code":"bad_request"}` for JSON that doesn't parse. `503 unavailable`. |

Every write also answers `403 {"code":"owner_only"}` to a member who isn't
the owner, before anything else, and the Store's `404
{"code":"unknown_app"}` for an app the catalogue doesn't have. A POST with
no `Origin` (or another site's) is `403` in plain text, from the CSRF
check.

## Ask Base

No Ask route answers `401` while Base's session is good: Equipo refusing
Ask's key is `409 {"state":"reconnect"}` (or `state` `reconnect` in a read).
A `401` is only ever Base's own `{"code":"unauthenticated"}`.

The web's own routes. Writes are **form-encoded**
(`application/x-www-form-urlencoded`), not JSON, and must send
`Accept: application/json` to get JSON back (without it they redirect, as
for a form). Every answer is `no-store`.

### What the app can draw

`GET /ask/state` and `GET /ask/events` may say what this build of the app
draws: a comma-separated list, as the header `Base-Client-Capabilities`
or as the query parameter `client_caps` (the kit's `NativeHTTP` sets no
header of its own, so the app sends the parameter). Base keeps the newest
list per app session, from these two reads only, and tells the Assistant
a feature is drawn only while every app the member used in the last 30
days last said so. An app session Base has not yet heard say it (a live
session that hasn't read `/ask/state` since, or whose read Base couldn't
record) counts as drawing nothing. An app that says nothing keeps what it
gets today: its reads carry each message's lookups folded into its
`text`, with the answer's own links and markup escaped.

| value | what the app draws |
|---|---|
| `lookup_answers` | A message's `lookups` (below) |

### The states

`state` is one of:

| state | meaning |
|---|---|
| `connect` | Ask isn't connected for this person (or a code ran out). |
| `pending` | A sign-in code waits for approval in Equipo (web code flow). |
| `ready` | Connected; the conversation shows. |
| `norunner`, `oldrunner` | Connected, but the person has no runner that can answer (none, or too old). The conversation shows, with a note. |
| `reconnect` | Equipo refused the key, or it expired. |
| `unavailable` | Equipo didn't answer. |
| `not_linked` | (Only from `POST /api/v1/ask/oficina`.) Equipo has no member linked to this Oficina ID. |

### `GET /ask/state`

```json
{"state": "ready", "messages": [Message…], "jobs": [Job…], "cards": [Card…], "turn": Turn|null,
 "status": "Thinking…", "cursor": 42, "renew_after": "2026-11-28T10:00:00Z", "equipo_origin": "https://equipo.acme.example",
 "disconnected": false, "can_attach": true}
```

- `messages`, `jobs` and `cards` are never `null`. Outside `ready`,
  `norunner` and `oldrunner` they are empty and `cursor` is `0`.
- `turn` is the newest turn the person sees running (never a quiet
  `team_update` one), or `null`. `status` is the line under the
  conversation while it runs: `"Thinking…"`, `"Ask Base can't answer right
  now. It will as soon as it can."` (turn `waiting`), or `""`. With no
  turn running, it says why the turn of the person's newest message
  failed for its files (Turn `reason` `attachment_unavailable`,
  `runner_storage_full` or `runner_too_old`), as a live `turn` event with
  that reason should too.
- `cursor` is where `/ask/events` starts.
- `renew_after` (RFC 3339, UTC) is set when Ask was connected with Oficina
  ID: from then on, the app sets Ask up again with a fresh assertion
  (below), silently. `""` for a key from the web's code flow, or none.
- `equipo_origin` is the Equipo instance Ask relays to: the audience of the
  assertion for Ask set-up.
- `disconnected` is `true` once the member has disconnected Ask, on any
  device or the web, until a connection they make themselves is stored.
  While it is, no client sets Ask up by itself; the app offers Connect
  instead. `state` is then `connect`, as after any Disconnect, or `pending`
  while a code from the web's Connect waits for approval (still `true`).
- `can_attach` is `true` only in `ready`, when Equipo says the person's
  runner can take files. Offer attaching only then; an older Equipo never
  says so.

**Message** `{"id", "role": "person"|"assistant", "text", "turn_id", "created_at", "attachments"?, "lookups"?}`.
An assistant's `text` may carry the little Markdown `ask.js` draws (lists,
links to http(s) only, bold and italics). `attachments` is there only on a
person's message with files, in the order they were attached; its `text`
may then be `""`. Until the app draws files, show their names as a line
under the text, so a bubble is never empty.

`lookups` is there only on an assistant's message that looked
something up, and only for an app that says it draws them (above), at
most 2, in the order they were made.

**Lookup** `{"answer", "sources": [{"url", "title"}]}`. Draw each after
the message's own words: the answer in the little Markdown above but with
**no links at all** (a Markdown link shows as its words, a bare URL as
text), then a quiet "Sources" line with each source's `title` as a link
(an `https` address, opening in the browser) and its host after it, without
`www.`. An empty `title` shows the host. The answer is at most 1,200
characters, a title at most 120, and there are at most 5 sources.

**Attachment** `{"id", "kind": "image"|"file", "name", "media_type", "size", "width", "height"}`.
`size` is in bytes; `width` and `height` are an image's upright size, `0`
for a file. Treat any other `kind` as `file`. Read it from Base, never from
Equipo: `GET /ask/attachments/{id}`, and an image's thumbnail (at most
1024 px) at `/ask/attachments/{id}/preview` (see Files).

**Turn** `{"id", "kind", "state": "queued"|"running"|"waiting"|"replied"|"failed"|"cancelled", "reason"}`.

**Card** `{"id", "revision", "kind", "state", "question", "options": [{"value","label"}], "links": [{"label","url","rel"?}], "result", "job"?, "detail"?, "started_at"?, "carry_on"?, "turn_id"?}`.
- `kind` is `question`, `confirm`, `outcome`, `connect` or `job`; a card is
  `open` while it waits for an answer. Every link is absolute http(s).
- `rel` is `equipo` (a job's page in Equipo) or `oficina_connect` (Base's
  own `/ask/oficina/connect`, a browser flow).
- A job card has no options; its `state` is `working`, `asking`, `ready`,
  `done`, `paused` or `stopped`; `result` is its short status, `detail` one sentence
  more and `started_at` when it began. A `paused` job carries on by itself
  (draw it with the clock mark); its `result` and `detail` are Equipo's,
  already in the team's time zone ("Paused until 21:00"): show them as
  given. `carry_on: true` (on a `paused` or `stopped` job card only, else
  absent) offers "Carry on now" (`POST /ask/cards/{id}/carry_on`). On a
  `paused` job it has the line "Uses your other plan, at no extra cost."
  under it; on a `stopped` one (a job held while building was turned off
  for the person, now on again) it has no line.
- `job` names the job card a card belongs to; `turn_id`, on a connect card
  from the stream, the turn that raised it.

**Job** is a job Card plus `"cards": [Card…]`, the cards that belong to it,
open and ended.

### `GET /ask/events?after=<cursor>`

Server-Sent Events, `text/event-stream`. On a reconnect, `Last-Event-ID`
wins over `after`. The stream opens with `retry: 3000`, then:

```
id: 43
event: message
data: {"type":"message","data":Message}
```

| event | data |
|---|---|
| `message` | Message |
| `turn` | Turn |
| `card` | Card (a job card included: `kind` `job`) |
| `delta` | `{"turn_id","offset","text"}`: a few of the reply's words; `offset` counts UTF-8 bytes |
| `activity` | `{"turn_id","label"}`: what the Assistant is doing |
| `glance` | `{"turn_id","text"}`: one line before the first words. **No `id` line**: it doesn't move the cursor. |
| `state` | `{"state"}`: the stream can't start (not `ready`); it ends after this one event. |

- `: ping` comments keep it alive. A stream lasts at most ten minutes;
  reconnect with the last id.
- Equipo not answering is `502`, and a bad `after` `400` (plain text):
  reload `/ask/state` and start again from its cursor.

### `POST /ask/messages`

Form: `text` (at most 16 KiB after trimming; valid UTF-8, no NUL), up to
five `attachments` (upload ids, in order; see Files) and `key`, a fresh
idempotency key per message (at most 128 bytes, not starting `reply:`,
`card:` or `handoff:`). Text may be empty when there are attachments. A
resend after a network error reuses the key: Equipo keeps one message per
key, with its files.

- `200 {"message": Message, "turn": Turn}`.
- `422 {"state":"ready"}`: empty, too long, not text, or more than five
  attachments or one that isn't an upload id. Nothing was sent.
- With attachments only:
  - `409 {"state":"ready","files":"unavailable"}`: the person's runner
    can't take files right now. Ask stays ready; nothing was stored. Drop
    the files (stop offering them: `can_attach` is now `false`) and the
    words can go alone.
  - `422 {"state":"ready","files":"stale"}`: an upload is gone (dropped
    after 24 hours unsent, removed, or already sent). Attach it again.

### `POST /ask/turns/{id}/cancel`

Form: `key`. `200 {"state":"ready"}`; `409 {"state":"ready"}` when the turn
has already ended.

### `POST /ask/cards/{id}/answer`

Form: `choice` (an option's `value`), `revision` (the card's, as seen) and
`key`.

- `200 {"card": Card, "message": Message|null, "turn": Turn|null}`. An
  outcome card's answer starts no turn: both are `null`.
- `409 {"state":"ready"}`: the card moved on (answered, or a newer
  revision). Reload `/ask/state` to redraw it.

### `POST /ask/cards/{id}/carry_on`

A paused or held job's "Carry on now". Form: `revision` (the job card's, as seen)
and `key`, a fresh idempotency key kept until an answer settles the
outcome (a resend after a network error or a `502` reuses it).

- `200 {"card": Card}`: the job card as it is now (working again).
  `{"card": null}` (a card Base does not draw): reload `/ask/state`.
- `409 {"state":"ready"}`: the job moved on (a newer revision), is no longer
  paused, no longer offers it, or is not one of the person's jobs at all
  (Equipo's `card_stale`, `not_paused`, `carry_on_unavailable`, `404`).
  Reload `/ask/state` to redraw it; never resend.
- A failure to try again (`502`, no answer) belongs next to the button,
  inside the job view, not only on the status line.
- Without scripts, the form answers `303 /ask`.

### Every write's other answers

| status | body | meaning |
|---|---|---|
| `409` | `{"state":"connect"\|"pending"\|"reconnect"\|"unavailable"}` | Not connected: nothing was sent. |
| `409` | `{"state":"reconnect"}` | Equipo refused the key as it was used. Set Ask up again (with Oficina ID, below). |
| `409` | `{"state":"norunner"\|"oldrunner"}` | No runner can answer; nothing was sent. |
| `502` | `{"state":"unavailable"}` | Equipo didn't answer. Try again with the same key. |
| `400` | plain text | The form couldn't be read. |

### Files

An upload belongs to the person until it is sent, or for 24 hours; at
most 20 wait unsent. Base keeps no copy: it relays to Equipo with the
person's key. Equipo's limits: 10 MiB a file, at most 12,000 px a side and
40 megapixels an image, and nine types (PNG, JPEG, WebP, GIF, PDF, plain
text, Markdown, CSV, JSON), sniffed from the bytes. HEIC is refused.

#### `POST /ask/attachments`

The file's bytes as the body (with `Content-Length`), its type in
`Content-Type`, its name in `Content-Disposition` (`filename`, or
`filename*` beyond ASCII). Not a form, and no key: a retry is a second
upload.

- `201 Attachment`.
- `413 {"code":"attachment_too_large"}`: over 10 MiB, refused before the
  body is read when the length says so.
- Equipo's refusals, with its status: `415 {"code":"attachment_type"}`,
  `415 {"code":"attachment_heic"}`, `413 {"code":"attachment_pixels"}`,
  `409 {"code":"attachment_limit"}` (20 wait unsent: send or remove some).
- `503 {"code":"busy"}` with `Retry-After` (seconds): every upload slot is
  busy. Try again after it, by itself.
- `409 {"state"}` and `502 {"state":"unavailable"}` as for every write.

#### `DELETE /ask/attachments/{id}`

Removes an unsent upload, freeing its place among the 20. `204`, also when
it was gone already. `409 {"state":"ready","code":"attachment_sent"}` once
it is on a message. `404` for anything that isn't an upload id.

#### `POST /ask/attachments/discard`

The web page's beacon as it goes away: form, up to five `id`. Base deletes
each as best it can and always answers `204`.

#### `GET /ask/attachments/{id}`, `GET /ask/attachments/{id}/preview`

The file, streamed (`HEAD` too). `/preview` is an image's thumbnail, and
`404` for any other file. PNG, JPEG, WebP and GIF are `inline`; everything
else is an `attachment` download (text with `charset=utf-8`), and a type
outside the nine is `application/octet-stream`. Every answer has
`X-Content-Type-Options: nosniff` and a sandboxing CSP. A preview may be
cached by this client for an hour (`private, max-age=3600`); the file
itself is `no-store`.

- `404` (empty) for a file that isn't the person's, or without a live Ask
  connection; never a redirect.
- `502` (empty) when Equipo fails or is slow.

### `POST /ask/disconnect`

Ends the person's Ask connection, revoking the key in Equipo, whichever
way it was made, and marks Ask disconnected by the member (`disconnected`
in `/ask/state`), for every device and the web. It answers `303 /settings`.
A Disconnect that fails changes nothing, the mark included.

Only a connection the member makes clears the mark, in the same
transaction that stores it:
- the web's code flow once the code is approved and the key stored (pressing
  Connect or Reconnect alone leaves it set; a code that expires, is refused
  or is abandoned leaves it set);
- `POST /api/v1/ask/oficina` without `"auto": true`, once its key is stored.

Any set-up that fails leaves it set. An automatic set-up never clears it,
and the save itself refuses while it is set.

## Ask set-up with Oficina ID

### `POST /api/v1/ask/oficina`

JSON `{"assertion", "verifier", "auto"?}`: an assertion the kit fetched
from Oficina ID addressed to `equipo_origin`, and its verifier. `auto` is
`true` for a set-up the app starts by itself (after sign-in, or to renew
after `renew_after`); leave it out, or send `false`, when the person asked
to connect. `auto` must be exactly `true` or `false`: `null`, any other
type, a repeated key or another spelling (`"AUTO"`) is `400 invalid`, as is
any unknown or repeated name. Only a native session may
call it (with `Origin`, as every POST). Base completes Equipo's own native
sign-in for the person (Equipo's `POST /native/oficina-id`, then
`POST /api/v1/auth/devices/native`), keeps the key it gets sealed where the
web's code flow keeps one, and registers the Assistant's host with it.
Two posts for the same person run one at a time, and a set-up yields to
anything else that changes the person's connection meanwhile.

| status | body | meaning |
|---|---|---|
| `200` | `{"state":"ready","renew_after":"<RFC 3339>"}` | Ask is ready. The key ends with Equipo's session, seven days after it was made; `renew_after` is five days after. After it, post a fresh assertion: that replaces the key and revokes the old one in Equipo. |
| `409` | `{"state":"disconnected"}` | `auto` was `true`, and the member has disconnected Ask (also while a web code waits for approval). Nothing is stored; if the member disconnected while this set-up was at Equipo, the device Equipo made was revoked. Show Connect; post again without `auto` only when the person asks. |
| `409` | `{"state":"not_linked"}` | Equipo has no member linked to this Oficina ID. Show Ask's "not set up for you yet" state, with the web's code flow; don't retry in a loop. |
| `409` | `{"state":"superseded"}` | Ask's connection changed while Equipo answered (Disconnect, or a connection from the web): nothing was saved, and the device Equipo made was revoked. Reload `/ask/state`; don't retry at once. |
| `422` | `{"state":"failed"}` | Equipo didn't accept the assertion (expired, wrong audience or verifier, already used). Get a fresh one and try once more. |
| `400` | `{"state":"invalid"}` | Not JSON, an unknown, repeated or missing name, `auto` not exactly `true` or `false`, or Equipo couldn't read it. |
| `403` | `{"state":"forbidden"}` | Not a native session: the web keeps its code flow. |
| `502` | `{"state":"unavailable"}` | Equipo didn't answer, or refused the device. Try again later. |
| `503` | `{"state":"unavailable"}` | Base couldn't read or store the connection. |

Every failure stores nothing and leaves an existing connection as it was.
The route answers `404` when Ask Base is off. It never answers `401` for
Equipo's sake: a `401` is always Base's own session.
