# Set Metadata

`PATCH /metadata/collection/{collection}/{id}` · Core · Data, Queries & Vectors

Update collection by id. (Data, Queries & Vectors) PATCH `/@:projectSlug/metadata/collection/{collection}/{id}`.

## Parameters

| Name | In | Required | Description |
|---|---|---|---|
| `collection` | path | yes | The collection's name. Example: `"products"`. |
| `id` | path | yes | The id of the record whose metadata columns to write. Example: `"rec_01hx7d4q8w2e"`. |
| `projectSlug` | path | yes | The project's slug: the path segment after `@`. It is unique within the org. Example: `"storefront"`. |

## Request body

| Field | Type | Required | Description |
|---|---|---|---|
| `metadata` | object | yes | The metadata columns to write, keyed by declared column name, at least one. Each value must suit the column's type, as on PATCH /data/{collection}/{id}. An undeclared key is refused with 400 `K9O_METADATA_FIELD_NOT_INDEXED`. |

## Responses

- **200**: Success

| Field | Type | Required | Description |
|---|---|---|---|
| `data` | object | yes | The updated record, every column the caller may see, keyed by column name (the same row PATCH /data/{collection}/{id} returns). Protected columns the caller may not reveal are withheld as on that route. |
| `meta` | object | yes | Request metadata: request id, trace id, timing and cost. |

- **202**: Accepted

| Field | Type | Required | Description |
|---|---|---|---|
| `data` | object | yes | The change, held for approval. Returned with status 202 when the collection needs an approver for updates; nothing is written until it is approved. |
| `data.id` | string | yes | The pending change's id (`apr_...`). Decide it through the pending approvals API. |
| `data.status` | enum ("pending_approval") | yes | Always `pending_approval`: the change waits for an approver. |
| `data.collection` | string | yes | The collection the change targets. |
| `data.operation` | enum ("update") | yes | Always `update` on this route. |
| `data.makerId` | string | yes | Who asked for the change: the accountable originator's subject id (a user, end user or token id). |
| `data.proposedPayload` | object | yes | The metadata columns and values the change will write, exactly as sent in `metadata`. |
| `data.targetRecordId` | string | yes | The id of the record the change updates. |
| `meta` | object | yes | Request metadata, plus `approval.status` = `pending_approval`. |


## Errors

| HTTP | Code | When |
|---|---|---|
| 401 | `K9O_INVALID_CREDENTIALS` | The caller is not authenticated. |
| 403 | `K9O_ACCESS_DENIED` | The caller has no read access to the collection (the record read's own gate). |
| 403 | `K9O_EXTENSION_PERMISSION_DENIED` | An extension token: the Metadata API is not extension-callable yet. |
| 400 | `K9O_INVALID_FIELD_VALUE` | The body is malformed: an unknown field, a wrong type, or an unsupported where operator. |
| 400 | `K9O_METADATA_FIELD_NOT_INDEXED` | A field is not a declared metadata field of the surface (ch04 §1.3.1); declare it with PUT /metadata/index/{collection}. |
| 403 | `K9O_RBAC_DENY` | The caller may not write this record (the record PATCH's own gate). |
| 404 | `K9O_NOT_FOUND` | No such record in the collection (the record PATCH's own not-found). |

## Code samples

### cURL

```bash
curl -X PATCH 'https://<org>.api-<region>.k9o.io/@storefront/metadata/collection/products/prod_demo00000001' \
  -H 'Authorization: Bearer $K9O_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{}'
```

### fetch

```javascript
const res = await fetch('https://<org>.api-<region>.k9o.io/@storefront/metadata/collection/products/prod_demo00000001', {
  method: 'PATCH',
  headers: {
    'Authorization': `Bearer ${process.env.K9O_TOKEN}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({}),
});
const { data, meta } = await res.json();
```

### k9o CLI

```bash
k9o metadata set <collection> <id> --set '<json object>'
```

### @k9o/core

```typescript
// const k9o = new K9o({ orgSlug: 'playground', projectSlug: 'storefront', apiKey: 'k9o_…' })
const { data } = await k9o.metadata.set('<collection>', '<id>', {})
```

### k9o

```python
# k9o = create_client(org_slug="playground", project_slug="storefront", api_key="k9o_…")
response = k9o.metadata.set("<collection>", "<id>", {})
```

### sdk-go

```go
// ctx := context.Background()
// client, _ := k9o.NewClient(k9o.ClientOpts{OrgSlug: "playground", ProjectSlug: "storefront", APIKey: "k9o_…"})
data, _, kerr := client.Metadata.Set(ctx, "<collection>", "<id>", map[string]any{})
```

### k9o

```rust
// let k9o = Client::new(ClientOpts { org_slug: "playground".into(), project_slug: "storefront".into(), api_key: "k9o_…".into() })?;
let resp = k9o.metadata.set("<collection>", "<id>", json!({})).await?
```

### dev.k9o:core

```java
// K9o k9o = K9o.createClient(K9o.Opts.builder().orgSlug("playground").projectSlug("storefront").apiKey("k9o_…").build());
var response = k9o.metadata().set("<collection>", "<id>", Map.of())
```

### K9o.Core

```c#
// var k9o = K9oClient.Create(new K9oOptions { OrgSlug = "playground", ProjectSlug = "storefront", ApiKey = "k9o_…" });
var response = await k9o.Metadata.SetAsync("<collection>", "<id>", new Dictionary<string, object> {  })
```

### k9o

```ruby
# k9o = K9o.create_client(org_slug: "playground", project_slug: "storefront", api_key: "k9o_…")
response = k9o.metadata.set("<collection>", "<id>", {  })
```

### k9o/sdk

```php
// $k9o = K9oClient::create(['org_slug' => 'playground', 'project_slug' => 'storefront', 'api_key' => 'k9o_…']);
$response = $k9o->metadata->set('<collection>', '<id>', [])
```


Canonical page: https://docs.kilimanjaro.dev/api-playground/core/set-metadata
