'use client' // Settings → API Keys: personal long-lived "sb_..." Bearer credentials for // machine clients — the superbrain-mcp server (federated mcpserver gateway, // Claude/any MCP client) or plain REST scripts pointed at the Go API. The // plaintext is shown exactly once, right after creation; list rows carry // only the display prefix (write-once secret, house pattern). Plan gating // is the server's job: `allowed` comes from GET /api/keys and a 403 on // create surfaces its message — the UI never guesses entitlements. import { useCallback, useEffect, useState } from 'react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Badge } from '@/components/ui/badge' import { SafeDate } from '@/components/safe-format' import { KeyRound, Loader2, Plus, Trash2, Copy, Check } from 'lucide-react' import { toast } from 'sonner' type ApiKey = { id: string; name: string; prefix: string createdAt: string; lastUsedAt: string | null } async function api(path: string, init?: RequestInit): Promise { return fetch(path, { headers: { 'Content-Type': 'application/json' }, cache: 'no-store', ...init }) } export function ApiKeysSettings() { const [keys, setKeys] = useState([]) const [allowed, setAllowed] = useState(false) const [loading, setLoading] = useState(true) const [creating, setCreating] = useState(false) const [name, setName] = useState('') // The one-and-only plaintext reveal; cleared on the next reload. const [newKey, setNewKey] = useState(null) const [copied, setCopied] = useState(false) // Promise-chain loading (house pattern: no synchronous setState in // effects — the SSR lint refuses cascading renders). const reload = useCallback(() => api('/api/keys') .then(r => r.json()) .then(data => { setKeys(Array.isArray(data?.keys) ? data.keys : []) setAllowed(data?.allowed === true) }) .catch(() => { toast.error('Could not load API keys') }) .finally(() => { setLoading(false) }), []) useEffect(() => { reload() }, [reload]) const create = async () => { const trimmed = name.trim() if (!trimmed) { toast.error('Give the key a name'); return } setCreating(true) try { const res = await api('/api/keys', { method: 'POST', body: JSON.stringify({ name: trimmed }) }) const data = await res.json().catch(() => null) if (res.ok && data?.key) { setNewKey(data.key) setCopied(false) setName('') toast.success('API key created — copy it now, it is only shown once') await reload() } else { toast.error(data?.error ?? 'Could not create the key') } } catch { toast.error('Could not create the key') } finally { setCreating(false) } } const revoke = async (key: ApiKey) => { try { const res = await api(`/api/keys/${key.id}`, { method: 'DELETE' }) if (res.ok) { if (newKey && key.prefix === newKey.slice(0, key.prefix.length)) setNewKey(null) toast.success(`Revoked ${key.name}`) await reload() } else { const data = await res.json().catch(() => null) toast.error(data?.error ?? 'Could not revoke the key') } } catch { toast.error('Could not revoke the key') } } const copyKey = async () => { if (!newKey) return try { await navigator.clipboard.writeText(newKey) setCopied(true) toast.success('Copied to clipboard') } catch { toast.error('Copy failed — select the key text manually') } } if (loading) { return (
Loading API keys…
) } return (

API Keys

Long-lived credentials for machine clients: the Superbrain MCP server (AI tools such as Claude or your federated core’s gateway) and REST scripts. Keys authenticate against the Go API directly (Authorization: Bearer sb_…); the browser session proxy does not forward them.

{!allowed && (
API keys are not available on your current plan.
)} {newKey && (

Copy this key now — it is only shown once:

{newKey}
)} {allowed && (
) => setName(e.target.value)} placeholder="Key name (e.g. mcpserver, backup script)" maxLength={100} onKeyDown={(e: React.KeyboardEvent) => { if (e.key === 'Enter') create() }} />
)} {keys.length === 0 ? (

No API keys yet.

) : (
    {keys.map(k => (
  • {k.name} {k.prefix}…

    Created {' · '} {k.lastUsedAt ? <>Last used : 'Never used'}

  • ))}
)}

Revoking deletes the key immediately. Keys are not affected by “sign out everywhere” — revoke them here.

) }