# Dicts

> A key-value store for small shared state, what has been seen, counters, locks and caches with expiry, that reads like a Python dict.

A **dict** is a key-value store NoClick keeps for you: string keys, JSON values, and an expiry per key if you want one. It's for the small pieces of state your code and agents share: which orders you've already handled, a counter, a lock so a job runs once, a cache that empties itself. Every read and write goes to the API, so a web app, a worker and an agent see each other's changes at once, with nothing to host.

Values are JSON (a dict, list, string, number, boolean or `None`), never pickled, so what your Python code writes reads the same from TypeScript, from an agent, or in the console.

## Create a dict

Name it in your code: `from_name` with `create_if_missing=True` creates the dict the first time it's used, and finds it every time after.

```python
import noclick

seen = noclick.Dict.from_name("processed-orders", create_if_missing=True)
```

```ts
import { Dict } from "noclick";

const seen = Dict.fromName("processed-orders", { createIfMissing: true });
```

`create` makes one outright, and raises `noclick.Conflict` if the name is taken (its `existing_id` names the one that's there). A name is 1 to 64 letters, digits and `. _ -`, unique for each user, and for your account; created inside `as_user`, a dict belongs to that user and bills to it:

```python
with noclick.as_user("acme"):
    flags = noclick.Dict.create("feature-flags")
```

```ts
import { Dict, asUser } from "noclick";

await asUser("acme", async () => {
  const flags = await Dict.create("feature-flags");
});
```

`region=` keeps its keys in a [region](https://www.noclick.com/docs/guide/regions.md), for good (your account's default, else `us-east`). Dicts are kept in NoClick's own database in `us-east` for now, so that's the one region they take: another, or an account default region elsewhere, raises `noclick.InvalidRequest` naming it, and you pass `region="us-east"` to keep the dict there.

## Read and write keys

A dict reads like a Python dict:

```python
seen = noclick.Dict.from_name("processed-orders", create_if_missing=True)

seen["o_1841"] = {"status": "refunded"}
print(seen["o_1841"]["status"])   # refunded
print("o_1842" in seen)           # False
print(seen.get("o_1842", default="new"))
del seen["o_1841"]
print(len(seen))
```

```ts
import { Dict } from "noclick";

const seen = Dict.fromName("processed-orders", { createIfMissing: true });

await seen.put("o_1841", { status: "refunded" });
console.log((await seen.get<{ status: string }>("o_1841"))?.status);   // refunded
console.log(await seen.has("o_1842"));                                // false
console.log((await seen.get("o_1842")) ?? "new");
await seen.remove("o_1841");
console.log(await seen.size());
```

A missing key raises `KeyError` on `seen[key]` and `del seen[key]`; `get` returns its `default` instead (`None` unless you give one). `pop(key, default)` removes a key and returns its value, and `remove(key)` removes one and says whether it was there. `len(seen)` counts the keys now; `seen.key_count` is the count as of the last load.

Each of these is a call, never a local copy, so there's no save step and no stale read. In async code, use the method forms: `await seen.get.aio(key)`, `await seen.put.aio(key, value)`, `await seen.pop.aio(key, None)`.

Keys are non-empty strings. A value that isn't JSON, like a `datetime` or a set, raises `TypeError` before anything is sent; convert it first (a `datetime` to an ISO string, say). A tuple comes back as a list.

## Expire keys

`put` writes a key like `seen[key] = value` does, and takes `ttl_s`: the key disappears that many seconds later.

```python
cache = noclick.Dict.from_name("rates-cache", create_if_missing=True)
cache.put("usd:eur", 0.92, ttl_s=86400)
```

```ts
import { Dict } from "noclick";

const cache = Dict.fromName("rates-cache", { createIfMissing: true });
await cache.put("usd:eur", 0.92, { ttlS: 86400 });
```

A `put` without `ttl_s` keeps the key until it's removed, and clears any expiry it had. An expired key is gone for every read: `get` returns the default, `in` is false, and it no longer counts in `len`.

## Take a lock, or run something once

`put_if_absent` writes a key only if it isn't there, in one atomic step: of any number of callers racing for the same key, exactly one gets `True`.

```python
locks = noclick.Dict.from_name("locks", create_if_missing=True)
if locks.put_if_absent("nightly-report", "worker-3", ttl_s=300):
    print("This worker runs the report")
```

```ts
import { Dict } from "noclick";

const locks = Dict.fromName("locks", { createIfMissing: true });
if (await locks.putIfAbsent("nightly-report", "worker-3", { ttlS: 300 })) {
  console.log("This worker runs the report");
}
```

With `ttl_s`, a lock whose holder crashes frees itself; `locks.remove("nightly-report")` frees it early. The same call makes a webhook handler safe to retry, by recording each delivery id the first time:

```python
seen = noclick.Dict.from_name("processed-orders", create_if_missing=True)
if seen.put_if_absent("evt_9f2c", True, ttl_s=7 * 86400):
    print("First delivery: handle it")
```

```ts
import { Dict } from "noclick";

const seen = Dict.fromName("processed-orders", { createIfMissing: true });
if (await seen.putIfAbsent("evt_9f2c", true, { ttlS: 7 * 86400 })) {
  console.log("First delivery: handle it");
}
```

## Count

`increment` adds to a counter atomically, so concurrent callers never lose a count, and returns the new value. A missing key starts at 0, and `by` can be negative.

```python
stats = noclick.Dict.from_name("stats", create_if_missing=True)
total = stats.increment("refunds-today")

limits = noclick.Dict.from_name("rate-limits", create_if_missing=True)
if limits.increment("calls:acme:2026-09-28T14", ttl_s=3600) > 100:
    print("Over the limit; try again next hour")
```

```ts
import { Dict } from "noclick";

const stats = Dict.fromName("stats", { createIfMissing: true });
const total = await stats.increment("refunds-today");

const limits = Dict.fromName("rate-limits", { createIfMissing: true });
if ((await limits.increment("calls:acme:2026-09-28T14", { ttlS: 3600 })) > 100) {
  console.log("Over the limit; try again next hour");
}
```

`ttl_s` on `increment` sets the expiry only when the call creates the key, so a counter per window cleans itself up. A key that holds anything but an integer raises `noclick.InvalidRequest`.

## Write many at once

`update` writes up to 1,000 keys in one call, all of them or none, with an optional `ttl_s` for each:

```python
flags = noclick.Dict.from_name("feature-flags", create_if_missing=True)
flags.update({"new-checkout": True, "beta-search": False, "max-upload-mb": 50})
```

```ts
import { Dict } from "noclick";

const flags = Dict.fromName("feature-flags", { createIfMissing: true });
await flags.update({ "new-checkout": true, "beta-search": false, "max-upload-mb": 50 });
```

## List keys and values

`keys` and `items` list in key order, optionally only the keys that start with a `prefix`. Iterating the dict itself walks every key a page at a time.

```python
seen = noclick.Dict.from_name("processed-orders")

for key in seen.keys(prefix="lock:"):
    print(key)

for key, value in seen.items(prefix="o_", limit=100):
    print(key, value)

for item in seen.items(prefix="lock:"):
    print(item.key, item.expires_at)
```

```ts
import { Dict } from "noclick";

const seen = Dict.fromName("processed-orders");

for (const key of await seen.keys({ prefix: "lock:" })) {
  console.log(key);
}

for (const { key, value } of await seen.items({ prefix: "o_", limit: 100 })) {
  console.log(key, value);
}

for (const item of await seen.items({ prefix: "lock:" })) {
  console.log(item.key, item.expiresAt);
}
```

Each `DictItem` unpacks like a pair, so `dict(seen.items())` works, and also carries `expires_at`, `updated_at` and `size_bytes`. Without a `limit`, `keys` and `items` fetch everything, page by page; for a big dict, use `items_page(prefix=..., cursor=...)` a page at a time, or iterate the dict. A prefix scheme (`lock:`, `o_`, `cache:`) keeps one dict tidy for several uses.

## Use it from an app

An app reaches a dict through a binding: `app.bind("SEEN", seen)` makes it available to the deployed code as `SEEN`, and the dict's `used_by` lists the apps that bind it; the app's code can also find or make it by name with `from_name`. See [Apps](https://www.noclick.com/docs/guide/apps.md).

A preview's code gets a copy of the dict's keys the first time it uses it, and writes only to that. See [A preview's own data](https://www.noclick.com/docs/guide/apps.md#a-preview-s-own-data).

A dict holds data your code reads and writes. What an agent should carry from one conversation to the next, in its own words, belongs in its [memory](https://www.noclick.com/docs/guide/memory.md) instead.

## Clear, export and delete

```python
cache = noclick.Dict.from_name("rates-cache")
cleared = cache.clear()            # every key gone; the dict stays
cleared.undo()                     # changed your mind: the keys are back
item = cache.delete()              # to the trash, keys and all
item.restore()
```

```ts
import { Dict } from "noclick";

const cache = Dict.fromName("rates-cache");
const cleared = await cache.clear();     // every key gone; the dict stays
await cleared.undo();                    // changed your mind: the keys are back
const item = await cache.delete();       // to the trash, keys and all
await item.restore();
```

`delete` moves the dict and its keys to the trash, restorable for 30 days. Every write is logged with the value it replaced, so one key can be put back too: see [Undo and the trash](https://www.noclick.com/docs/guide/undo.md).

A dict an app binds goes to the trash too, since restoring it undoes the delete: the app keeps its binding, and while the dict is in the trash the app's code gets `noclick.NotFound` for it. Restore it and the app reaches it again; once it's purged, the app's binding names nothing. Its delete's preview (`POST /v1/delete_previews`) names each app that binds it before anything happens.

```python
seen = noclick.Dict.from_name("processed-orders")
bundle = seen.export().wait()      # JSON Lines: one key, value and expiry per line
print(bundle.url)
```

```ts
import { Dict } from "noclick";

const seen = Dict.fromName("processed-orders");
const bundle = await (await seen.export()).wait();   // JSON Lines: one key, value and expiry per line
console.log(bundle.url);
```

`export` is an [operation](https://www.noclick.com/docs/guide/operations.md) that gives an [export bundle](https://www.noclick.com/docs/guide/export.md): `dicts/<name>.jsonl`, every key as the dict stood at one moment, in key order, one `{"key", "value", "expires_at"}` per line.

## Limits and billing

| Limit | Value |
| --- | --- |
| Key | 512 bytes of UTF-8 |
| Value | 1 MB as JSON |
| One `update` | 1,000 keys |
| A dict | 100 MB of keys and values |
| Your account's dicts and queues together | 1 GB |
| Dicts per account, its users' included | 1,000 |
| Expiry | 1 second to 365 days |

Dicts cost nothing within these limits. Metered pricing, by the GB-month stored and per thousand reads and writes, will come with larger limits. For rows you query with SQL, use a [database](https://www.noclick.com/docs/guide/databases.md); for files, a [bucket](https://www.noclick.com/docs/guide/storage.md); for work to hand off, a [queue](https://www.noclick.com/docs/guide/queues.md).

## Errors

| Error | When |
| --- | --- |
| `KeyError` | `seen[key]`, `del seen[key]` or `pop(key)` without a default, on a missing or expired key |
| `TypeError` | a value that isn't JSON, or a key that isn't a non-empty string |
| `InvalidRequest` | `increment` on a key that isn't an integer; a key or value over its limit, a value that holds NaN or a NUL character; a region dicts aren't kept in |
| `Conflict` | the name is taken |
| `QuotaExceeded` | the dict would pass 100 MB, or your account's dicts and queues 1 GB; a 1,001st dict |
| `NotFound` | no dict by that name or id, for the user the call acts for |

Every method is in the [Dict reference](https://www.noclick.com/docs/reference/python/Dict.md): [`get`](https://www.noclick.com/docs/reference/python/Dict.md#Dict.get), [`put`](https://www.noclick.com/docs/reference/python/Dict.md#Dict.put), [`put_if_absent`](https://www.noclick.com/docs/reference/python/Dict.md#Dict.put_if_absent), [`increment`](https://www.noclick.com/docs/reference/python/Dict.md#Dict.increment), [`items`](https://www.noclick.com/docs/reference/python/Dict.md#Dict.items) and the rest.
