In this categoryTroubleshooting ยท 27
- How to Fix an OpenAI 401 'Invalid API Key' ErrorStart
- How to Fix OpenAI 429 Rate Limit Errors With Backoff
- How to Fix Anthropic Claude API 401 Authentication Errors
- How to Fix Google Gemini 'API Key Not Valid' Errors
- How to Fix an API Key That Loads as Undefined
- How to Handle Anthropic 529 'Overloaded' Errors
- How to Fix Rate Limit Errors from an AI API
- How to Fix CORS Errors When Calling an AI API From the Browser
- How to Fix 'Model Not Found' and Deprecated Model Errors
- How to Rotate a Leaked API Key Without Downtime
- How to Fix SSL Certificate Errors When Calling AI APIs
- How to count tokens before sending a prompt to Claude
- How to Fix 'Context Length Exceeded' Token Limit Errors
- How to fix a context length exceeded error in the Claude API
- How to fix a Claude response that gets cut off mid-sentence
- How to reduce Claude hallucinations by grounding answers in your documents
- How to keep a long Claude conversation under the context limit
- How to stop Claude from calling tools when it should not
- How to handle a Claude Fable 5 refusal with a fallback model
- How to choose the right Claude model for cost and quality
- How to cut Claude API costs with prompt caching
- How to halve Claude costs for bulk jobs with the Batch API
- How to cap spend on a Claude agent with a task budget
- How to Debug an MCP Server That Will Not Connect
- How to Ask an Agent to Explain a Bug Before Fixing It
- How to Roll Back a Bad Deploy Quickly
- How to Fix Cursor Not Indexing Your Codebase
How to Fix Google Gemini 'API Key Not Valid' Errors
Get Gemini API requests working by enabling the API, fixing key restrictions, and using the right endpoint.
Gemini returns 'API key not valid' for several reasons that go beyond a wrong key. The Generative Language API may not be enabled, the key may have HTTP referrer or IP restrictions that block your server, or you may be calling the wrong endpoint. This guide checks each cause.
- A Google AI Studio or Google Cloud account
- An API key created in AI Studio or the Cloud console
- curl to test the endpoint
Step 1: Test the key against the real endpoint
Gemini keys are passed as a query parameter or an x-goog-api-key header. Test with a minimal request so you can see the exact error.
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY" \
-H "content-type: application/json" \
-d '{ "contents": [{ "parts": [{ "text": "ping" }] }] }'Step 2: Enable the Generative Language API
If you created the key in Google Cloud rather than AI Studio, the API itself may be disabled for that project. Enable the Generative Language API in the Cloud console, then wait a minute for it to propagate.
Step 3: Check key restrictions
In the Cloud console you can lock a key to specific HTTP referrers, IP addresses, or apps. A key restricted to a website referrer will fail when called from a server. For server use, restrict by IP or leave it for testing, then tighten later.
Step 4: Confirm the model name
Using a model name that does not exist or is not available to your account can surface as a request error. Use a current model id like gemini-2.0-flash and confirm it appears in the models list endpoint.
Result
After enabling the Generative Language API and removing a leftover referrer restriction, the same curl call returns generated text. The most frequent culprit was the disabled API on a Cloud-created project.
Watch related tutorials
16:10
12:25
14:45
20:30
22:15
35:20New guides in your inbox
Fresh step-by-step how-to guides as we publish them. One email a week, no more.