---
title: "Rotate a secret key"
description: "Replace a project's secret key without downtime, and stop the old key at once when it leaked."
url: "https://agentpost.tools/docs/platform/guides/rotate-keys"
kind: guide
needs:
  - what: "The `api` helper that sends each request"
    from: "https://agentpost.tools/docs/platform/quickstart.md"
  - what: "Your credential, `token`, as an owner or admin of the workspace that holds the project"
    from: "https://agentpost.tools/docs/platform/guides/sign-up.md"
  - what: "A project, `project`, with a `dev` secret key named `server`"
    from: "https://agentpost.tools/docs/platform/guides/keys.md"
---

# Rotate a secret key

Rotate a secret key on a schedule, and whenever it may have leaked. [Secret keys](/docs/platform/concepts/secret-keys.md#status) describes the values of `status`.

## Before you start

-   The `api` helper that sends each request, from [Platform quickstart](/docs/platform/quickstart.md).
-   Your credential, `token`, as an owner or admin of the workspace that holds the project, from [Start a workspace](/docs/platform/guides/sign-up.md).
-   A project, `project`, with a `dev` secret key named `server`, from [Create a project and issue a key](/docs/platform/guides/keys.md).

This page's code continues the script of the pages above: run its blocks after theirs, in the same file. To run them as a script of their own, first copy each helper from the page that defines it, and set each other value, such as a key or an ID, to one you already have.

## 1\. Find the key

List the project’s keys and pick the active `dev` secret key named `server`. If more than one matches, use the ID of the key your server uses.

```ts
type Key = {
  id: string;
  name: string;
  status: string;
  kind: string;
  environment: string;
};
const { keys } = await api('GET', `/v1/projects/${project.id}/keys`, token);
const server: Key = keys.find(
  (k: Key) =>
    k.name === 'server' &&
    k.status === 'active' &&
    k.kind === 'secret' &&
    k.environment === 'dev',
);
```

## 2\. Rotate the key

This example rotates `server.id` and keeps the old key working for one hour.

```ts
const {
  key,
  record: replacement,
  previous,
} = await api(
  'POST',
  `/v1/projects/${project.id}/keys/${server.id}/rotate`,
  token,
  { grace: '1h' },
);
console.log(key.slice(0, 8), previous.status); // sk_test_ grace
```

1.  Store `key`, the new secret key, where the server reads it.
2.  Deploy the server with it.
3.  The old key keeps working until `previous.graceEndsAt`. [Secret keys](/docs/platform/concepts/secret-keys.md#status) says how long the user tokens it minted keep working.

Send `grace` as a duration string from `"0s"` to `"7d"`. If you leave it out, the API answers [`invalid_request`](/docs/errors.md#invalid_request). A grace of `"0s"` stops the old key at once. With it, send the key’s name in `confirm`, as in `{ grace: '0s', confirm: 'server' }`, or the API answers [`confirmation_required`](/docs/errors.md#confirmation_required). When a rotation answers [`conflict`](/docs/errors.md#conflict), for a reason [Secret keys](/docs/platform/concepts/secret-keys.md#status) gives, rotate the key its `replacedBy` names, or issue a new key if it has no `replacedBy`.

## 3\. Revoke the old key

When the old key leaked, revoke it once the server runs with the new key, even if its grace has ended. Revoking stops the key and every user token it minted at once, and can’t be undone. For a routine rotation, you can let grace finish and skip this step.

```ts
const { record } = await api(
  'DELETE',
  `/v1/projects/${project.id}/keys/${previous.id}`,
  token,
);
console.log(record.status); // revoked
```

Revoking may answer [`not_found`](/docs/errors.md#not_found).

## Check it worked

List the project’s keys again and check the two keys by ID:

```ts
const after = await api('GET', `/v1/projects/${project.id}/keys`, token);
const servers = after.keys.filter(
  (k: Key) => k.id === previous.id || k.id === replacement.id,
);
console.log(servers.map((k: Key) => k.status)); // [ 'revoked', 'active' ]
```

The list is oldest first. The old key is `revoked` and the new one is `active`. If you skipped step 3, the old key shows `grace` or `expired`, as [Secret keys](/docs/platform/concepts/secret-keys.md#status) describes.
