# Apply Migrations

`POST /migrations/apply` · Core · Storage & Schema

Applies all pending migration files to the project's schema, executing each atomically in a Postgres transaction. Corresponds to the `k9o migration apply` CLI command and uses a time-bounded `_admin` DB credential revoked after the run completes.

Concurrent apply calls are serialised via a Postgres advisory lock. A second call that cannot acquire the lock within 60 seconds returns `K9O_MIGRATION_CONFLICT` (409).

Schema changes that introduce undefined-policy state are rejected with `K9O_MIGRATION_POLICY_VALIDATION_FAILED` (400) unless the migration declares `noPolicy: true`.

Requires `PROJECT_ADMIN`.

## Parameters

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

## Responses

- **200**: Success

| Field | Type | Required | Description |
|---|---|---|---|
| `data` | object | yes | Which of the submitted migrations ran and which were skipped. |
| `data.applied` | array of object | yes | The migrations applied by this call, in the order they ran. |
| `data.applied.[]version` | integer | yes | The migration's version, an integer from 1 to 2147483647. Versions apply in ascending order. |
| `data.applied.[]slug` | string | yes | The migration's slug, as given when it was submitted. |
| `data.applied.[]checksum` | string | yes | SHA-256 (hex) of the migration's canonical JSON (`version`, `slug`, `up`, `down`, `policy`). Re-applying a version with a different checksum is refused. |
| `data.skipped` | array of object | yes | The migrations skipped because they were already applied with the same checksum. |
| `data.skipped.[]version` | integer | yes | The migration's version, an integer from 1 to 2147483647. Versions apply in ascending order. |
| `data.skipped.[]slug` | string | yes | The migration's slug, as given when it was submitted. |
| `data.skipped.[]reason` | enum ("already_applied") | yes | Why the migration was skipped. `already_applied`: this version is already in the project with the same checksum. |
| `meta` | object | yes | Request metadata: cost, timing and correlation ids. |


## Errors

| HTTP | Code | When |
|---|---|---|
| 400 | `K9O_INVALID_FIELD_VALUE` | The migrations body is malformed, a column uses the platform-reserved _k9o_ prefix, or a migration op fails to apply; fix the request and retry. |
| 403 | `K9O_ACCESS_DENIED` | Applying migrations requires the PROJECT_ADMIN role. |

## Code samples

### cURL

```bash
curl -X POST 'https://<org>.api-<region>.k9o.io/@storefront/migrations/apply' \
  -H 'Authorization: Bearer $K9O_TOKEN'
```

### fetch

```javascript
const res = await fetch('https://<org>.api-<region>.k9o.io/@storefront/migrations/apply', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${process.env.K9O_TOKEN}`
  },
});
const { data, meta } = await res.json();
```

### k9o CLI

```bash
k9o migration apply <file>
```

### @k9o/core

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

### k9o

```python
# k9o = create_client(org_slug="playground", project_slug="storefront", api_key="k9o_…")
response = k9o.migrations.apply()
```

### sdk-go

```go
// ctx := context.Background()
// client, _ := k9o.NewClient(k9o.ClientOpts{OrgSlug: "playground", ProjectSlug: "storefront", APIKey: "k9o_…"})
data, _, kerr := client.Migrations.Apply(ctx)
```

### k9o

```rust
// let k9o = Client::new(ClientOpts { org_slug: "playground".into(), project_slug: "storefront".into(), api_key: "k9o_…".into() })?;
let resp = k9o.migrations.apply().await?
```

### dev.k9o:core

```java
// K9o k9o = K9o.createClient(K9o.Opts.builder().orgSlug("playground").projectSlug("storefront").apiKey("k9o_…").build());
var response = k9o.migrations().apply()
```

### K9o.Core

```c#
// var k9o = K9oClient.Create(new K9oOptions { OrgSlug = "playground", ProjectSlug = "storefront", ApiKey = "k9o_…" });
var response = await k9o.Migrations.ApplyAsync()
```

### k9o

```ruby
# k9o = K9o.create_client(org_slug: "playground", project_slug: "storefront", api_key: "k9o_…")
response = k9o.migrations.apply()
```

### k9o/sdk

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


Canonical page: https://docs.kilimanjaro.dev/api-playground/core/apply-migrations
