QuantumNous/new-api · error · Error

Failed to load API keys

Error message

Failed to load API keys

What it means

Thrown by fetchActiveChatKey() when getApiKeys({p:1,size:50}) responds with success falsy and no message. The chat-link feature needs an enabled API key, and this is the first step (listing keys) failing — usually an auth or permission problem rather than 'no keys', which has its own error (19).

Source

Thrown at web/src/features/chat/hooks/use-active-chat-key.ts:28

but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.

For commercial licensing, please contact support@quantumnous.com
*/
import { useQuery } from '@tanstack/react-query'

import { fetchTokenKey, getApiKeys } from '@/features/keys/api'
import { API_KEY_STATUS } from '@/features/keys/constants'
import { useAuthStore } from '@/stores/auth-store'

export async function fetchActiveChatKey() {
  const result = await getApiKeys({ p: 1, size: 50 })
  if (!result.success) {
    throw new Error(result.message || 'Failed to load API keys')
  }

  const items = result.data?.items ?? []
  const active = items.find((item) => item.status === API_KEY_STATUS.ENABLED)
  if (!active) {
    throw new Error('No enabled API keys found. Create or enable one first.')
  }

  const keyResult = await fetchTokenKey(active.id)
  if (!keyResult.success || !keyResult.data?.key) {
    throw new Error(keyResult.message || 'Failed to load API key')
  }

  return `sk-${keyResult.data.key}`
}

/**
 * Get the currently active API key for chat links

View on GitHub (pinned to e2c7aa7b10)

Solutions

  1. Check the GET /api/token/ request in DevTools — a 401 means the login expired; sign in again.
  2. Reload the page after the backend is healthy.
  3. If the response is a legitimate error envelope, surface its message in the UI.
  4. Consider treating auth-expiry specially so the user is redirected to login instead of seeing this error.
Defensive patterns

Strategy: try-catch

Validate before calling

const auth = useAuthStore.getState()
if (!auth.isAuthenticated) {
  redirectToLogin()
  return
}

Type guard

const isKeyListSuccess = (
  r: unknown
): r is { success: true; data: { items: unknown[] } } =>
  typeof r === 'object' && r !== null && (r as { success?: unknown }).success === true

Try / catch

try {
  const key = await fetchActiveChatKey()
  return key
} catch (e) {
  if (/load/i.test(getErrorMessage(e))) {
    invalidateAuthAndRedirect() // likely expired session
  }
  throw e
}

Prevention

When it happens

Trigger: GET /api/token/ listing failing due to an expired login session; backend error; network envelope with success:false; user with no token-list permission.

Common situations: Chat playground opened in a background tab after the session expired; backend restarting when the page loaded; response interceptor converting a 401 into {success:false} without a message.

Related errors


AI-assisted analysis of QuantumNous/new-api@e2c7aa7b10 (2026-08-15). Data as JSON: /api/errors/79c4a0e59d539598. Report an issue: GitHub.