# Key-value

Fast reads and writes by key, atomic counters and expiring entries — for sessions, caches, rate limits and lookups.

```js
import { kv } from 'wisp';

await kv.set('session:abc', { userId: 'u_1' }, { ttl: 3600 });
const session = await kv.get('session:abc');
const views = await kv.incr('views:home');
```

```python
from wisp import kv

kv.set("session:abc", {"userId": "u_1"}, ttl=3600)
session = kv.get("session:abc")
views = kv.incr("views:home")
```

JavaScript methods are async; Python methods are synchronous.

## Methods

| Method | Returns |
| --- | --- |
| `get(key)` | The value, or `null` / `None` |
| `set(key, value, { ttl })` | Stores any JSON value. `ttl` in seconds makes it expire. |
| `delete(key)` | Removes the key |
| `incr(key, by = 1, { ttl })` | The new number. Atomic; missing keys start at 0. |
| `list({ prefix, limit, cursor })` | `{ items: [{ key, value, expiresAt }], cursor }` in key order |

Full signatures: [kv reference](/api/sdk-kv).

## Name keys by collection

Use `<collection>:<id>` — `todo:42`, `user:ada@example.com`. The part before the first `:` or `/` is the key's collection, and the console groups keys by it.

## Design keys for your lookups

There are no queries or indexes. Shape keys around how you read:

```js
// Items, listed with a prefix
await kv.set(`todo:${id}`, todo);
const { items } = await kv.list({ prefix: 'todo:' });

// A secondary lookup: email → id
await kv.set(`user-by-email:${email}`, userId);
```

For records you filter by field, use a [table](/tables) instead.

## Counters and rate limiting

`incr` is atomic, so it's safe under concurrent requests:

```js
const n = await kv.incr(`signups:${today}`);
```

To cap how often clients call your API, use the built-in [rate limits](/rate-limits) — they're enforced before your code runs.

## Limits

- Keys up to 512 bytes; values up to 256 KB.
- Reads are strongly consistent.
- Writes are last-writer-wins, except `incr`.
- `list` returns at most 1,000 items per page; pass `cursor` back for the next page.
