---
title: "IdentityHound API: API keys"
description: "Create an API key, list API keys, rotate an API key, or revoke an API key, with the IdentityHound API."
url: "https://agentpost.tools/docs/identityhound/reference/api-keys"
kind: reference
---

# API keys

A signed-in principal’s own credential. A key may be restricted to workspaces and permissions, which caps what it can do.

[Principals](/docs/identityhound/concepts/principals.md) explains which principals hold API keys, and [Access checks](/docs/identityhound/concepts/access-checks.md) how a key’s restrictions cap it.

Paths are relative to `https://api.agentpost.tools/v1/identity`, and every request follows the [API basics](/docs/platform/api-basics.md). [Who may call](/docs/identityhound/reference.md#who-may-call) explains each kind of caller.

## Create an API key

`POST /principals/:id/api-keys`

Issues an API key. The token is in the response once.

-   **Who may call:** Your server or a signed-in principal.
-   **Access:** Only the principal or your server.
-   **Credential:** the examples send your project’s `dev` secret key, `sk_test_…`, as `Authorization: Bearer <token>`.
-   **SDK:** `client.apiKeys.create(principalId, input)`
-   **Idempotency-Key:** optional. [Retrying a write](/docs/platform/api-basics.md#retrying-a-write) has the rule.

### Path parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | Yes | A principal ID, `prn_…`. The `id` of a [`Principal`](/docs/identityhound/reference/principals.md#the-principal-object). |

### Body

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `name` | `string` | Yes | 1–120 characters. |
| `restrictions` | `object` | No |  |
| `restrictions.workspaceIds` | `string[]` | No | At least 1. |
| `restrictions.permissions` | `string[]` | No | At least 1. |
| `expiresAt` | `string` | No | An ISO 8601 date and time. |

### Response

`201 Created`, with [`ApiKeyCreated`](/docs/identityhound/reference/api-keys.md#the-apikeycreated-object).

### Errors

-   [`forbidden`](/docs/identityhound/errors.md#forbidden) (403)
-   [`not_found`](/docs/identityhound/errors.md#not_found) (404)

Any operation can also answer with the codes [Conventions](/docs/identityhound/reference.md#conventions) lists.

```bash tab="curl"
curl -X POST https://api.agentpost.tools/v1/identity/principals/prn_01JDWRHCP925GR1HC5AP0MCFKH/api-keys \
  -H "Authorization: Bearer sk_test_…" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Report assistant",
    "restrictions": {
      "workspaceIds": [
        "wsp_01JDGKQN2K4RF0M0Z2HBASFA62"
      ],
      "permissions": [
        "report:view",
        "report:edit"
      ]
    }
  }'
```

```ts tab="SDK"
import { createClient } from '@agentpost-tools/identityhound-sdk';

const client = createClient({ token: 'sk_test_…' });
const { apiKey, token } = await client.apiKeys.create(
  'prn_01JDWRHCP925GR1HC5AP0MCFKH',
  {
    name: 'Report assistant',
    restrictions: {
      workspaceIds: ['wsp_01JDGKQN2K4RF0M0Z2HBASFA62'],
      permissions: ['report:view', 'report:edit'],
    },
  },
);
```

```json title="Response"
{
  "apiKey": {
    "id": "key_01JD3F215PNGEK0WH9BSJZRD81",
    "principalId": "prn_01JDWRHCP925GR1HC5AP0MCFKH",
    "name": "Report assistant",
    "prefix": "agp_ak_acme_3f9a1c2b7d4e",
    "restrictions": {
      "workspaceIds": ["wsp_01JDGKQN2K4RF0M0Z2HBASFA62"],
      "permissions": ["report:view", "report:edit"]
    },
    "status": "active",
    "createdAt": "2026-11-06T16:45:00.000Z",
    "createdBy": "prn_01JDG9X6KH1ZEF2ZWP185VMW9D"
  },
  "token": "agp_ak_…"
}
```

## List API keys

`GET /principals/:id/api-keys`

A principal’s keys, without their secrets.

-   **Who may call:** Your server or a signed-in principal.
-   **Access:** Only the principal or your server.
-   **Credential:** the examples send your project’s `dev` secret key, `sk_test_…`, as `Authorization: Bearer <token>`.
-   **SDK:** `client.apiKeys.list(principalId)`

### Path parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | Yes | A principal ID, `prn_…`. The `id` of a [`Principal`](/docs/identityhound/reference/principals.md#the-principal-object). |

### Response

`200 OK`, with:

| Name | Type | Present | Description |
| --- | --- | --- | --- |
| `apiKeys` | [`ApiKey`](/docs/identityhound/reference/api-keys.md#the-apikey-object) array | Always |  |

### Errors

-   [`forbidden`](/docs/identityhound/errors.md#forbidden) (403)
-   [`not_found`](/docs/identityhound/errors.md#not_found) (404)

Any operation can also answer with the codes [Conventions](/docs/identityhound/reference.md#conventions) lists.

```bash tab="curl"
curl https://api.agentpost.tools/v1/identity/principals/prn_01JDWRHCP925GR1HC5AP0MCFKH/api-keys \
  -H "Authorization: Bearer sk_test_…"
```

```ts tab="SDK"
import { createClient } from '@agentpost-tools/identityhound-sdk';

const client = createClient({ token: 'sk_test_…' });
const { apiKeys } = await client.apiKeys.list('prn_01JDWRHCP925GR1HC5AP0MCFKH');
```

```json title="Response"
{
  "apiKeys": [
    {
      "id": "key_01JD3F215PNGEK0WH9BSJZRD81",
      "principalId": "prn_01JDWRHCP925GR1HC5AP0MCFKH",
      "name": "Report assistant",
      "prefix": "agp_ak_acme_3f9a1c2b7d4e",
      "restrictions": {
        "workspaceIds": ["wsp_01JDGKQN2K4RF0M0Z2HBASFA62"],
        "permissions": ["report:view", "report:edit"]
      },
      "status": "active",
      "createdAt": "2026-11-06T16:45:00.000Z",
      "createdBy": "prn_01JDG9X6KH1ZEF2ZWP185VMW9D"
    }
  ]
}
```

## Rotate an API key

`POST /api-keys/:id/rotate`

Issues a replacement key. Set `grace` from `0s` to `7d` to choose how long the old key keeps working. `0s` stops it at once. A key is replaced once: rotating a key already rotated answers `conflict`.

-   **Who may call:** Your server or a signed-in principal.
-   **Access:** Only the principal or your server.
-   **Credential:** the examples send your project’s `dev` secret key, `sk_test_…`, as `Authorization: Bearer <token>`.
-   **SDK:** `client.apiKeys.rotate(id, input)`
-   **Idempotency-Key:** optional. [Retrying a write](/docs/platform/api-basics.md#retrying-a-write) has the rule.

### Path parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | Yes | An API key ID, `key_…`. The `id` of a [`ApiKey`](/docs/identityhound/reference/api-keys.md#the-apikey-object). |

### Body

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `grace` | `string` | Yes | How long the old key keeps working, from `0s` to `7d`. Send `0s` to stop it at once. |

### Response

`201 Created`, with [`ApiKeyCreated`](/docs/identityhound/reference/api-keys.md#the-apikeycreated-object).

### Errors

-   [`conflict`](/docs/identityhound/errors.md#conflict) (409)
-   [`forbidden`](/docs/identityhound/errors.md#forbidden) (403)
-   [`not_found`](/docs/identityhound/errors.md#not_found) (404)

Any operation can also answer with the codes [Conventions](/docs/identityhound/reference.md#conventions) lists.

```bash tab="curl"
curl -X POST https://api.agentpost.tools/v1/identity/api-keys/key_01JD3F215PNGEK0WH9BSJZRD81/rotate \
  -H "Authorization: Bearer sk_test_…" \
  -H "Content-Type: application/json" \
  -d '{
    "grace": "1h"
  }'
```

```ts tab="SDK"
import { createClient } from '@agentpost-tools/identityhound-sdk';

const client = createClient({ token: 'sk_test_…' });
const { apiKey, token } = await client.apiKeys.rotate(
  'key_01JD3F215PNGEK0WH9BSJZRD81',
  { grace: '1h' },
);
```

```json title="Response"
{
  "apiKey": {
    "id": "key_01JD6MHWDD1MSKST11GBDWDP7K",
    "principalId": "prn_01JDWRHCP925GR1HC5AP0MCFKH",
    "name": "Report assistant",
    "prefix": "agp_ak_acme_8c1e5f0a9b2d",
    "restrictions": {
      "workspaceIds": ["wsp_01JDGKQN2K4RF0M0Z2HBASFA62"],
      "permissions": ["report:view", "report:edit"]
    },
    "status": "active",
    "createdAt": "2026-11-13T09:30:00.000Z",
    "createdBy": "prn_01JDG9X6KH1ZEF2ZWP185VMW9D",
    "rotatedFrom": "key_01JD3F215PNGEK0WH9BSJZRD81"
  },
  "token": "agp_ak_…"
}
```

## Revoke an API key

`DELETE /api-keys/:id`

Revokes a key at once when the principal has another way to sign in.

-   **Who may call:** Your server or a signed-in principal.
-   **Access:** Only the principal or your server.
-   **Credential:** the examples send your project’s `dev` secret key, `sk_test_…`, as `Authorization: Bearer <token>`.
-   **SDK:** `client.apiKeys.revoke(id)`
-   **Idempotency-Key:** optional. [Retrying a write](/docs/platform/api-basics.md#retrying-a-write) has the rule.

### Path parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | Yes | An API key ID, `key_…`. The `id` of a [`ApiKey`](/docs/identityhound/reference/api-keys.md#the-apikey-object). |

### Response

`204 No Content`, with no body.

### Errors

-   [`conflict`](/docs/identityhound/errors.md#conflict) (409)
-   [`forbidden`](/docs/identityhound/errors.md#forbidden) (403)
-   [`last_sign_in_method`](/docs/identityhound/errors.md#last_sign_in_method) (409)
-   [`not_found`](/docs/identityhound/errors.md#not_found) (404)

Any operation can also answer with the codes [Conventions](/docs/identityhound/reference.md#conventions) lists.

```bash tab="curl"
curl -X DELETE https://api.agentpost.tools/v1/identity/api-keys/key_01JD3F215PNGEK0WH9BSJZRD81 \
  -H "Authorization: Bearer sk_test_…"
```

```ts tab="SDK"
import { createClient } from '@agentpost-tools/identityhound-sdk';

const client = createClient({ token: 'sk_test_…' });
await client.apiKeys.revoke('key_01JD3F215PNGEK0WH9BSJZRD81');
```

```text title="Response"
204 No Content
```

## The ApiKeyCreated object

| Name | Type | Present | Description |
| --- | --- | --- | --- |
| `apiKey` | [`ApiKey`](/docs/identityhound/reference/api-keys.md#the-apikey-object) | Always |  |
| `token` | `string` | Always | The bearer token. Shown once. |

```json title="The ApiKeyCreated object"
{
  "apiKey": {
    "id": "key_01JD3F215PNGEK0WH9BSJZRD81",
    "principalId": "prn_01JDWRHCP925GR1HC5AP0MCFKH",
    "name": "Report assistant",
    "prefix": "agp_ak_acme_3f9a1c2b7d4e",
    "restrictions": {
      "workspaceIds": ["wsp_01JDGKQN2K4RF0M0Z2HBASFA62"],
      "permissions": ["report:view", "report:edit"]
    },
    "status": "active",
    "createdAt": "2026-11-06T16:45:00.000Z",
    "createdBy": "prn_01JDG9X6KH1ZEF2ZWP185VMW9D"
  },
  "token": "agp_ak_…"
}
```

## The ApiKey object

An API key as the API shows it: never with its secret or hash. `prefix` is the public part a holder sees on its copy.

| Name | Type | Present | Description |
| --- | --- | --- | --- |
| `id` | `string` | Always | An API key ID, `key_…`. |
| `principalId` | `string` | Always | A principal ID, `prn_…`. |
| `name` | `string` | Always |  |
| `prefix` | `string` | Always |  |
| `restrictions` | `object` | When set |  |
| `restrictions.workspaceIds` | `string[]` | When set |  |
| `restrictions.permissions` | `string[]` | When set |  |
| `status` | `"active"` or `"revoked"` | Always |  |
| `expiresAt` | `string` | When set | An ISO 8601 date and time. |
| `lastUsedAt` | `string` | When set | An ISO 8601 date and time. |
| `createdAt` | `string` | Always | An ISO 8601 date and time. |
| `createdBy` | `string` | Always | A principal ID, `prn_…`, or `"trusted_caller"`. |
| `revokedAt` | `string` | When set | An ISO 8601 date and time. |
| `revokedBy` | `string` | When set | A principal ID, `prn_…`, or `"trusted_caller"`. |
| `rotatedFrom` | `string` | When set | An API key ID, `key_…`. |
| `graceEndsAt` | `string` | When set | Set on a rotated key: it keeps working until then. |

```json title="The ApiKey object"
{
  "id": "key_01JD3F215PNGEK0WH9BSJZRD81",
  "principalId": "prn_01JDWRHCP925GR1HC5AP0MCFKH",
  "name": "Report assistant",
  "prefix": "agp_ak_acme_3f9a1c2b7d4e",
  "restrictions": {
    "workspaceIds": ["wsp_01JDGKQN2K4RF0M0Z2HBASFA62"],
    "permissions": ["report:view", "report:edit"]
  },
  "status": "active",
  "createdAt": "2026-11-06T16:45:00.000Z",
  "createdBy": "prn_01JDG9X6KH1ZEF2ZWP185VMW9D"
}
```
