Submit a toolSubmit

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.

The problem: 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.

How do I fix which Gemini key is sent, and stop retrying 400s?

  1. 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

  2. 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

  3. 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

  4. 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.

  5. 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.

All tools for “Fix Gemini API_KEY_INVALID / API key not valid” →

Related fixes

See also AI tools launched today on the daily board.