# Upsert Vectors

`POST /vectors/{collection}/upsert` · Core · Data, Queries & Vectors

Upsert vectors. (Data, Queries & Vectors) POST `/@:projectSlug/vectors/{collection}/upsert`.

## Parameters

| Name | In | Required | Description |
|---|---|---|---|
| `collection` | path | yes | The derived collection's name. It starts with `_`. Example: `"_context_chunks"`. |
| `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 |
|---|---|---|---|
| `rows` | array of object | yes | The chunks to write, 1 to 1,000 per request, all with the same vector dimension. |
| `rows.[]id` | string | no | The chunk's id. An existing id is overwritten (every column replaced). Omitted: a new `rec_...` id is minted. |
| `rows.[]vector` | array of number | yes | The pre-computed embedding. Every row in the request, and every row already in the collection, must have the same dimension: the first upsert fixes it. |
| `rows.[]_source` | object | yes | Where the chunk came from. Stored as sent and returned by search; filter on it with `source.<key>`. |
| `rows.[]_source.handle` | string | yes | An opaque handle for the source document the chunk came from. |
| `rows.[]_source.chunkIndex` | integer | yes | The chunk's position within its source, from 0. |
| `rows.[]text` | string | no | The chunk's source text, returned by search. On a per-subject collection it is encrypted at rest under the subject's key. Never filterable. |
| `rows.[]metadata` | object | no | Your own filter keys for the chunk, stored as a JSON object (default `{}`) and returned by search as sent. Keys are yours; values may be any JSON value. Only keys made of letters, digits and `_` can be filtered on (`metadata.<key>`), and a filter compares the value's text form, so keep filter values to strings, numbers and booleans. A `model` key, when present, is also returned as the result's `model`. There is no per-key or per-object limit beyond the 2 MiB request size; don't put source text here (use `text`). |
| `rows.[]subjectIds` | array of string | no | The end user (`eu_...`) the chunk's content belongs to: exactly one. Required on a per-subject collection (400 `K9O_VECTOR_SUBJECT_ATTRIBUTION_REQUIRED` without it) and refused on a collection declared `shared`. An end user calling for themselves must name themselves. |

## Responses

- **200**: Success

| Field | Type | Required | Description |
|---|---|---|---|
| `data` | object | yes | What the upsert wrote. |
| `data.upserted` | integer | yes | How many chunks were written (inserted or overwritten): the number of rows sent. |
| `data.rows` | array of object | yes | One entry per row sent, in the order sent. |
| `data.rows.[]id` | string | yes | The chunk's id: the one sent, or the `rec_...` id minted for it. |
| `data.rows.[]contentHash` | object | yes | A keyed hash of the chunk's `text`, computed by the platform: 64 lowercase hex. It is the same for the same text in the same collection, so a caller can skip re-embedding a chunk whose hash is unchanged, and it can't be compared across collections or projects. Null when the chunk has no `text`, or when the project's keys are held in the customer's own cloud KMS. Filterable as `contentHash`. |
| _one of_ | | |  |
| _or_ | | |  |
| `data.collection` | string | yes | The derived collection written. It is created on the first upsert. |
| `meta` | object | yes | Request metadata: request id, trace id, timing and cost. |


## Errors

| HTTP | Code | When |
|---|---|---|
| 400 | `K9O_INVALID_FIELD_VALUE` | The project identifier in the request path is not valid. |
| 403 | `K9O_RESERVED_COLUMN_WRITE_PROHIBITED` | Vector upserts are only allowed into an extension's own '_'-prefixed derived collection; this collection name is not one. |
| 400 | `K9O_INVALID_VECTOR_DIMENSION` | All vectors in a single upsert batch must share the same dimension as the first row. |
| 400 | `K9O_VECTOR_SUBJECT_ATTRIBUTION_FORBIDDEN` | This collection is declared residencyClass='shared' (no data subject), so rows must not carry subjectIds; remove them or declare the collection per-subject. |
| 400 | `K9O_VECTOR_SUBJECT_ATTRIBUTION_REQUIRED` | Each vector row must carry subjectIds identifying the content-subject the chunk derives from, so it can later be sealed and erased. |
| 403 | `K9O_IAM_POLICY_DENIED` | The caller's project role can read vector data but not change it (reason read_only_role): only admin, editor, member and author roles write, as for storage objects. |
| 409 | `K9O_BYOK_REGION_MISMATCH` | A chunk belongs to a data subject, and the project's customer-managed key does not cover the region it is written in. Nothing was written, and the chunk text is never sealed under a platform key. |
| 503 | `K9O_KEY_SERVICE_UNAVAILABLE` | The keyed content hash for the chunks couldn't be computed, so nothing was written (context.reason content_hash_unavailable). Retry shortly. |

## Code samples

### cURL

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

### fetch

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

### k9o CLI

```bash
k9o vectors upsert <collection> --rows '<json array>' --rows-file <path|->
```

### @k9o/core

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

### k9o

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

### sdk-go

```go
// ctx := context.Background()
// client, _ := k9o.NewClient(k9o.ClientOpts{OrgSlug: "playground", ProjectSlug: "storefront", APIKey: "k9o_…"})
data, _, kerr := client.Vectors.Upsert(ctx, "<collection>", 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.vectors.upsert("<collection>", json!({})).await?
```

### dev.k9o:core

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

### K9o.Core

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

### k9o

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

### k9o/sdk

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


Canonical page: https://docs.kilimanjaro.dev/api-playground/core/upsert-vectors
