# HTTP API reference

Every call is `POST https://lichkey.com/api/v1/<operation>` with a JSON object as the body; the [OpenAPI document](https://lichkey.com/api/v1/openapi.json) is generated from the same routes. Access is limited to the Anomaly team for now.

## Authentication

`Authorization: Bearer <credential>`.

- **Data calls** take a store token, `lk1.<workspace>.<store>.<id>.<secret>`. The token decides the store, the access (read, or write which includes read) and an optional key prefix; the body never names a store. A revoked token or deleted store stops working within about 10 seconds.
- **Control calls** (workspaces, stores, tokens) take an OAuth access token from Lichkey's OAuth server (`/.well-known/oauth-authorization-server`, the same sign-in the MCP server at `/mcp` uses). You can use the workspaces you're a member of: your own, the shared `anomaly` one, ones you create and ones you're invited to. Owners also invite, remove members and delete; members use stores and tokens. Store tokens don't work for control calls.
- **Getting an access token from an agent:** `POST /api/v1/auth/device`, then poll `POST /api/v1/auth/token` (OAuth device flow, RFC 8628). The home page's "For agents" section shows the exact requests.

## Where calls run

Each call is served from the Lichkey region nearest the Cloudflare location it lands on; the response's `lichkey-region` header says which. Writes go to the region that owns the key's group.

## Keys

A key is a list of parts: `["acct_123", "grant", 9]`. Parts are strings, numbers, booleans, bytes or bigints, ordered like Deno KV (bytes, then strings, numbers, bigints, booleans; a key sorts before the keys under it). The first part is the group: keys in a group are stored together, listed together and committed together. A key is at most 32 parts and 512 bytes encoded.

## Values

Values are strings, numbers, booleans, bytes, sets, lists or documents. JSON carries what it can't say as tags:

- `{"$bytes": "<base64>"}`: bytes.
- `{"$set": ["a", "b"]}`: a set of strings, or of numbers.
- `{"$bigint": "12"}`: a bigint, in keys only.
- `{"$doc": {...}}`: a document taken as written, needed only when a document's only field starts with `$`.

An object with exactly one field starting with `$` is a tag; any other object is a document. An update's `value` maps dotted field names to new values or to ops: `{"$add": 1}`, `{"$add": "x"}` (into a set), `{"$remove": "x"}`, `{"$append": [..]}`, `{"$unset": true}`. Messages are plain JSON. Durations (`ttl`, `cache`) are milliseconds or strings like `"30s"`, `"10m"`, `"1d"`.

## Results and errors

Entries are `{"key", "value", "version", "expires"?}`; a missing key is `{"key", "value": null, "version": null}`. Writes return `{"ok": true, "version"}`. A failed condition is a result with status 200: `{"ok": false, "current": …}` (for `add`, `{"ok": false}` when `max` or `min` refused it). Errors have a status and a stable code:

| Status | Code | Meaning |
|---|---|---|
| 400 | invalid, cross_group | The body is malformed or breaks a rule (a commit across groups). |
| 401 | unauthorized | Missing, invalid, expired or revoked credential. |
| 403 | forbidden | A read-only token writing, or a key outside the token's prefix. |
| 404 | not_found | No such workspace, store or token. |
| 409 | exists, idempotency_mismatch | Created before (ids are never reused), or an idempotency value reused with a different payload. |
| 413 | too_large | The body is over 8 MB. |
| 415 | unsupported | The body is declared as something other than JSON. A missing content type, or curl's default form type, is read as JSON. |
| 429 | rate_limited | The store is over its limit. Retry after `Retry-After` seconds. |
| 503 | unavailable, contended, moved, behind | Temporary. Retry after `Retry-After` seconds. |
| 500 | internal | A bug. |

The body is `{"error": {"code", "message", "retryable"}}`.

## Retries and idempotency

Writes take `idempotency` (or an `Idempotency-Key` header): a retry with the same value returns the first result for 24 hours, and a different payload with the same value is refused. Without one, retrying a write that timed out may apply it twice (a plain `set` is safe to repeat; an `add` isn't).

## Pages

`list` returns up to `limit` entries of one group and a `cursor` when there are more. Cursors are signed: they only work for the same store and range, and can't be edited.

## Limits

Per store, as safety rails: 1,000 calls and 1,000 write units per second by default (a `many` or `commit` is one call; one write unit per write per started KB). Values up to 350,000 bytes, messages up to 127 KB, up to 100 keys per `many` and 99 items per `commit`.

## Data calls

### POST /api/v1/get

Read a key.

- `key`: Key
- `consistency?`: Consistency
- `cache?`: integer | string. Serve from the edge cache, up to this old.
- `since?`: Version. Only accept this version or newer (read-your-writes).

```json
{"key":["acct_123","profile"],"consistency":"strong"}
```

### POST /api/v1/many

Read up to 100 keys, in order. Each meets its own guarantee; not a snapshot.

- `keys`: Key[]
- `consistency?`: Consistency
- `cache?`: integer | string. Serve from the edge cache, up to this old.
- `since?`: Version | Version | null[]. A version for all keys, or one (or null) per key.

```json
{"keys":[["acct_123","profile"],["acct_123","plan"]]}
```

### POST /api/v1/set

Write a key. Without if, the last write wins. Takes an `Idempotency-Key` header.

- `key`: Key
- `value`: Value
- `if?`: Condition
- `ttl?`: integer | string. Expire after this long.
- `wait?`: boolean. false: reply before the write commits (no version, no condition outcome).
- `idempotency?`: Idempotency

```json
{"key":["acct_123","profile"],"value":{"name":"dax","avatar":{"$bytes":"iVBORw0KGgo="}},"ttl":"30d"}
```

### POST /api/v1/create

Write a key only if it doesn't exist: ok false if it does. Takes an `Idempotency-Key` header.

- `key`: Key
- `value`: Value
- `ttl?`: integer | string. Expire after this long.
- `wait?`: boolean. false: reply before the write commits (no version, no condition outcome).
- `idempotency?`: Idempotency

```json
{"key":["handle","dax"],"value":"acct_123"}
```

### POST /api/v1/update

Change fields of a document (created if missing), atomically at the owner. Returns the new document. Takes an `Idempotency-Key` header.

- `key`: Key
- `value`: Changes
- `if?`: Condition
- `ttl?`: integer | string. Expire after this long.
- `wait?`: boolean. false: reply before the write commits (no version, no condition outcome).
- `idempotency?`: Idempotency

```json
{"key":["acct_123","profile"],"value":{"name":"dax","logins":{"$add":1},"tags":{"$add":"admin"},"legacy":{"$unset":true}},"if":{"lt":["edits",100]}}
```

### POST /api/v1/add

Add to a number atomically (a missing key counts as 0). Returns the new value; ok false when max or min refused it. ttl is set on creation and kept (fixed windows). Takes an `Idempotency-Key` header.

- `key`: Key
- `value?`: number
- `max?`: number
- `min?`: number
- `ttl?`: integer | string. Expire after this long.
- `wait?`: boolean. false: reply before the write commits (no version, no condition outcome).
- `idempotency?`: Idempotency

```json
{"key":["ws_9","limit","2026-11-23"],"value":1,"max":1000,"ttl":"1d"}
```

### POST /api/v1/delete

Delete a key. ok false if the condition failed. Takes an `Idempotency-Key` header.

- `key`: Key
- `if?`: Condition
- `wait?`: boolean. false: reply before the write commits (no version, no condition outcome).
- `idempotency?`: Idempotency

```json
{"key":["acct_123","session"],"if":{"eq":["revoked",true]}}
```

### POST /api/v1/list

Entries in one group, in key order, a page at a time. prefix lists the keys under it; start is inclusive, end exclusive. The first part of each is the group.

- `prefix?`: Key
- `start?`: Key
- `end?`: Key
- `limit?`: integer. Page size; default 1,000.
- `reverse?`: boolean
- `consistency?`: Consistency
- `cursor?`: string. From the previous page; only valid for the same store and range.

```json
{"prefix":["acct_123","grant"],"limit":100}
```

### POST /api/v1/commit

Checks, writes and messages in one group, all or nothing. Messages are sent only if it commits. Takes an `Idempotency-Key` header.

- `checks?`: object[]. Versions read: each key must still be at this version (null: still missing).
- `writes?`: Write[]
- `messages?`: Json[]. Each at most 127 KB of JSON.
- `idempotency?`: Idempotency

```json
{"checks":[{"key":["acct_123","profile"],"version":"0193f0a1b2c30000ab12"}],"writes":[{"type":"set","key":["acct_123","profile"],"value":{"name":"dax"}},{"type":"delete","key":["acct_123","grant","9"]}],"messages":[{"event":"renamed","account":"acct_123"}]}
```

## Control calls

### POST /api/v1/workspaces/create

Create a workspace; you become its owner. Ids are never reused. Up to 10 owned workspaces per person, your own included.

- `workspace`: string
- `name?`: string

```json
{"workspace":"acme","name":"Acme"}
```

### POST /api/v1/workspaces/get

Read a workspace you belong to.

- `workspace`: string

### POST /api/v1/workspaces/list

The workspaces you belong to, with your role.

Body: `{}`.

### POST /api/v1/workspaces/delete

Delete a workspace (owners): its stores and tokens stop working at once.

- `workspace`: string

### POST /api/v1/members/list

A workspace's members (any member can list).

- `workspace`: string

### POST /api/v1/members/remove

Remove a member (owners), or yourself (leave). The last owner can't. Their access stops within about 10 s, and store tokens they made in the workspace are revoked.

- `workspace`: string
- `member`: string. Their account subject. Yours to leave.

```json
{"workspace":"acme","member":"<account sub>"}
```

### POST /api/v1/invites/create

Invite someone (owners) by verified email, account id, or both. They join when they next sign in with that email or account, or by opening the link. Expires in 7 days; up to 50 pending per workspace.

- `workspace`: string
- `email?`: string
- `sub?`: string. Their account subject.

```json
{"workspace":"acme","email":"ana@anoma.ly"}
```

### POST /api/v1/invites/list

With workspace: its pending invites (owners). Without: pending invites addressed to you.

- `workspace?`: string

### POST /api/v1/invites/revoke

Revoke a pending invite (owners).

- `workspace`: string
- `invite`: string

### POST /api/v1/invites/accept

Accept an invite addressed to you: by its code (from a link), or by workspace and invite id (from invites/list). Used once.

- `code?`: string
- `workspace?`: string
- `invite?`: string

```json
{"code":"lki1.acme.…"}
```

### POST /api/v1/stores/create

Create a store.

- `workspace`: string
- `store`: string

```json
{"workspace":"acme","store":"main"}
```

### POST /api/v1/stores/get

Read a store.

- `workspace`: string
- `store`: string

### POST /api/v1/stores/list

A workspace's stores.

- `workspace`: string

### POST /api/v1/stores/delete

Delete a store: its tokens stop working at once.

- `workspace`: string
- `store`: string

### POST /api/v1/stores/usage

Hourly usage of a store (UTC hours, approximate).

- `workspace`: string
- `store`: string

### POST /api/v1/tokens/create

Create a store token. The token is returned once; only its hash is kept.

- `workspace`: string
- `store`: string
- `access?`: "read" | "write"
- `prefix?`: Key. Only keys under this key.
- `name?`: string
- `expiresAt?`: integer. Milliseconds since the epoch.

```json
{"workspace":"acme","store":"main","access":"read","prefix":["acct_123"],"name":"reporting"}
```

### POST /api/v1/tokens/list

A workspace's or store's tokens, without secrets.

- `workspace`: string
- `store?`: string

### POST /api/v1/tokens/revoke

Revoke a token.

- `workspace`: string
- `store`: string
- `token`: string
