Skip to main content
401Not retryable

Google Gemini 401 Unauthenticated

Credentials are missing or invalid — wrong API key, or OAuth token problems when going through Vertex AI.

Most likely causes

  1. 1.API key invalid, restricted to wrong APIs, or deleted
  2. 2.Key restriction (HTTP referrer / IP) blocking your server
  3. 3.Vertex path: expired or wrongly-scoped OAuth token

Fix checklist

  • Verify the key in Google AI Studio; regenerate if in doubt
  • Check key restrictions in the Cloud console credentials page
  • For Vertex: refresh application default credentials (gcloud auth)

Retry guidance

Not retryable until credentials are fixed.

// 401 is NOT retryable — surface it and fix the request/config.
const res = await fetch("https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent", {
  method: "POST",
  headers: {
    "content-type": "application/json",
    "x-goog-api-key": process.env.GEMINI_API_KEY!,
  },
  body: JSON.stringify(payload),
});
if (res.status === 401) {
  const body = await res.json();
  // Do not retry: log the provider's message — it names the exact problem.
  throw new Error(`Google Gemini 401: ${JSON.stringify(body.error ?? body)}`);
}

Provider status page: status.cloud.google.com