{"record":{"id":"4b9581191d18c285","repo":"QuantumNous/new-api","slug":"no-enabled-api-keys-found-create-or-enable-one-fi","errorCode":null,"errorMessage":"No enabled API keys found. Create or enable one first.","messagePattern":"No enabled API keys found\\. Create or enable one first\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"web/src/features/chat/hooks/use-active-chat-key.ts","lineNumber":34,"sourceCode":"\nFor commercial licensing, please contact support@quantumnous.com\n*/\nimport { useQuery } from '@tanstack/react-query'\n\nimport { fetchTokenKey, getApiKeys } from '@/features/keys/api'\nimport { API_KEY_STATUS } from '@/features/keys/constants'\nimport { useAuthStore } from '@/stores/auth-store'\n\nexport async function fetchActiveChatKey() {\n  const result = await getApiKeys({ p: 1, size: 50 })\n  if (!result.success) {\n    throw new Error(result.message || 'Failed to load API keys')\n  }\n\n  const items = result.data?.items ?? []\n  const active = items.find((item) => item.status === API_KEY_STATUS.ENABLED)\n  if (!active) {\n    throw new Error('No enabled API keys found. Create or enable one first.')\n  }\n\n  const keyResult = await fetchTokenKey(active.id)\n  if (!keyResult.success || !keyResult.data?.key) {\n    throw new Error(keyResult.message || 'Failed to load API key')\n  }\n\n  return `sk-${keyResult.data.key}`\n}\n\n/**\n * Get the currently active API key for chat links\n */\nexport function useActiveChatKey(enabled: boolean) {\n  const userId = useAuthStore((state) => state.auth.user?.id)\n\n  return useQuery({\n    queryKey: ['chat-active-key', userId],","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/QuantumNous/new-api/blob/e2c7aa7b102c2075eae2377df3508658d45e88dc/web/src/features/chat/hooks/use-active-chat-key.ts#L16-L52","documentation":"Thrown by fetchActiveChatKey() when the key list loads successfully but none of the first 50 keys has status ENABLED (API_KEY_STATUS.ENABLED). This is an expected account-state condition: the chat feature requires an active sk- key to build chat links, and the current user has none enabled.","triggerScenarios":"New user who never created a token; all tokens disabled or expired; user has more than 50 tokens with the enabled one beyond page 1; admin disabled the user's tokens.","commonSituations":"Fresh account opening the chat playground; tokens auto-disabled by quota exhaustion; keys beyond the hardcoded size:50 window.","solutions":["Go to the API Keys (tokens) page and create a new key, or enable an existing one.","If an admin disabled the keys, resolve that with the admin (quota/billing).","If you legitimately have >50 keys, raise the size parameter or paginate until an enabled key is found.","Surface this message with a direct 'Create key' action in the UI instead of a bare error."],"exampleFix":"// before\nconst result = await getApiKeys({ p: 1, size: 50 })\nconst active = items.find((item) => item.status === API_KEY_STATUS.ENABLED)\nif (!active) throw new Error('No enabled API keys found. Create or enable one first.')\n\n// after — search beyond the first page\nasync function findEnabledKey(): Promise<TokenInfo | undefined> {\n  for (let p = 1; p <= 5; p++) {\n    const r = await getApiKeys({ p, size: 100 })\n    if (!r.success) throw new Error(r.message || 'Failed to load API keys')\n    const hit = (r.data?.items ?? []).find(\n      (item) => item.status === API_KEY_STATUS.ENABLED\n    )\n    if (hit) return hit\n    if ((r.data?.items ?? []).length < 100) return undefined\n  }\n  return undefined\n}","handlingStrategy":"validation","validationCode":"const { data: keyList } = useQuery({\n  queryKey: ['api-keys'],\n  queryFn: () => getApiKeys({ p: 1, size: 100 }),\n})\nconst hasEnabledKey = (keyList?.data?.items ?? []).some(\n  (item) => item.status === API_KEY_STATUS.ENABLED\n)\n// render a 'Create API key' empty-state instead of triggering the error","typeGuard":"const hasEnabledApiKey = (\n  items: { status: number }[] | undefined\n): boolean =>\n  (items ?? []).some((item) => item.status === API_KEY_STATUS.ENABLED)","tryCatchPattern":"try {\n  return await fetchActiveChatKey()\n} catch (e) {\n  if (/No enabled API keys/i.test(getErrorMessage(e))) {\n    return renderCreateKeyPrompt() // guided recovery, not an error\n  }\n  throw e\n}","preventionTips":["Create and enable at least one API key before using chat links","Pre-check for an enabled key and render a create-key empty state","Paginate past 50 keys (or raise size) if the account has many tokens"],"tags":["api-keys","chat","account-state","frontend"],"backgroundTag":null,"analyzedSha":"e2c7aa7b102c2075eae2377df3508658d45e88dc","analyzedAt":"2026-08-15T10:35:18.111Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}