Gemini API Key Not Working? Step-by-Step Fixes
Updated 10/11/2026
When integrating Google's Gemini models into your applications, encountering an invalid or non-functional API key is a common roadblock. This issue typically manifests as a 400 Bad Request (with an invalid key message), a 401 Unauthorized exception, or a silent failure where your SDK initialization fails to load credentials.
This step-by-step guide walks you through diagnosing and fixing Gemini API key validation errors across both Google AI Studio and Google Cloud environments.
Step 1: Verify API Key Active Status in Google AI Studio An API key can stop working if it was deleted, regenerated, or tied to a Google Cloud project that has been disabled.
- Sign in to Google AI Studio using the Google account associated with your project.
- In the left-hand navigation pane, click on Get API key.
- Locate the API key you are using in your code under the "API keys" list.
- If the key is not listed, it has been deleted. Click Create API key to generate a new one.
- If the key exists, check for any associated warning icons indicating project restrictions or billing holds.
Step 2: Confirm SDK Environment Variable Naming The official Google Gen AI SDKs search for specific environment variable names by default. If you use a custom name, the SDK will look for a default variable, find nothing, and throw an authentication error.
- Python SDK: The library expects the environment variable to be named exactly GEMINI_API_KEY.
- Node.js SDK: The library also defaults to checking process.env.GEMINI_API_KEY.
Ensure your .env file or hosting environment (e.g., Vercel, Render, AWS Lambda) is configured correctly:
`bash # Correct naming convention GEMINI_API_KEY="AIzaSyYourActualKeyHere..." `
If you prefer to pass the key explicitly in your code rather than relying on environment variables, ensure you are passing it to the correct configuration parameter:
`python # Python Example import google.generativeai as genai import os
genai.configure(api_key=os.environ["GEMINI_API_KEY"]) `
`javascript // Node.js Example const { GoogleGenAI } = require("@google/genai"); const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }); `
Step 3: Inspect Request Headers in Direct REST Calls If you are making direct HTTPS requests to the Gemini API rather than using the official SDKs, you must pass the API key either as a query parameter or inside a specific custom header.
- Query Parameter Method: Append ?key=YOUR_API_KEY to the end of the request URL.
- Header Method: Use the x-goog-api-key header to transmit your key.
Here is a correct curl request payload structure for testing your key directly in the command line:
`bash curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=YOUR_API_KEY" \ -H 'Content-Type: application/json' \ -d '{"contents": [{"parts":[{"text": "Test connection"}]}]}' ` If this curl command succeeds but your application code fails, the issue lies in how your application code reads or passes the API key variable.
Step 4: Differentiate Between Google AI Studio and Vertex AI A frequent source of authentication failure is mixing up **Google AI Studio** keys with **Google Cloud Vertex AI** credentials. They use completely different authentication pathways:
- Google AI Studio: Uses a simple API key (AIzaSy...) for access. You cannot use this key to authenticate against Vertex AI endpoints (us-central1-aiplatform.googleapis.com).
- Vertex AI: Does not support standard AI Studio API keys. Instead, it requires Google Cloud IAM authentication. You must use a Service Account JSON key file and set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to that file.
Ensure your code imports matches your intended backend. For Google AI Studio, use @google/genai or google-generativeai. For Vertex AI, use @google-cloud/vertexai or google-cloud-aiplatform.
Step 5: Check Google Cloud Project Billing and API Status Gemini API keys are tied to a Google Cloud Project in the background. If that underlying project is restricted, your key will fail.
- Open the Google Cloud Console and select the project tied to your AI Studio key.
- Navigate to Billing and verify that your billing account is active and has a valid payment method on file.
- Go to APIs & Services > Enabled APIs & Services.
- Search for the Generative Language API (for AI Studio keys) and ensure its status is set to "Enabled". If it is disabled, re-enable it.