Gemini returns “API key not valid”. Fix which key is sent, then stop retrying 400s
A 400 API_KEY_INVALID means Gemini rejected the key you sent, not a rate limit. Check the env var and quotes, stop retrying, and rotate a leaked or blocked key. Short answer: 5 steps. 1) Treat 400 API_KEY_INVALID as a client error, not a retry; 2) See which environment variable the SDK actually reads; 3) Send the key the way native Gemini expects; 4) Mint a new AI Studio key and replace every copy; 5) Separate “key invalid” from project/billing blocks.
Last updated: · This page is the decision checklist; the tools list for “Fix Gemini API_KEY_INVALID / API key not valid” is a separate URL.
How do I fix which Gemini key is sent, and stop retrying 400s?
Treat 400 API_KEY_INVALID as a client error, not a retry
Google’s troubleshooting guide says not to retry client errors like `400` (invalid keys, bad syntax). Exponential backoff is for transient `429`/`5xx`. If the response details show `reason: API_KEY_INVALID` / “API key not valid. Please pass a valid API key.”, fix the credential first. Troubleshooting: https://aistudio.google.com/docs/troubleshooting
See which environment variable the SDK actually reads
Official Gemini client libraries pick up `GEMINI_API_KEY` or `GOOGLE_API_KEY`. If both are set, `GOOGLE_API_KEY` wins—so an old Cloud key in your shell can override a fresh AI Studio key you just put in `GEMINI_API_KEY`. Unset the one you don’t mean, or pass `api_key=` explicitly to the client. API key guide: https://ai.google.dev/gemini-api/docs/api-key
Send the key the way native Gemini expects
For REST to `generativelanguage.googleapis.com`, send `x-goog-api-key: $GEMINI_API_KEY` (or the `key` query param)—not an OpenAI-style `Authorization: Bearer` header. Strip wrapping quotes and trailing newlines from `.env` pastes; quoted or truncated `AIza…` strings commonly produce exactly this 400. API reference: https://ai.google.dev/api
Mint a new AI Studio key and replace every copy
In Google AI Studio, create a new key (Studio keys are restricted to the Gemini API by default), update local env, CI secrets, and host config, then retire the old key. If Studio shows the key as leaked/blocked, or the API says it was reported as leaked, that key cannot be reused—generate another and keep it out of client-side code and public repos.
Separate “key invalid” from project/billing blocks
`models.list` can return 200 while inference still fails when the project needs verification, billing, or paid-tier access. A `403 PERMISSION_DENIED` (verification, free-tier unavailable, payment needed, or leaked key) is a different fix path than `API_KEY_INVALID`. Check the project status in AI Studio / Cloud Billing after the key string itself validates.
Which tools are best to fix Gemini API_KEY_INVALID / API key not valid?
Using Gemini API keys
Env var precedence (GOOGLE_API_KEY over GEMINI_API_KEY) and how to pass a key explicitly.
Gemini troubleshooting
Do not retry 400s; leaked-key and project status guidance.
Gemini API reference
Native auth via x-goog-api-key on generativelanguage.googleapis.com.
Google AI Studio API keys
Create and rotate Studio keys restricted to the Gemini API.
Open: 1 spot per week
One labeled sponsor for this topic each week; this week's spot is open. See open weeks →
All tools for “Fix Gemini API_KEY_INVALID / API key not valid” →
Related fixes
- Stop a Cursor agent loopHow to fix a Cursor agent loop (same edit, or work after Stop)Fix →
- Claude Code vs Codex vs Cursor ~$20 plansClaude Code vs Codex vs Cursor $20 plan limits: how to pick without burning the weekFix →
- Scan a vibe-coded app for leaked API keysCheck a vibe-coded app for leaked API keys before you share itFix →
- Add an MCP server to Claude Code, Cursor, or CodexHow to add an MCP server to Claude Code, Cursor, or CodexFix →
See also AI tools launched today on the daily board.
NewAIToolsToday Daily
Top 3 AI launches, one model or news item that matters, and one simple fix. A 5-minute read, every weekday.