To use the Gemini API, you must authenticate your requests. You can authenticate using a standard or authorization API key.
Create or view a Gemini API Key
API key types: standard versus authorization
API keys provide access to the Gemini API, but their security characteristics differ. The Gemini API is transitioning from standard API keys to authorization keys to improve security:
- Standard API keys: Associate requests with a Google Cloud project for billing and quota purposes. Standard keys don't identify a caller, which limits the granularity of permissions and access control they can support.
- Authorization (auth) keys: Bound directly to a Google Cloud service account. When you use an authorization key, your requests are processed under the identity of that bound service account, enabling granular access control. Authorization keys are restricted to the Generative Language API (Gemini API) by default and provide fast-acting leaked key enforcement that quickly stops the usage of leaked keys detected by our systems.
To ensure secure usage, Gemini API will move from Standard keys to Auth keys:
- Auth keys default: All new API keys created in Google AI Studio are automatically created as auth keys.
- Unrestricted keys rejected: The Gemini API rejects requests from unrestricted standard keys. Standard API keys that have explicit restrictions applied continue to work. This restriction prevents the unauthorized use of keys that might be shared publicly or linked to other services.
- On September 2026: the Gemini API will reject requests from Standard keys. You must migrate to auth keys before this date to avoid service interruption. Make sure to migrate to auth keys before September 2026.
Managing API keys in Google AI Studio
You can manage your projects and keys directly in Google AI Studio.
Google Cloud projects
Every Gemini API key is associated with a Google Cloud project. Google Cloud projects manage billing, collaborators, and permissions. Google AI Studio provides a lightweight interface to access these projects.
- Default project: If you are a new user, Google AI Studio automatically creates a default Google Cloud project and API key after you accept the Terms of Service. You can rename this project by navigating to the Projects view in your dashboard.
- Existing projects: If you already have a Google Cloud account, AI Studio does not create a default project. Instead, you must import your existing projects.
Importing projects
By default, Google AI Studio does not display all of your Google Cloud projects. You must import the projects you want to use:
- Go to Google AI Studio.
- Open the Dashboard from the left panel and select Projects.
- Click the Import projects button.
- Search for and select the Google Cloud project you want to import, then click Import.
- Once imported, navigate to the API Keys page in the dashboard to create a key in that project.
Troubleshooting key creation permissions
If the Create API key button is unavailable and displays the message: "You do not have permission to create a key in this project", you lack the required IAM permissions.
Ask your Google Cloud project or organization administrator to grant you a role containing the following permissions (such as Project Editor):
resourcemanager.projects.get: Allows AI Studio to verify the project.apikeys.keys.create: Allows key generation.serviceusage.services.enable: Ensures the Generative Language API is enabled.iam.serviceAccounts.create: Required to create the linked service account.iam.serviceAccountApiKeyBindings.create: Binds the service account to the API key.
If you cannot get administrative access, you can create a new Google Cloud project that is not associated with an organization to generate your keys.
Setting up your environment
Once you have a key, configure your environment to use it securely in your applications.
Option 1: Use environment variables (recommended)
Set the environment variable GEMINI_API_KEY or GOOGLE_API_KEY. The Gemini
API client libraries automatically detect and use these variables. If both are
set, GOOGLE_API_KEY takes precedence.
Select your operating system to set the variable:
Linux/macOS - Bash
Verify if you have a bash configuration file:
~/.bashrcIf not, create one and open it:
touch ~/.bashrc && open ~/.bashrcAdd the export command at the end of the file:
export GEMINI_API_KEY=<YOUR_API_KEY_HERE>Save the file, then apply the changes:
source ~/.bashrcmacOS - Zsh
Verify if you have a zsh configuration file:
~/.zshrcIf not, create one and open it:
touch ~/.zshrc && open ~/.zshrcAdd the export command:
export GEMINI_API_KEY=<YOUR_API_KEY_HERE>Save the file, then apply the changes:
source ~/.zshrcWindows
- Search for "Environment Variables" in the Windows search bar.
- Click Environment Variables in the System Properties dialog.
- Under User variables or System variables, click New....
- Set the variable name to
GEMINI_API_KEYand the value to your API key. - Click OK to save. Open a new terminal session to load the variable.
Option 2: Provide the API key explicitly in code
You can pass the API key explicitly when initializing the client. Only do this if you cannot use environment variables.
Python
from google import genai
client = genai.Client(api_key="YOUR_API_KEY")
interaction = client.interactions.create(
model="gemini-3.7-flash",
input="Explain how AI works in a few words"
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY" });
async function main() {
const