# kv

Key-value storage — get, set, delete, incr and list.

```js
import { kv } from 'wisp';     // async: await every call
```

```python
from wisp import kv            # synchronous
```

Guide: [Key-value](/key-value).

## kv.get(key)

```js
const value = await kv.get('user:42');          // value or null
```
```python
value = kv.get("user:42", default=None)
```

## kv.set(key, value, options?)

Stores any JSON value. `ttl` (seconds) makes it expire.

```js
await kv.set('session:abc', { userId: 42 }, { ttl: 3600 });
```
```python
kv.set("session:abc", {"userId": 42}, ttl=3600)
```

## kv.delete(key)

```js
await kv.delete('session:abc');
```

## kv.incr(key, by = 1, options?)

Atomically adds `by` and returns the new number. Missing keys start at 0. Accepts `ttl` like `set`.

```js
const n = await kv.incr('views:home');
const left = await kv.incr('credits:42', -1);
```
```python
n = kv.incr("views:home")
```

## kv.list(options?)

Lists keys in key order.

| Option | Default | Description |
| --- | --- | --- |
| `prefix` | `""` | Only keys starting with this |
| `limit` | 100 | Up to 1,000 |
| `cursor` | `null` | From the previous page |

Returns `{ items: [{ key, value, expiresAt }], cursor }`; `cursor` is `null` / `None` on the last page.

```js
let cursor = null;
do {
  const page = await kv.list({ prefix: 'todo:', cursor });
  for (const { key, value } of page.items) console.log(key, value);
  cursor = page.cursor;
} while (cursor);
```
```python
page = kv.list(prefix="todo:", limit=100)
```

## Limits

Keys up to 512 bytes; values up to 256 KB. Writes fail with `Storage is full` when the workspace is over its [storage quota](/storage#quota).
