# Keycard

An API key on a plastic card that slides into a brass reader. For bring-your-own-key and credential screens.

- Category: Money
- Built from: Payment card
- Docs: https://bevel-ui.vercel.app/components/keycard
- Import from: `@/components/ui/keycard`

## Install

```bash
npx shadcn@latest add @bevel/keycard
```

npm dependencies (installed by the CLI): `@base-ui/react`, `cn`.

## Anatomy

```tsx
<KeycardSlot inserted>
  <Keycard>
    <KeycardLabel />
    <KeycardKey value="sk-…" />
  </Keycard>
</KeycardSlot>

<KeycardSwipe onSwipe={useKey}>
  <Keycard>…</Keycard>
</KeycardSwipe>
```

## Parts

| Part | Renders | Description |
| --- | --- | --- |
| `Keycard` | PaymentCard | The card, with a magnetic stripe. Takes PaymentCard's tone. |
| `KeycardLabel` | span | Whose key it is. |
| `KeycardKey` | span | The key, masked to its prefix and last four (read as “ending in”). revealed shows it all. |
| `KeycardSlot` | div | A brass reader. inserted slides the card in; status text on the plate is announced politely. |
| `KeycardSwipe` | div, card as button | A swipe reader: drag the card left to right, or flick it. onSwipe fires when it completes; accepted lights the lamp. Enter or Space swipes from the keyboard. |

## Example: Bring your own key

Switch the breaker and the key goes into the reader: usage bills to it instead of credits.

```tsx
"use client"

import * as React from "react"
import { Breaker } from "@/components/ui/breaker"
import { Keycard, KeycardKey, KeycardLabel, KeycardSlot } from "@/components/ui/keycard"

/** Bring your own key: put it in the reader and usage bills to it instead of credits. */
export function KeycardByok() {
  const [inUse, setInUse] = React.useState(false)
  return (
    <div className="flex flex-col items-center gap-6">
      <KeycardSlot inserted={inUse} status={inUse ? "Billed to your key" : "Billed to credits"}>
        <Keycard>
          <KeycardLabel>Your OpenAI key</KeycardLabel>
          <KeycardKey value="sk-proj-8f2c91d0a7e44b3c9e1f" />
        </Keycard>
      </KeycardSlot>
      <Breaker label="Use my key" checked={inUse} onCheckedChange={setInUse} />
    </div>
  )
}
```

## Example: Swipe to use

Drag the card through the reader, or flick it. A short drag springs back. Keyboard: focus the card, press Enter.

```tsx
"use client"

import * as React from "react"
import { Keycard, KeycardKey, KeycardLabel, KeycardSwipe } from "@/components/ui/keycard"

/** Swipe the key through the reader to switch billing to it; swipe again to switch back. */
export function KeycardSwipeDemo() {
  const [inUse, setInUse] = React.useState(false)
  return (
    <KeycardSwipe
      accepted={inUse}
      onSwipe={() => setInUse((v) => !v)}
      label={inUse ? "Swipe to stop using your key" : "Swipe to use your key"}
      status={inUse ? "Billed to your key" : "Swipe to bill to your key"}
    >
      <Keycard>
        <KeycardLabel>Your OpenAI key</KeycardLabel>
        <KeycardKey value="sk-proj-8f2c91d0a7e44b3c9e1f" />
      </Keycard>
    </KeycardSwipe>
  )
}
```
