|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { IconKey, IconPlus, IconX, IconCheck } from "../icons"; |
| 3 | + |
| 4 | +interface ApiKeyEntry { |
| 5 | + id: string; |
| 6 | + name: string; |
| 7 | + prefix: string; |
| 8 | + createdAt: string; |
| 9 | +} |
| 10 | + |
| 11 | +export default function ApiKeys({ apiBase }: { apiBase: string }) { |
| 12 | + const [keys, setKeys] = useState<ApiKeyEntry[]>([]); |
| 13 | + const [endpoint, setEndpoint] = useState(""); |
| 14 | + const [newName, setNewName] = useState(""); |
| 15 | + const [creating, setCreating] = useState(false); |
| 16 | + const [newKey, setNewKey] = useState<string | null>(null); |
| 17 | + const [copied, setCopied] = useState(false); |
| 18 | + const [confirmDelete, setConfirmDelete] = useState<string | null>(null); |
| 19 | + |
| 20 | + const fetchKeys = async () => { |
| 21 | + try { |
| 22 | + const res = await fetch(`${apiBase}/api/keys`); |
| 23 | + if (res.ok) { |
| 24 | + const data = await res.json(); |
| 25 | + setKeys(data.keys ?? []); |
| 26 | + setEndpoint(data.endpoint ?? ""); |
| 27 | + } |
| 28 | + } catch { /* proxy down */ } |
| 29 | + }; |
| 30 | + |
| 31 | + useEffect(() => { fetchKeys(); }, []); |
| 32 | + |
| 33 | + const handleCreate = async () => { |
| 34 | + setCreating(true); |
| 35 | + try { |
| 36 | + const res = await fetch(`${apiBase}/api/keys`, { |
| 37 | + method: "POST", |
| 38 | + headers: { "Content-Type": "application/json" }, |
| 39 | + body: JSON.stringify({ name: newName || "default" }), |
| 40 | + }); |
| 41 | + if (res.ok) { |
| 42 | + const data = await res.json(); |
| 43 | + setNewKey(data.key); |
| 44 | + setNewName(""); |
| 45 | + fetchKeys(); |
| 46 | + } |
| 47 | + } finally { |
| 48 | + setCreating(false); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + const handleDelete = async (id: string) => { |
| 53 | + await fetch(`${apiBase}/api/keys`, { |
| 54 | + method: "DELETE", |
| 55 | + headers: { "Content-Type": "application/json" }, |
| 56 | + body: JSON.stringify({ id }), |
| 57 | + }); |
| 58 | + setConfirmDelete(null); |
| 59 | + fetchKeys(); |
| 60 | + }; |
| 61 | + |
| 62 | + const copyKey = () => { |
| 63 | + if (newKey) { |
| 64 | + navigator.clipboard.writeText(newKey); |
| 65 | + setCopied(true); |
| 66 | + setTimeout(() => setCopied(false), 2000); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + return ( |
| 71 | + <section className="page"> |
| 72 | + <h2><IconKey /> API Access</h2> |
| 73 | + <p className="muted"> |
| 74 | + Use generated API keys to access the opencodex proxy from external apps. |
| 75 | + Keys authenticate via <code>Authorization: Bearer ocx_...</code> or <code>x-opencodex-api-key</code> header. |
| 76 | + </p> |
| 77 | + |
| 78 | + <div className="card" style={{ marginTop: "1rem" }}> |
| 79 | + <h3>Endpoint</h3> |
| 80 | + <code className="block">{endpoint || "http://127.0.0.1:10100/v1/responses"}</code> |
| 81 | + <p className="muted small">Compatible with OpenAI Responses API format.</p> |
| 82 | + </div> |
| 83 | + |
| 84 | + {newKey && ( |
| 85 | + <div className="card highlight" style={{ marginTop: "1rem" }}> |
| 86 | + <h3>New Key Created</h3> |
| 87 | + <p className="muted small">Copy this key now — it won't be shown again.</p> |
| 88 | + <div style={{ display: "flex", gap: "0.5rem", alignItems: "center" }}> |
| 89 | + <code className="block" style={{ flex: 1, wordBreak: "break-all" }}>{newKey}</code> |
| 90 | + <button className="btn btn-sm" onClick={copyKey}> |
| 91 | + {copied ? <><IconCheck /> Copied</> : "Copy"} |
| 92 | + </button> |
| 93 | + </div> |
| 94 | + <button className="btn btn-sm" style={{ marginTop: "0.5rem" }} onClick={() => setNewKey(null)}> |
| 95 | + Dismiss |
| 96 | + </button> |
| 97 | + </div> |
| 98 | + )} |
| 99 | + |
| 100 | + <div className="card" style={{ marginTop: "1rem" }}> |
| 101 | + <h3>Generate Key</h3> |
| 102 | + <div style={{ display: "flex", gap: "0.5rem", alignItems: "center" }}> |
| 103 | + <input |
| 104 | + type="text" |
| 105 | + placeholder="Key name (optional)" |
| 106 | + value={newName} |
| 107 | + onChange={e => setNewName(e.target.value)} |
| 108 | + className="input" |
| 109 | + style={{ flex: 1 }} |
| 110 | + /> |
| 111 | + <button className="btn btn-primary" onClick={handleCreate} disabled={creating}> |
| 112 | + <IconPlus /> {creating ? "Creating..." : "Generate"} |
| 113 | + </button> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + |
| 117 | + <div className="card" style={{ marginTop: "1rem" }}> |
| 118 | + <h3>Active Keys ({keys.length})</h3> |
| 119 | + {keys.length === 0 ? ( |
| 120 | + <p className="muted">No API keys yet. Generate one above.</p> |
| 121 | + ) : ( |
| 122 | + <table className="table"> |
| 123 | + <thead> |
| 124 | + <tr><th>Name</th><th>Key</th><th>Created</th><th></th></tr> |
| 125 | + </thead> |
| 126 | + <tbody> |
| 127 | + {keys.map(k => ( |
| 128 | + <tr key={k.id}> |
| 129 | + <td>{k.name}</td> |
| 130 | + <td><code>{k.prefix}</code></td> |
| 131 | + <td>{new Date(k.createdAt).toLocaleDateString()}</td> |
| 132 | + <td> |
| 133 | + {confirmDelete === k.id ? ( |
| 134 | + <span style={{ display: "flex", gap: "0.25rem" }}> |
| 135 | + <button className="btn btn-sm btn-danger" onClick={() => handleDelete(k.id)}>Confirm</button> |
| 136 | + <button className="btn btn-sm" onClick={() => setConfirmDelete(null)}>Cancel</button> |
| 137 | + </span> |
| 138 | + ) : ( |
| 139 | + <button className="btn btn-sm" onClick={() => setConfirmDelete(k.id)}><IconX /></button> |
| 140 | + )} |
| 141 | + </td> |
| 142 | + </tr> |
| 143 | + ))} |
| 144 | + </tbody> |
| 145 | + </table> |
| 146 | + )} |
| 147 | + </div> |
| 148 | + |
| 149 | + <div className="card" style={{ marginTop: "1rem" }}> |
| 150 | + <h3>Usage Example</h3> |
| 151 | + <pre className="block">{`curl ${endpoint || "http://127.0.0.1:10100"}/v1/responses \\ |
| 152 | + -H "Authorization: Bearer ocx_YOUR_KEY_HERE" \\ |
| 153 | + -H "Content-Type: application/json" \\ |
| 154 | + -d '{ |
| 155 | + "model": "gpt-5.4", |
| 156 | + "input": "Hello, world!" |
| 157 | + }'`}</pre> |
| 158 | + </div> |
| 159 | + </section> |
| 160 | + ); |
| 161 | +} |
0 commit comments