# API keys

A workspace's keys with scopes, who made them, last use and expiry; create, rotate with a grace period, and revoke.

> chatcn Pro: needs a Pro license (https://chatcn.dev/pro). Install from the `@chatcn-pro` registry with your license token; setup at https://chatcn.dev/docs/chatcn-pro.

Example: API keys. Four keys: one mid rotation, one healthy, one unused for months and one expired. Create, rotate or revoke. Preview: https://chatcn.dev/preview/api-keys-demo

## Installation

```bash
npx shadcn@latest add @chatcn-pro/api-keys
```

## Usage

```tsx
import { ApiKeys } from "@/components/ui/chatcn/pro/api-keys"

<ApiKeys
  keys={keys}                     // { id, name, prefix, last4, scopes, createdAt, createdBy?, lastUsedAt?, expiresAt?, oldSecretUntil? }
  scopes={[{ id: "chat", label: "Send messages" }, { id: "files", label: "Read files" }]}
  envName="CHATCN_API_KEY"
  onCreate={(input) => createKey(input)}   // returns { secret }
  onRotate={(id) => rotateKey(id)}         // returns { secret }
  onRevoke={(id) => revokeKey(id)}
/>
```

Each key shows its prefix and last four characters, what it can do, who made it and when, when it was last used and when it expires. A key nobody has used for `staleDays` (90 by default) says so on its row and suggests revoking it; an expired key says Expired. Create asks for a name, scopes and an expiry and then shows the secret once with `key-reveal-once`. Rotate makes a new secret and keeps the old one working for `graceHours` (24 by default) so a deploy can switch over, and the row says when the old one stops; revoke says it is immediate and final. Both confirm inside the row with Cancel focused. Keep secrets on your server: this component only ever sees a secret at the moment it is shown.

## Examples

### States

Example: States. No keys yet, and a narrow frame where each key stacks. Preview: https://chatcn.dev/preview/api-keys-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `keys` | `ApiKeyRecord[]` | Described without the secret |
| `scopes` | `ApiKeyScope[]` | `{ id, label, description? }` |
| `expiries` | `ApiKeyExpiry[]` | Choices when creating; default 30 days, 90 days, 1 year, never |
| `onCreate` | `(input) => Promise<{ secret, name? }>` | `{ name, scopes, expiresInDays }` |
| `onRotate`, `onRevoke` |  | Rotate returns the new `{ secret }` |
| `graceHours`, `staleDays` | `number` | 24 and 90 by default |
| `envName` | `string` | Offers a `.env` line when a secret is shown |
| `now` | `Date` | Pins relative times |
| `labels` | `Partial<ApiKeysLabels>` | Every string, for translation |

## Accessibility

- Each key's masked value reads as the prefix and the last four characters.
- Scopes are a labelled list; create uses native checkboxes and radios.
- Confirmations open inside the row with Cancel focused; Escape cancels and focus returns to the menu.

## Motion

None beyond the spinner while an action runs.

Source: https://chatcn.dev/docs/components/api-keys