# Create kv

URL: https://docs.hanzo.ai/docs/openapi/kv/post-kv-by-bucket

> Creates a KV bucket and returns it.

`POST /v1/kv/{bucket}`

> [KV →](https://docs.hanzo.ai/docs/openapi/kv) · [All API references →](https://docs.hanzo.ai/docs/openapi) · [Get started →](https://docs.hanzo.ai/docs/guides)

|               |                                        |
| ------------- | -------------------------------------- |
| **Address**   | `https://api.hanzo.ai/v1/kv/{bucket}`  |
| **Method**    | `POST`                                 |
| **Operation** | `post_kv_by_bucket`                    |
| **Auth**      | `Authorization: Bearer $HANZO_API_KEY` |

Creates a KV bucket and returns it. A bucket is keyed state on
the same durable plane as the streams: each key holds up to History
revisions, entries can expire by TTL, and watchers on the NATS port see every
write. 409 when the org already has a bucket of that name.

## Request

5 fields, body `application/json` (required).

| Field      | In   | Type            | Required | Description                                                                                 |
| ---------- | ---- | --------------- | -------- | ------------------------------------------------------------------------------------------- |
| `bucket`   | path | string          | yes      | Bucket is the bucket's name within the org, from the path: 1–64 of \[A-Za-z0-9\_], no dash. |
| `bucket`   | body | string          | —        | Bucket is the bucket's name within the org, from the path: 1–64 of \[A-Za-z0-9\_], no dash. |
| `history`  | body | integer (int64) | —        | History is how many revisions each key keeps, 1–64.                                         |
| `maxValue` | body | integer (int64) | —        | MaxValue caps one value's size in bytes.                                                    |
| `ttl`      | body | integer (int64) | —        | TTL expires entries after this many SECONDS.                                                |

## Response

| Status    | Body            | Meaning |
| --------- | --------------- | ------- |
| `201`     | kv.bucketRecord | created |
| `default` | problem-details | refused |

`201` body — 4 fields.

| Field     | In   | Type             | Always | Description                                           |
| --------- | ---- | ---------------- | ------ | ----------------------------------------------------- |
| `bucket`  | body | string           | —      | Bucket is the bucket's name within the org.           |
| `history` | body | integer (int64)  | —      | History is how many revisions each key keeps.         |
| `ttl`     | body | integer (int64)  | —      | TTL is the entry expiry in seconds; 0 means none.     |
| `values`  | body | integer (uint64) | —      | Values is how many values the bucket holds right now. |

Failure carries the platform error shape — see [Errors](https://docs.hanzo.ai/docs/errors).

## Examples

**CLI**

```bash
hanzo kv add <bucket>
```

**SDK**

**.ts**

```ts
import { Configuration, KvApi } from 'hanzoai';

const api = new KvApi(new Configuration({ accessToken: process.env.HANZO_API_KEY }));
const { data } = await api.postKvByBucket({ bucket: 'bucket', bucket: "<bucket>", history: 0 });
```

**.py**

```python
from hanzoai.cloud import ApiClient, Configuration
from hanzoai.cloud.api import KvApi

client = ApiClient(Configuration(access_token=os.environ["HANZO_API_KEY"]))
result = KvApi(client).post_kv_by_bucket(bucket='bucket', bucket="<bucket>", history=0)
```

**.go**

```go
cfg := hanzoai.NewConfiguration()
cfg.AddDefaultHeader("Authorization", "Bearer "+os.Getenv("HANZO_API_KEY"))
client := hanzoai.NewAPIClient(cfg)

resp, _, err := client.KvAPI.PostKvByBucket(context.Background()).Execute()
if err != nil {
	return err
}
```

**.rs**

```rust
use hanzo_client::apis::{configuration::Configuration, kv_api};

let mut cfg = Configuration::new();
cfg.bearer_access_token = std::env::var("HANZO_API_KEY").ok();

let result = kv_api::post_kv_by_bucket(&cfg, Default::default()).await?;
```

**.java**

```java
import ai.hanzo.cloud.ApiClient;
import ai.hanzo.cloud.api.KvApi;

ApiClient client = new ApiClient();
client.setBearerToken(System.getenv("HANZO_API_KEY"));

var result = new KvApi(client).postKvByBucket();
```

**HTTP**

```bash
curl -X POST https://api.hanzo.ai/v1/kv/<bucket> \
  -H "Authorization: Bearer $HANZO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
       "bucket": "<bucket>",
       "history": 0
     }'
```

**MCP**

MCP reaches **kv** through the `kv` tool, which names its 6 operations with its own verbs — this one among them, under a name only MCP declares. `describe` explains any of them:

```bash
curl -X POST https://api.hanzo.ai/v1/mcp \
  -H "Content-Type: application/json" \
  -d '{
       "jsonrpc": "2.0",
       "id": 1,
       "method": "tools/call",
       "params": {
         "name": "describe",
         "arguments": {
           "op": "get_kv"
         }
       }
     }'
```

---

[KV API](https://docs.hanzo.ai/docs/openapi/kv) · [All Hanzo APIs](https://docs.hanzo.ai/docs/openapi) · [Interactive reference](https://docs.hanzo.ai/docs/openapi/reference)
