Mintplex-Labs/anything-llm · error · Error

res.statusText || "Error generating api key."

Error message

res.statusText || "Error generating api key."

What it means

Thrown by the frontend System API client when POST /system/generate-api-key returns a non-2xx HTTP status. The message uses res.statusText (the HTTP reason phrase) and only falls back to 'Error generating api key.' when the status text is empty (e.g. HTTP/2 responses or synthesized network errors). The surrounding .catch swallows the throw and returns { apiKey: null, error } so callers see a typed failure object, not a thrown exception.

Source

Thrown at frontend/src/models/system.js:570

        if (!res.ok) {
          throw new Error(res.statusText || "Error fetching api key.");
        }
        return res.json();
      })
      .catch((e) => {
        console.error(e);
        return { apiKey: null, error: e.message };
      });
  },
  generateApiKey: async function (data = {}) {
    return fetch(`${API_BASE}/system/generate-api-key`, {
      method: "POST",
      headers: baseHeaders(),
      body: JSON.stringify(data),
    })
      .then((res) => {
        if (!res.ok) {
          throw new Error(res.statusText || "Error generating api key.");
        }
        return res.json();
      })
      .catch((e) => {
        console.error(e);
        return { apiKey: null, error: e.message };
      });
  },
  deleteApiKey: async function (apiKeyId = "") {
    return fetch(`${API_BASE}/system/api-key/${apiKeyId}`, {
      method: "DELETE",
      headers: baseHeaders(),
    })
      .then((res) => res.ok)
      .catch((e) => {
        console.error(e);
        return false;
      });

View on GitHub (pinned to 526360e320)

Solutions

  1. Check the browser DevTools Network tab for the actual status code and server response body on the /system/generate-api-key request.
  2. If 401/403, refresh the session or re-authenticate before regenerating the key.
  3. If 500, inspect the backend server logs around the generate-api-key controller for the real exception.
  4. Render both res.status and the response body in the thrown message so the fallback is no longer information-lossy.

Example fix

// before
if (!res.ok) {
  throw new Error(res.statusText || "Error generating api key.");
}

// after
if (!res.ok) {
  let detail = res.statusText;
  try { const body = await res.json(); detail = body?.message || detail; } catch {}
  throw new Error(detail || `Error generating api key (HTTP ${res.status}).`);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Confirm session is authed and the endpoint is reachable before generating.
async function canGenerate() {
  const r = await fetch(`${API_BASE}/system/ping`, { headers: baseHeaders() });
  return r.ok;
}

Type guard

// Result type guard for the generateApiKey return shape.
function isApiKeyFailure(x): x is { apiKey: null; error: string } {
  return x && x.apiKey === null && typeof x.error === 'string';
}

Try / catch

const result = await System.generateApiKey(payload);
if (result.apiKey === null) {
  showToast(result.error || 'Could not generate API key.');
  return;
}
// use result.apiKey

Prevention

When it happens

Trigger: Calling System.generateApiKey(data) while unauthenticated (401/403), when the backend LLM provider key validation rejects the request (422), when the server errors during key generation (500), or when the network/CORS preflight fails (statusText often empty).

Common situations: Session expired while the user sat on the API-key settings page; the backend has no LLM API key configured; reverse proxy strips the reason phrase; HTTP/2 server returns no statusText so only the fallback message surfaces.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13). Data as JSON: /api/errors/bb91cf4ca1749b7d. Report an issue: GitHub.