API Keys
Card with a list of API keys, each with scope badge, reveal, copy and revoke actions.
"use client"
import { useState } from "react"
import {
CopyIcon,
EyeIcon,
EyeSlashIcon,
PlusIcon,
TrashIcon,
} from "@phosphor-icons/react"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import {
Card,
CardAction,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
const COPY = {
title: "API keys",
description:
"Keys grant access to the Meridian API. Treat them like passwords.",
create: "Create key",
newName: (n: number) => `New key ${n}`,
newScope: "Read and write",
justNow: "Just now",
empty: "No keys. Create one to start calling the API.",
}
const INITIAL_KEYS = [
{
id: "k1",
name: "Production server",
secret: "mrd_live_9f2kQ7xT4vLb81Zc",
scope: "Read and write",
used: "2 minutes ago",
},
{
id: "k2",
name: "CI pipeline",
secret: "mrd_live_3aPw0sHn6RdE55Yu",
scope: "Read only",
used: "Yesterday",
},
{
id: "k3",
name: "Analytics export",
secret: "mrd_test_7cVh2oJm9KqX10Ba",
scope: "Read only",
used: "Nov 2",
},
]
/** API key list with create, reveal, copy and revoke. A new key is revealed once on creation. */
export default function SettingsApiKeys01() {
const [keys, setKeys] = useState(INITIAL_KEYS)
const [shown, setShown] = useState<string | null>(null)
const [copied, setCopied] = useState<string | null>(null)
// Generate a sample secret and reveal it once, as the real flow does.
function createKey() {
const alphabet =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
const random = Array.from(
{ length: 16 },
() => alphabet[Math.floor(Math.random() * alphabet.length)]
).join("")
const id = `k${Date.now()}`
setKeys((all) => [
{
id,
name: COPY.newName(all.length + 1),
secret: `mrd_live_${random}`,
scope: COPY.newScope,
used: COPY.justNow.toLowerCase(),
},
...all,
])
setShown(id)
}
return (
<Card className="mx-auto w-full max-w-2xl">
<CardHeader>
<CardTitle>{COPY.title}</CardTitle>
<CardDescription>{COPY.description}</CardDescription>
<CardAction>
<Button size="sm" onClick={createKey}>
<PlusIcon data-icon="inline-start" />
{COPY.create}
</Button>
</CardAction>
</CardHeader>
<CardContent>
{keys.length === 0 ? (
<p className="rounded-2xl border border-dashed p-8 text-center text-sm text-muted-foreground">
{COPY.empty}
</p>
) : (
<ul className="divide-y rounded-2xl border">
{keys.map((k) => (
<li
key={k.id}
className="flex flex-col gap-3 p-4 sm:flex-row sm:items-center"
>
<div className="min-w-0 flex-1">
<div className="flex items-center gap-2">
<p className="text-sm font-medium">{k.name}</p>
<Badge variant="secondary">{k.scope}</Badge>
</div>
<p className="mt-1 truncate font-mono text-sm text-muted-foreground">
{shown === k.id
? k.secret
: `${k.secret.slice(0, 9)}${"•".repeat(14)}`}
</p>
<p className="mt-0.5 text-xs text-muted-foreground">
Last used {k.used}
</p>
</div>
<div className="flex gap-1">
<Button
variant="ghost"
size="icon-sm"
aria-label={shown === k.id ? "Hide key" : "Reveal key"}
onClick={() => setShown(shown === k.id ? null : k.id)}
>
{shown === k.id ? <EyeSlashIcon /> : <EyeIcon />}
</Button>
<Button
variant="ghost"
size="icon-sm"
aria-label="Copy key"
onClick={() => {
void navigator.clipboard?.writeText(k.secret)
setCopied(k.id)
setTimeout(() => setCopied(null), 1200)
}}
>
{copied === k.id ? (
<span className="text-xs">Done</span>
) : (
<CopyIcon />
)}
</Button>
<Button
variant="destructive"
size="icon-sm"
aria-label="Revoke key"
onClick={() => setKeys(keys.filter((x) => x.id !== k.id))}
>
<TrashIcon />
</Button>
</div>
</li>
))}
</ul>
)}
</CardContent>
</Card>
)
}
Installation
pnpm dlx shadcn@latest add @01orbital/api-keys-01npx shadcn@latest add @01orbital/api-keys-01yarn dlx shadcn@latest add @01orbital/api-keys-01bunx --bun shadcn@latest add @01orbital/api-keys-01Make it yours
- Its words are in
COPYat the top ofcomponents/blocks/settings/api-keys-01/index.tsx: change them there, not in the markup. INITIAL_KEYS, also at the top of the file, holds its lists and settings.
Built from
npm: @phosphor-icons/react. The CLI installs it for you.
Built from these parts. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:
The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.
Last updated on