# Bring your own keys

One row per provider: the masked key, whether it works, when it was last used, and a test that runs in place.

> 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: Bring your own keys. Four providers: one working, one revoked, one with no key and one never tested. Test runs in place. Preview: https://chatcn.dev/preview/byok-keys-demo

## Installation

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

## Usage

```tsx
import { ByokKeys } from "@/components/ui/chatcn/pro/byok-keys"

<ByokKeys
  providers={[
    {
      id: "anthropic",
      name: "Anthropic",
      format: apiKeyFormats.anthropic,
      key: { prefix: "sk-ant-", last4: "r0Xt", lastUsedAt },
      status: "ok",
      checkedAt,
      usage: requestsPerDay,          // 14 numbers, oldest first
      usedFor: "Chats on Claude models",
    },
    { id: "google", name: "Google", key: null },
  ]}
  onTest={(provider) => testStoredKey(provider)}
  onSave={(provider, key) => storeKey(provider, key)}
  onRemove={(provider) => deleteKey(provider)}
/>
```

Keys are described, never shown: the prefix and last four characters, with fixed dots between so the length does not leak. Each row says in plain words whether the key works and when it was last checked; a failing key carries its reason on the row (a revoked key, an empty balance), not in a toast that has gone by the time anyone looks. Test runs one real request with the stored key and reports how long it took. Two weeks of use sit beside the last used time, so a key that stopped being used is obvious. Replace opens the free `api-key-input` inside the row, with its paste cleanup and checks; remove asks first and says what will stop working.

## Examples

### States

Example: States. No keys yet, and a narrow frame where use moves under each row. Preview: https://chatcn.dev/preview/byok-keys-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `providers` | `ByokProvider[]` | `{ id, name, icon?, format?, key?, status?, error?, checkedAt?, usage?, usedFor? }` |
| `onTest` | `(provider) => Promise<{ ok, latencyMs?, error? }>` | Shows Test on rows with a key |
| `onValidate` | `(provider, key, { signal }) => Promise<ApiKeyValidation>` | Live check while replacing |
| `onSave` | `(provider, key) => void | Promise<void>` | New or replacement key |
| `onRemove` | `(provider) => void | Promise<void>` | Shows Remove in the menu |
| `now` | `Date` | Pins relative times |
| `labels` | `Partial<ByokKeysLabels>` | Every string, for translation |

## Accessibility

- Each key's state is a status region, so a test result is heard.
- The masked key reads as the prefix and the last four characters.
- The usage bars are an image labelled with the total.
- Remove asks inside the row with Cancel focused; Escape cancels and focus returns to the menu.

## Motion

None beyond the spinner while a test or removal runs.

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