# Materialize Collection

`POST /analytics/{datasetSlug}/materialize` · Core · Analytics & Intelligence

Copies rows into the dataset from exactly one source: `fromCollection` (a collection name) or `fromQuery` (a saved Studio query id that reads a single collection). The source is read with the caller's own row-level access. Returns counts (`rowsRead`, `ingested`, `rejected`) and the partitions touched. Copying a collection that holds per-subject personal data into a `shared` dataset is refused (`400 K9O_DATASET_RESIDENCY_SOURCE_MISMATCH`).

Requires the `datasets:write` permission (PROJECT_ADMIN or PROJECT_AUTHOR by default).

Callers must be members of the project (an organization role alone does not grant access); project service tokens and extension tokens cannot call it.

Returns `503 K9O_ANALYTICS_UNAVAILABLE` until the region's analytics storage is provisioned.

## Parameters

| Name | In | Required | Description |
|---|---|---|---|
| `datasetSlug` | path | yes | The Dataset's slug. Example: `"orders"`. |
| `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 |
|---|---|---|---|
| `fromCollection` | string | no | The collection to snapshot into the dataset. Send exactly one of `fromCollection` and `fromQuery`. A collection name: a letter or underscore, then letters, digits or underscores. |
| `fromQuery` | string | no | The saved Studio Query (`stq_...`) whose result to snapshot into the dataset. Send exactly one of `fromCollection` and `fromQuery`. |

## Responses

- **200**: Success

| Field | Type | Required | Description |
|---|---|---|---|
| `data` | object | yes | The snapshot written into the dataset: what was read, written and refused. |
| `data.datasetId` | string | yes | The target dataset's id (`dset_...`). |
| `data.sourceCollection` | string | no | The collection read. Present when the request sent `fromCollection`. |
| `data.fromQuery` | string | no | The Studio Query read (`stq_...`). Present when the request sent `fromQuery`. |
| `data.rowsRead` | integer | yes | Rows read from the source, as the caller's own row-level access allowed. |
| `data.ingested` | integer | yes | Rows written to the dataset. |
| `data.rejected` | integer | yes | Rows the dataset write refused. |
| `data.partitionsTouched` | array of string | yes | The partition paths written, region first, such as `region=eu/occurred_at=2026-08-30`. |
| `data.bytesWritten` | integer | no | Bytes written to storage. Present only when something was written. |
| `meta` | object | yes |  |


## Errors

| HTTP | Code | When |
|---|---|---|
| 401 | `K9O_UNAUTHENTICATED` | The caller is not signed in. |
| 403 | `K9O_ACCESS_DENIED` | The caller is not a member of this project; an organization role alone does not grant access. |
| 404 | `K9O_NOT_FOUND` | The project was not found for this caller (project service tokens and extension tokens cannot call this endpoint). |
| 403 | `K9O_IAM_POLICY_DENIED` | The caller's role or IAM policy does not grant datasets:write. |
| 400 | `K9O_INVALID_FIELD_VALUE` | Send exactly one of `fromCollection` or `fromQuery`, and the source's columns must map onto the dataset. |
| 400 | `K9O_DATASET_QUERY_SOURCE_AMBIGUOUS` | The saved query does not read exactly one collection. |
| 400 | `K9O_DATASET_RESIDENCY_SOURCE_MISMATCH` | The collection holds per-subject personal data and the dataset is `shared`. |
| 404 | `K9O_COLLECTION_NOT_FOUND` | The source collection does not exist. |
| 404 | `K9O_STUDIO_QUERY_NOT_FOUND` | The saved query does not exist. |
| 404 | `K9O_DATASET_NOT_FOUND` | No active dataset with this slug exists in the project. |
| 403 | `K9O_FORBIDDEN` | Platform telemetry datasets are read-only. |
| 503 | `K9O_ANALYTICS_UNAVAILABLE` | The region's analytics storage is not available yet; retry later. |

## Code samples

### cURL

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

### fetch

```javascript
const res = await fetch('https://<org>.api-<region>.k9o.io/@storefront/analytics/<datasetSlug>/materialize', {
  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 analytics materialize <slug> --from-collection <name> --from-query <saved query id>
```

### @k9o/core

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

### k9o

```python
# k9o = create_client(org_slug="playground", project_slug="storefront", api_key="k9o_…")
response = k9o.analytics.materialize("<datasetSlug>", {})
```

### sdk-go

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

### dev.k9o:core

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

### K9o.Core

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

### k9o

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

### k9o/sdk

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


Canonical page: https://docs.kilimanjaro.dev/api-playground/core/materialize-collection
