# The API

Everything the CLI does goes through this API at `https://api.afile.sh/v1`. Your agent encrypts first; the API only ever gets ciphertext.

The full profile, with every field, is in the protocol: [Agent storage, share links and receive links](https://github.com/afile-org/protocol/blob/main/spec/0.1/agent-storage.md). `GET https://api.afile.sh/.well-known/afile` describes the provider and its limits.

## Calls [#calls]

| Call | What it does |
| --- | --- |
| `POST /v1/agents` | Register an agent, with no account. Returns its token. |
| `GET /v1/me` | The agent: its storage used, items and open links |
| `POST /v1/items` | Save an item |
| `GET /v1/items` | List items, newest change first |
| `GET /v1/items/{id}` | Read an item, with its `ETag` |
| `GET /v1/items/{id}/content` | Its sealed bytes, or an earlier `?version=` |
| `PUT /v1/items/{id}` | Replace it or change its meta, with `If-Match` |
| `DELETE /v1/items/{id}` | Delete it, with `If-Match` |
| `POST /v1/items/{id}/links` | Make a share link |
| `POST /v1/receive-links` | Make a receive link |
| `GET /v1/links` | List links |
| `DELETE /v1/links/{id}` | Close a link |

Send the token as `Authorization: Bearer <token>`. Lists take `cursor` and `limit` (1 to 200).

## How writes work [#writes]

- **Sealed bytes.** An item's key, name, tags, note and contents are encrypted before they're sent. Contents can go as a framed body, `application/afile-item`, so aFile can stream them to storage without decoding.
- **Retries.** Every write takes an `Idempotency-Key`. The same key with the same body returns the first result.
- **Changes.** Replacing or deleting needs `If-Match` with the item's current `ETag`. A stale one gets `412 precondition_failed`.

## Errors [#errors]

Errors come back as `{"error", "status", "code", "detail", "next"}`. Read `error`; `next`, when present, says what to do. The codes include `invalid_token`, `not_found`, `precondition_failed`, `idempotency_conflict`, `item_too_large`, `quota_exceeded`, `link_closed` and `rate_limited`, which comes with `retry-after`.
