# API key setup

Bring your own key, proven before it is saved: a real test call with its reply and timing, then the models the key can reach.

> 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 key setup. A sample key is tested on arrival: the call, its reply and timing, then the models it can use. Preview: https://chatcn.dev/preview/api-key-setup-demo

## Installation

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

## Usage

```tsx
import { ApiKeySetup } from "@/components/ui/chatcn/pro/api-key-setup"

import { apiKeyFormats } from "@/components/ui/chatcn/api-key-input"

<ApiKeySetup
  providers={[
    { id: "anthropic", name: "Anthropic", format: apiKeyFormats.anthropic, keysUrl: "https://console.anthropic.com/settings/keys", testModel: "claude-haiku-5-5" },
    { id: "openai", name: "OpenAI", format: apiKeyFormats.openai, testModel: "gpt-6.1-sol" },
  ]}
  onValidate={(provider, key, { signal }) => checkKey(provider, key, signal)}
  onTest={(provider, key, { signal }) => testCall(provider, key, signal)}
  onListModels={(provider, key, { signal }) => listModels(provider, key, signal)}
  onSave={({ provider, key, models, defaultModel }) => storeKey(…)}
/>
```

Built on the free `api-key-input`, so the key gets the same care: it lives only in the field, masked, with paste cleanup and a format check per provider. Once the key checks out, one small real call runs on its own and shows what was sent, what came back, the status, the time it took and the tokens used, so a key with no credits or the wrong scopes is caught here and not in the first chat. When the call works, the models the key can reach are listed; each can be switched off and one is the default, which always stays one that is on. Save and use unlocks only then. After saving, only the prefix and last four characters are shown. Run every call from your server; the key never needs to touch your client logs.

## Examples

### States

Example: States. Nothing pasted yet, and a valid key whose test call fails because the account has no credits. Preview: https://chatcn.dev/preview/api-key-setup-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `providers` | `ApiKeyProvider[]` | `{ id, name, icon?, format?, keysUrl?, testModel? }` |
| `provider`, `defaultProvider`, `onProviderChange` | `string` | Changing it clears the key |
| `onValidate` | `(provider, key, { signal }) => Promise<ApiKeyValidation>` | Optional live check |
| `onTest` | `(provider, key, { signal }) => Promise<ApiKeyTestResult>` | `{ ok, status?, latencyMs?, output?, tokens?, error? }` |
| `onListModels` | `(provider, key, { signal }) => Promise<ApiKeyModel[]>` | `{ id, name, note?, context?, off?, recommended? }` |
| `testPrompt` | `string` | Default `Reply with the word ready.` |
| `onSave` | `(result) => void | Promise<void>` | `{ provider, key, models, defaultModel }`; throw to show the error |
| `defaultKey` | `string` | A key on mount, tested right away. For previews and edits |
| `labels` | `Partial<ApiKeySetupLabels>` | Every string, for translation |

## Accessibility

- Providers are toggle buttons in a group; models are toggle buttons that report whether they are on.
- The test call's progress and result are status regions; a failed call is an alert.
- Each step is labelled, and the step numbers and checks are decoration.

## Motion

The test result and the model list rise 4 px in over 220 ms, models 40 ms apart. Step nodes change color over 200 ms. Nothing moves under reduced motion.

Source: https://chatcn.dev/docs/components/api-key-setup