# Lichkey

A key-value store that runs everywhere. Data lives where it's written, and reads come from nearby.

Access is limited to the Anomaly team for now.

## How it works

```
 write in London ──▶ owned in eu-west-2
                          │ push (~100 ms)
                          ▼
 read in Tokyo  ──▶ local copy in ap-northeast-1  (7 ms)
```

Each group of keys is stored in the region that first writes it. Other regions get a copy the first time they read it, kept fresh by pushes.

## Speed

| | p50 |
|---|---|
| read | 7–15 ms |
| write | 13–26 ms |
| change seen in other regions | ~100 ms |

Measured from six regions. A single-region store is that fast in one place and 70–200 ms everywhere else.

## Keys

```
["acct_123", "grant", 9]
 └─group──┘
```

Keys are tuples, ordered like Deno KV. The first part is the group: keys in a group are stored together, listed together and committed together.

## API

```ts
await kv.set(["acct_123", "profile"], { name: "dax" })
await kv.get(["acct_123", "profile"])
await kv.create(["handle", "dax"], "acct_123")                 // only if it doesn't exist
await kv.add(["ws_9", "limit", day], 1, { max: 1000, ttl: "1d" })
await kv.atomic(async (tx) => { /* read, decide, write */ })
```

A Deno KV-style API, plus conditions, partial updates, atomic counters and transactions within a group.

## Guarantees

- Reads through the SDK include your own writes.
- Conditional writes, counters and `atomic` are exact.
- Strong reads return the latest value when you ask for it.

## Use it

- **Workers:** a service binding and the `lichkey` SDK.
- **HTTP:** `POST /api/v1/<verb>` (`get`, `set`, `update`, `list`, `commit`, …) with a store token. [Reference](/docs/api), [OpenAPI](/api/v1/openapi.json)
- **MCP:** add `https://lichkey.com/mcp` to your agent and sign in with your Anomaly account.

## For agents

If someone gives you this URL and asks to use Lichkey, sign them up:

1. Start sign-in. Show the person `verification_uri_complete` and `user_code`.
2. Poll `auth/token` every `interval` seconds. `authorization_pending` means wait, `slow_down` means wait longer. Stop on any other error.
3. List their workspaces and use their own, the one without `"shared": true`.
4. Create a store there, then a store token. The token is shown once.
5. Add `https://lichkey.com/mcp` as an MCP server, or give their app `LICHKEY_URL=https://lichkey.com` and `LICHKEY_TOKEN=<token>`.

```http
POST https://lichkey.com/api/v1/auth/device
{"client_name": "<your name>"}
→ {"device_code": "lkd1.…", "user_code": "BCDF-GHJK", "verification_uri_complete": "…", "interval": 5, "expires_in": 600}

POST https://lichkey.com/api/v1/auth/token
{"grant_type": "urn:ietf:params:oauth:grant-type:device_code", "device_code": "lkd1.…"}
→ 400 {"error": "authorization_pending"}, then 200 {"access_token": "lkm1.…", "refresh_token": "lkr1.…", "expires_in": 3600}

POST https://lichkey.com/api/v1/workspaces/list
Authorization: Bearer lkm1.…
{}
→ {"workspaces": [{"id": "w…", "role": "owner"}, {"id": "anomaly", "shared": true, "role": "member"}]}

POST https://lichkey.com/api/v1/stores/create
Authorization: Bearer lkm1.…
{"workspace": "w…", "store": "app"}

POST https://lichkey.com/api/v1/tokens/create
Authorization: Bearer lkm1.…
{"workspace": "w…", "store": "app", "access": "write", "name": "app"}
→ {"token": "lk1.…"}
```

To share a workspace, `workspaces/create` one and `invites/create` someone by email; they join when they sign in, or by opening the invite link. The access token lasts an hour; refresh it at `https://lichkey.com/oauth/token`. The app calls `POST https://lichkey.com/api/v1/<verb>` with `Authorization: Bearer $LICHKEY_TOKEN` and a body like `{"key": ["acct_123", "profile"], "value": {...}}`, or uses `lichkey({ url, token })`.

## Built on

DynamoDB in six regions and one Cloudflare Worker. Pay-per-use, no fixed cost.
