How to get your Gemini API key
Connecting Gemini—Google’s LLM and formerly known as Bard—with your internal applications and/or product can fundamentally change how your employees interact with your applications and/or how customers use your product.
But before you can access one of Gemini’s models via API requests, you’ll need to generate an API key within the Google AI Studio.
We’ll help you do just that by walking through each of the 5 steps you’ll need to take.
Step-by-Step Guide to Generating Your Gemini API Key
1. Sign in to Google AI Studio
Navigate to
Note: If you are using a Google Workspace account (for work or school), your administrator may need to enable access to "Google AI Studio" in the Admin Console.
2. Navigate to the API Key Section
Once you are logged into the dashboard, look at the left-hand sidebar menu.
3. Create the Key
You will see two primary options for creating a key:
Create API key in new project: This is the easiest method. Google will automatically create a new Google Cloud project for you in the background and generate a key for it.
Create API key in existing project: Choose this if you already have a Google Cloud project (e.g., for Firebase or other Google APIs) and want to keep your resources organized together.
Click "Create API key" after making your selection.
4. Copy and Secure Your Key
A pop-up will appear containing your unique API key.
Copy the key immediately.
Store it safely: Use a password manager or an environment variable file (
.env).Security Warning: Never hard-code this key directly into public GitHub repositories. If your key is leaked, anyone can use your quota, which may lead to service interruptions or charges if you have billing enabled.
Managing and Restricting Your Key
To ensure your key is used correctly, you can manage it further:
| Action | How to do it |
| Delete/Rotate | Click the trash icon next to the key in the "API keys" list and generate a new one. |
| Set Billing | Go to the "Usage & Billing" tab to upgrade to the Pay-as-you-go tier for higher rate limits. |
| Restrictions | Click on the Project ID link to open the Google Cloud Console, where you can restrict the key to only work with specific IP addresses or websites. |
Quick Connection Test
Once you have your key, you can verify it works by running a simple curl command in your terminal (replace YOUR_API_KEY with your actual key):
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=YOUR_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{"text": "Write a tagline for a coffee shop."}]
}]
}'
Part 2: Detailed Guide (Manual Project & Key Setup)
Use this if you want to link your key to a specific project (e.g., "Trend-Analysis-2026")
for better organization in the Google Cloud Console.
Use this if you want to link your key to a specific project (e.g., "Trend-Analysis-2026")
for better organization in the Google Cloud Console.
Step 1: Create a Project in Google Cloud
Go to the Google Cloud Console .
Click the Project Dropdown (top left) > New Project.
Enter your Project Name and click Create.
Go to the Google Cloud Console .
Click the Project Dropdown (top left) > New Project.
Enter your Project Name and click Create.
Step 2: Enable the Gemini API
In the Cloud Console search bar, type "Generative Language API".
Select it from the results and click Enable.
In the Cloud Console search bar, type "Generative Language API".
Select it from the results and click Enable.
Step 3: Link Project to AI Studio
Return to Google AI Studio .
Click "Get API key" in the sidebar.
Click "Create API key in existing project".
A list will appear; select the project you just created in Step 1.
Click "Generate Key".
Return to Google AI Studio .
Click "Get API key" in the sidebar.
Click "Create API key in existing project".
A list will appear; select the project you just created in Step 1.
Click "Generate Key".
Part 3: Securing and Testing Your Key
1. Copy and Store
Once the key is generated, copy it immediately.
Recommendation: Store it in a
.envfile for your Python scripts:Bash# .env file GEMINI_API_KEY=your_actual_key_here
2. Verify with Gemini 3 (2026 Update)
The latest SDKs (like google-genai) use the key automatically if stored in your environment. You can test it with this minimal Python snippet:
from google import genai
# The client automatically looks for the GEMINI_API_KEY environment variable
client = genai.Client()
response = client.models.generate_content(
model="gemini-3-flash",
contents="Hello, confirm API key is active."
)
print(response.text)
Critical 2026 Security Reminders
Do Not Hardcode: Never put your key inside client = genai.Client(api_key="...") if you plan to share your code or push it to GitHub.
Usage Limits: Free tier keys now include SynthID watermarking on all generated images and media by default.
API Key Restrictions: If you want to prevent others from using your key, go to Google Cloud Console > APIs & Services > Credentials, click your key, and restrict it to "Generative Language API" only.
Do Not Hardcode: Never put your key inside client = genai.Client(api_key="...") if you plan to share your code or push it to GitHub.
Usage Limits: Free tier keys now include SynthID watermarking on all generated images and media by default.
API Key Restrictions: If you want to prevent others from using your key, go to Google Cloud Console > APIs & Services > Credentials, click your key, and restrict it to "Generative Language API" only.