'use client'; import { useEffect, useState } from 'react'; import { ApiProvider } from '@prisma/client'; type ApiKeyRecord = { id: string; provider: ApiProvider; label: string | null; isActive: boolean; lastUsedAt: string | null; createdAt: string; maskedKey: string; }; const providers: ApiProvider[] = ['NEWSAPI', 'GNEWS', 'NEWSDATA', 'KIMI', 'WEBSEARCH', 'BRAVE']; export default function ApiKeysPage() { const [keys, setKeys] = useState([]); const [loading, setLoading] = useState(true); const [form, setForm] = useState({ provider: 'KIMI' as ApiProvider, key: '', label: '' }); async function fetchKeys() { const res = await fetch('/api/admin/keys'); if (res.ok) setKeys(await res.json()); setLoading(false); } useEffect(() => { fetchKeys(); }, []); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); const res = await fetch('/api/admin/keys', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(form), }); if (res.ok) { setForm({ provider: 'KIMI', key: '', label: '' }); fetchKeys(); } else { const data = await res.json(); alert(data.error || 'Failed to add key'); } } async function toggleActive(id: string, current: boolean) { const res = await fetch(`/api/admin/keys/${id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ isActive: !current }), }); if (res.ok) fetchKeys(); } async function removeKey(id: string) { if (!confirm('Delete this API key?')) return; const res = await fetch(`/api/admin/keys/${id}`, { method: 'DELETE' }); if (res.ok) fetchKeys(); } return (

API Keys

Add New Key

setForm({ ...form, key: e.target.value })} className="w-full px-3 py-2 border rounded-lg dark:bg-neutral-800 dark:border-neutral-700" placeholder="Paste key here" required />
setForm({ ...form, label: e.target.value })} className="w-full px-3 py-2 border rounded-lg dark:bg-neutral-800 dark:border-neutral-700" placeholder="Optional" />
{loading ? (

Loading...

) : (
{keys.map((k) => ( ))} {keys.length === 0 && ( )}
Provider Label Status Added Actions
{k.provider} {k.label || '-'} {k.isActive ? 'Active' : 'Inactive'} {new Date(k.createdAt).toLocaleDateString()}
No API keys configured yet.
)}
); }