# Insert Record

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

Inserts a new record into the named user-collection. The platform auto-generates a `rec_<base62>` id if none is supplied. Any caller-supplied `metadata` block is silently stripped before the INSERT reaches Postgres.

Passing `?requireApproval=true` routes the write through the Maker-Checker evaluation engine. If no auto-approve policy matches, the operation is queued as a pending change and the response returns HTTP 202 with `meta.status: "pending_approval"` instead of the committed record.

Errors:
- `K9O_COLLECTION_NOT_FOUND` (404) if the collection does not exist.
- `K9O_CONSTRAINT_VIOLATION` (409) for an id collision without an upsert flag.

## Parameters

| Name | In | Required | Description |
|---|---|---|---|
| `collection` | path | yes | The collection's name. Example: `"products"`. |
| `projectSlug` | path | yes | The project's slug: the path segment after `@`. It is unique within the org. Example: `"storefront"`. |

## Request body

## Responses

- **200**: Success

| Field | Type | Required | Description |
|---|---|---|---|
| `data` | object | yes | The record as stored after the write, keyed by column name: every column of the collection's table, including `id`. Protected columns come back as plaintext, except columns that need a reveal purpose, which come back in their opaque protected form. With `onConflict=ignore` and an existing row, that existing row. An anonymous guest-capture insert gets only `{ id }`. Values are serialised from their Postgres column type: - text, uuid and enum columns as strings - integer, real and double columns as numbers - bigint and numeric columns as strings (to keep precision) - boolean as true/false - timestamp and date columns as ISO 8601 date-time strings - json and jsonb as the stored JSON value - array columns as JSON arrays - bytea as `{"type":"Buffer","data":[...bytes]}` - a SQL NULL as null |
| `meta` | object | yes | Request metadata: cost, timing, request and trace ids. |

- **201**: Created

| Field | Type | Required | Description |
|---|---|---|---|
| `data` | object | yes | The record as stored after the write, keyed by column name: every column of the collection's table, including `id`. Protected columns come back as plaintext, except columns that need a reveal purpose, which come back in their opaque protected form. With `onConflict=ignore` and an existing row, that existing row. An anonymous guest-capture insert gets only `{ id }`. Values are serialised from their Postgres column type: - text, uuid and enum columns as strings - integer, real and double columns as numbers - bigint and numeric columns as strings (to keep precision) - boolean as true/false - timestamp and date columns as ISO 8601 date-time strings - json and jsonb as the stored JSON value - array columns as JSON arrays - bytea as `{"type":"Buffer","data":[...bytes]}` - a SQL NULL as null |
| `meta` | object | yes | Request metadata: cost, timing, request and trace ids. |

- **202**: Accepted

| Field | Type | Required | Description |
|---|---|---|---|
| `data` | object | yes | The write was queued for approval (HTTP 202) because a bound approval policy requires it, or `requireApproval=true` was passed. It expires after 28 days unless decided. |
| `data.id` | string | yes | The pending change's id (`apr_...`). Approve or reject it through the pending-approvals API. |
| `data.status` | enum ("pending_approval") | yes | Always `pending_approval`: nothing has been written. The write runs when the change is approved. |
| `data.collection` | string | yes | The collection the change targets. |
| `data.operation` | enum ("insert", "update", "delete") | yes | The write that was queued. |
| `data.makerId` | string | yes | Who proposed the change: the accountable originator on a delegated call, otherwise the caller (a user id, or `tok_...` for a token). |
| `data.proposedPayload` | object | yes | The request body as sent, applied when the change is approved: column name to value. Empty for a delete. |
| `data.targetRecordId` | object | yes | The record an update or delete targets; null for an insert. |
| _one of_ | | |  |
| _or_ | | |  |
| `meta` | object | yes | Request metadata (cost, timing, request and trace ids), plus `approval`. |
| `meta.approval` | object | yes | Marks the answer as queued for approval. |
| `meta.approval.status` | enum ("pending_approval") | yes |  |


## Errors

| HTTP | Code | When |
|---|---|---|
| 401 | `K9O_SESSION_EXPIRED` | Your session has expired; sign in again to insert records. |
| 401 | `K9O_INVALID_CREDENTIALS` | The credentials provided are not valid; the insert request was rejected. |
| 403 | `K9O_ACCESS_DENIED` | Your role does not permit inserting records into this collection. |
| 404 | `K9O_COLLECTION_NOT_FOUND` | The target collection does not exist in this project. |
| 409 | `K9O_ALREADY_EXISTS` | The record violates a unique constraint, for example a record with this id already exists in the collection. |
| 403 | `K9O_RESERVED_COLUMN_WRITE_PROHIBITED` | The request payload contains a reserved column name (prefixed with '_') that may only be written by the platform. |
| 422 | `K9O_MALWARE_DETECTED` | A file included in the request failed the malware scan and was not stored. |
| 409 | `K9O_BYOK_REGION_MISMATCH` | The record has an encrypted field for a data subject, and the project's customer-managed key does not cover the region the record is written in. Nothing was written, and the field is never sealed under a platform key. |

## Code samples

### cURL

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

### fetch

```javascript
const res = await fetch('https://<org>.api-<region>.k9o.io/@storefront/data/products', {
  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 data insert <collection> --data <json|@file|->
```

### @k9o/core

```typescript
// const k9o = new K9o({ orgSlug: 'playground', projectSlug: 'storefront', apiKey: 'k9o_…' })
const { data } = await k9o.from('products').insert({})
```

### k9o

```python
# k9o = create_client(org_slug="playground", project_slug="storefront", api_key="k9o_…")
response = k9o.from_("products").insert({})
```

### sdk-go

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

### dev.k9o:core

```java
// K9o k9o = K9o.createClient(K9o.Opts.builder().orgSlug("playground").projectSlug("storefront").apiKey("k9o_…").build());
var response = k9o.from("products").insert(Map.of())
```

### K9o.Core

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

### k9o

```ruby
# k9o = K9o.create_client(org_slug: "playground", project_slug: "storefront", api_key: "k9o_…")
response = k9o.from("products").insert({  })
```

### k9o/sdk

```php
// $k9o = K9oClient::create(['org_slug' => 'playground', 'project_slug' => 'storefront', 'api_key' => 'k9o_…']);
$response = $k9o->from('products')->insert([])
```

### K9o

```swift
// let k9o = try K9o.createClient(orgSlug: "playground", projectSlug: "storefront", apiKey: "k9o_…")
let result = try await k9o.from("products").insert({})
```

### core-kotlin

```kotlin
// val k9o = createClient { orgSlug = "playground"; projectSlug = "storefront"; apiKey = "k9o_…" }
val result = k9o.from("products").insert({})
```

### k9o (Flutter)

```dart
// final k9o = K9o.createClient(orgSlug: 'playground', projectSlug: 'storefront', apiKey: 'k9o_…');
final result = await k9o.from('products').insert({})
```

### @k9o/react-native

```javascript
// const k9o = createClient({ orgSlug: 'playground', projectSlug: 'storefront', apiKey: 'k9o_…' })  // RN, secure storage
const { data } = await k9o.from('products').insert({})
```


Canonical page: https://docs.kilimanjaro.dev/api-playground/core/insert-record
