# Rotate Key

`POST /vault/keys/{key_id}/rotate` · Keys & Secrets · Keys

Creates a new version of the project's platform KEK and re-wraps every data key under it. Values are never decrypted to do it. If the re-wrap fails, the old version stays active. One rotation or custody change runs at a time per project. Needs `PROJECT_ADMIN`.

## Parameters

| Name | In | Required | Description |
|---|---|---|---|
| `projectId` | query | yes | The project's id. Example: `"3f2c9a4e-8b1d-4c6f-9e2a-7d5b1c0a4e8f"`. |
| `key_id` | path | yes | The project's active KEK. Example: `"vkey_7Rt2Wq9LxP4m"`. |

## Responses

- **200**: The key was rotated: every data key is wrapped under the new version.

| Field | Type | Required | Description |
|---|---|---|---|
| `keyId` | string | yes | The KEK that was rotated (`vkey_*`). Example: `"vkey_7Rt2Wq9LxP4m"`. |
| `newVersion` | integer (int64) | yes | The KEK version active now. Example: `4`. |
| `previousVersion` | integer (int64) | yes | The KEK version active before the re-key. Example: `3`. |
| `status` | string | yes | Always `rotated`: the re-wrap window closed and the flip committed. Example: `"rotated"`. |


## Errors

| HTTP | Description |
|---|---|
| 401 | The request carries no authenticated caller. |
| 403 | The caller isn't a `PROJECT_ADMIN` of the project. |
| 409 | `K9O_KEY_NOT_ACTIVE`: the key isn't the active one. `K9O_KEY_ROTATION_IN_PROGRESS`: a rotation is already running. `K9O_REKEY_CUSTOMER_KEY`: the customer holds the project's keys, so rotate them at the customer's key service. |
| 500 | `K9O_REKEY_FAILED`: the re-wrap failed, and the old version stays active. |
| 501 | `K9O_REKEY_UNSUPPORTED`: the project's key backend can't create a new version. |
| 503 | `K9O_ENVELOPE_KEK_UNAVAILABLE`: the KEK couldn't be reached. Retry later. |

## Code samples

### cURL

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

### fetch

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

### k9o CLI

```bash
k9o keys rotate <key-id>
```

### @k9o/core

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

### k9o

```python
# k9o = create_client(org_slug="playground", project_slug="storefront", api_key="k9o_…")
response = k9o.keys.rotate("<keyId>")
```

### sdk-go

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

### dev.k9o:core

```java
// K9o k9o = K9o.createClient(K9o.Opts.builder().orgSlug("playground").projectSlug("storefront").apiKey("k9o_…").build());
var response = k9o.keys().rotate("<keyId>")
```

### K9o.Core

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

### k9o/sdk

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


Canonical page: https://docs.kilimanjaro.dev/api-playground/keys-secrets/rotate-key
