# API keys API

> Create, list and revoke API keys, and check which account, organization or user a key acts for. A new key is shown once.

Part of the AGNT API reference. AGNT (https://agnt.ai) is the developer platform behind OpenAssistant (https://openassistant.us). Docs index: https://agnt.ai/llms.txt. Web page: https://agnt.ai/reference/api/api-keys

Base URL: `https://api.agnt.ai`. Auth: `Authorization: Bearer ak_live_...`.

## GET /me

Retrieve the calling key

Returns the account, organization, user and scopes the API key acts for. Use it to check a key before you make other calls.

**Responses**

- `200`: Key context
  - `ok` (boolean)
  - `accountId` (string)
  - `email` (string | null): The key's user, for a user-scoped key.
  - `orgId` (string | null): Set for an organization-scoped key.
  - `userId` (string | null): Set for a user-scoped key.
  - `scopes` (string[])
- `401`: Unknown, revoked or expired API key.

```bash
curl 'https://api.agnt.ai/me' \
  -H "Authorization: Bearer ak_live_..."
```

Example response:

```json
{
  "ok": true,
  "accountId": "6660c4a1f2e3b7d8a9c01234",
  "email": "jane@harborview.co",
  "orgId": "6660c4a1f2e3b7d8a9c01234"
}
```

## GET /api-keys

List API keys

Returns API keys on your account, newest first, with only the prefix of each key. With `X-User-Email`, returns only that user's keys. `resource=organization` requires an organization admin.

**Query parameters**

- `page` (integer)
- `perPage` (integer): Default 20, max 100
- `resource` (enum): Organization admin view One of: `organization`.
- `scope` (enum): With resource=organization: user lists per-user keys; otherwise organization-level keys One of: `account`, `user`.

**Responses**

- `200`: Keys
  - `ok` (boolean)
  - `apiKeys` (object[])
    - `id` (string, required)
    - `account` (string, required)
    - `organization` (string | null)
    - `user` (string | null)
    - `label` (string, required)
    - `prefix` (string, required)
    - `scopes` (string[], required)
    - `status` (string, required)
    - `lastUsedAt` (string | null)
    - `expiresAt` (string | null)
    - `createdAt` (string, required)
  - `page` (integer)
  - `perPage` (integer)
  - `total` (integer)
- `403`: resource=organization without organization admin role

```bash
curl 'https://api.agnt.ai/api-keys' \
  -H "Authorization: Bearer ak_live_..."
```

Example response:

```json
{
  "ok": true,
  "apiKeys": [
    {
      "id": "6660c4a1f2e3b7d8a9c01234",
      "account": "account",
      "label": "label",
      "prefix": "prefix",
      "scopes": [
        "scope"
      ],
      "status": "status"
    }
  ],
  "page": 1,
  "perPage": 20
}
```

## POST /api-keys

Create an API key

Creates an API key and returns its full value once, in `apiKey.key`. Without `userId`, an account key creates an account-wide key; with `userId`, the new key acts as that user. Creating a key for another user requires an organization admin.

**Request body** (JSON)

- `label` (string, required): Name for the key
- `userId` (string): User the key acts as. Omit for an account-wide key (organization-scoped when an organization admin creates it)
- `scopes` (string[]): Stored with the key; empty means no restriction
- `expiresAt` (string)

**Responses**

- `201`: Created
  - `ok` (boolean)
  - `apiKey` (object)
    - `id` (string, required): Key id
    - `account` (string)
    - `organization` (string | null): Set for an organization-scoped key
    - `user` (string | null): The user the key acts as; null for an account-wide key
    - `label` (string)
    - `prefix` (string, required): The first characters of the key, shown in lists
    - `scopes` (string[])
    - `status` (enum) One of: `active`, `revoked`.
    - `lastUsedAt` (string | null)
    - `expiresAt` (string | null)
    - `createdAt` (string)
    - `key` (string, required): The full key (ak_live_...). Returned only in this response and never again: only a hash is stored. Store it now.
- `400`: label missing
- `403`: Not allowed to create this kind of key
- `404`: User or organization not in your account
- `428`: The X-User-Email user has not finished setup

```bash
curl 'https://api.agnt.ai/api-keys' \
  -X POST \
  -H "Authorization: Bearer ak_live_..." \
  -H "Content-Type: application/json" \
  -d '{
  "label": "label"
}'
```

Example response:

```json
{
  "ok": true,
  "apiKey": {
    "id": "6660c4a1f2e3b7d8a9c01234",
    "key": "ak_live_…",
    "prefix": "prefix"
  }
}
```

## POST /api-keys/{keyId}/revoke

Revoke an API key

Disables an API key immediately. A user can revoke their own keys; revoking another user's key requires an admin of the key's organization or an account key.

**Path parameters**

- `keyId` (string, required)

**Responses**

- `200`: Revoked key
  - `ok` (boolean)
  - `apiKey` (object)
    - `id` (string, required)
    - `account` (string, required)
    - `organization` (string | null)
    - `user` (string | null)
    - `label` (string, required)
    - `prefix` (string, required)
    - `scopes` (string[], required)
    - `status` (string, required)
    - `lastUsedAt` (string | null)
    - `expiresAt` (string | null)
    - `createdAt` (string, required)
- `400`: Already revoked
- `403`: Organization admin role required
- `404`: API key not found

```bash
curl 'https://api.agnt.ai/api-keys/<keyId>/revoke' \
  -X POST \
  -H "Authorization: Bearer ak_live_..."
```

Example response:

```json
{
  "ok": true,
  "apiKey": {
    "id": "6660c4a1f2e3b7d8a9c01234",
    "account": "account",
    "label": "label",
    "prefix": "prefix",
    "scopes": [
      "scope"
    ],
    "status": "status"
  }
}
```

## DELETE /api-keys/{keyId}

Delete an API key

Permanently deletes a revoked API key. Revoke the key first. Requires the same permission as revoking it.

**Path parameters**

- `keyId` (string, required)

**Responses**

- `200`: Deleted
  - `ok` (boolean)
- `400`: The key is not revoked, or is the live key of a deployed app
- `403`: Organization admin role required
- `404`: API key not found

```bash
curl 'https://api.agnt.ai/api-keys/<keyId>' \
  -X DELETE \
  -H "Authorization: Bearer ak_live_..."
```

Example response:

```json
{
  "ok": true
}
```
