How to Fix Gemini API 401 Unauthorized Error
Updated 10/29/2026
An HTTP 401 Unauthorized error from the Gemini API indicates that the Google servers rejected your request's credentials. Unlike a 403 Forbidden error (which means your key is recognized but lacks permission for a specific resource), a 401 error means the API gateway cannot authenticate you at all.
This is usually caused by an improperly formatted authorization header, a mismatched environment variable, or an inactive API key. Use the following steps to diagnose and resolve the error.
1. Verify and Correct Your Authorization Headers
One of the most common causes of a 401 error is formatting your HTTP request headers incorrectly. Developers transitioning from other LLM APIs often default to using the standard Authorization: Bearer [KEY] format.
By default, the Gemini API does not use the standard Bearer token scheme. Instead, you must authenticate using one of the following methods:
- Custom Header: Pass the key in the x-goog-api-key header.
- Query Parameter: Append the key directly to the request URL as ?key=YOUR_API_KEY.
Incorrect (will return 401): `bash Authorization: Bearer AIzaSy... `
Correct Header Format: `bash x-goog-api-key: AIzaSy... `
Correct URL Query Format: `bash https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=YOUR_API_KEY `
Agent Builder turns a task you repeat into a reusable assistant with setup text for each app.
Tickd is independent and not affiliated with any AI company.
2. Standardize Your Environment Variables
If you are using an official Google Gen AI SDK (such as Python, Node.js, Go, or Swift), the library automatically looks for a specific environment variable to authenticate requests. If this variable is named incorrectly or is empty, the SDK will fail to authenticate, resulting in a 401 error.
* Ensure your environment variable is named exactly GEMINI_API_KEY. * Avoid wrapping the API key in quotes inside your environment files (like .env). For example, write GEMINI_API_KEY=AIzaSy... instead of GEMINI_API_KEY="AIzaSy...". * In Node.js, confirm that your code is loading the environment variables before initializing the client: `javascript require('dotenv').config(); const { GoogleGenAI } = require('@google/genai'); // If dotenv isn't loaded first, process.env.GEMINI_API_KEY will be undefined `
3. Isolate the Issue with a Direct cURL Request
To rule out SDK bugs, package configuration issues, or code-level environment variable issues, test your API key directly from your terminal using a raw cURL command. Copy the following command, replace YOUR_API_KEY with your actual key, and run it:
`bash curl -H "Content-Type: application/json" \ -d '{"contents":[{"parts":[{"text":"Explain recursion in one sentence."}]}]}' \ -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=YOUR_API_KEY" `
- If this command succeeds (HTTP 200): Your API key is active and valid. The 401 error is caused by how your application code, SDK, or hosting server (e.g., Vercel, AWS) manages, imports, or injects the API key.
- If this command fails with a 401 error: The API key itself is inactive, deleted, or invalid.
4. Confirm the Generative Language API is Enabled
If you generated your API key through the Google Cloud Platform (GCP) Console rather than Google AI Studio, your key is tied to a specific GCP Project. You must ensure that the "Generative Language API" is enabled for that project:
- Open the Google Cloud Console.
- Select the project associated with your API key from the top dropdown menu.
- Navigate to APIs & Services > Library.
- Search for "Generative Language API".
- If it is not enabled, click Enable.
- Allow up to five minutes for the status update to propagate across Google's global edge servers.
5. Regenerate a Clean API Key
If you are still receiving a 401 error after testing the direct cURL request, your API key may have been revoked, deleted, or corrupted.
- Go to Google AI Studio.
- Navigate to the Get API key section.
- Delete your existing key to prevent unauthorized usage.
- Click Create API key.
- Select either a new project or an existing valid project to bind the key.
- Copy the new key immediately and update your application's environment configuration.
When to Escalate
If you have verified that the correct headers are sent, confirmed the API is enabled in GCP, and freshly generated keys still return a 401 error on raw cURL requests, check your Google Cloud Billing account. Google will occasionally restrict or suspend API key authorization silently if the linked billing account is delinquent, suspended, or flagged for security reviews. Ensure your billing status is active in the GCP Billing Console.
Once it's working again, two free Tickd tools can help: AI Setup Profile writes setup text for Gemini so you repeat yourself less, and Agent Builder turns a repeat task into a reusable assistant.