Google Gemini API Key: Create, Secure, and Use
What you’ll build
Section titled “What you’ll build”By the end of this guide you will have a Google Gemini API key stored in an environment variable and a working Python and cURL call that returns a Gemini response. Keys are created through Google AI Studio rather than through the Google Cloud Console — no billing account is required to get started.
Prerequisites
Section titled “Prerequisites”| Requirement | Notes |
|---|---|
| Google account | Sign in at aistudio.google.com |
| Python 3.9+ or Node.js 18+ | To run the verification samples |
| Paid tier (optional) | Required for higher quotas and production workloads |
The free tier provides a generous monthly quota (see §Available models and quotas). No credit card is needed to create a key.
Step 1 — Create the API key
Section titled “Step 1 — Create the API key”- Go to aistudio.google.com and sign in with your Google account.
- Click Get API key in the left sidebar.
- Click Create API key in new project (Google creates a Cloud project automatically) or Create API key in existing project if you already have one.
- Copy the key immediately — it starts with
AIzaSy. You can retrieve it again from the AI Studio keys page, but copying it now saves a trip back.
Step 2 — Restrict the key (recommended)
Section titled “Step 2 — Restrict the key (recommended)”An unrestricted key can be used to call any Google API. Restricting it to the Gemini API limits the blast radius if the key leaks.
- Open console.cloud.google.com/apis/credentials and select your project.
- Click the pencil icon next to your key.
- Under API restrictions, select Restrict key and choose Generative Language API.
- Under Application restrictions, choose IP addresses if the key will only be used from a known server, or leave it unrestricted for local development.
- Click Save.
Step 3 — Store the key securely
Section titled “Step 3 — Store the key securely”Linux / macOS (current shell session):
export GOOGLE_API_KEY="AIzaSy-your-key-here"Windows PowerShell (current session):
$env:GOOGLE_API_KEY = "AIzaSy-your-key-here".env file for local development (add .env to .gitignore):
GOOGLE_API_KEY=AIzaSy-your-key-hereLoad it in Python with python-dotenv:
from dotenv import load_dotenvload_dotenv()For production, use your platform’s secret store: Google Secret Manager, AWS Secrets Manager, Azure Key Vault, or equivalent.
Step 4 — Verify the key works
Section titled “Step 4 — Verify the key works”Install the SDK, then run a minimal request.
pip install google-generativeaiSave as check_key.py and run with python check_key.py (Python 3.9+):
import google.generativeai as genaiimport os
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
model = genai.GenerativeModel("gemini-2.5-flash")response = model.generate_content("Reply with the word VERIFIED.")
print(response.text)# Expected output: VERIFIEDOr use cURL:
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=$GOOGLE_API_KEY" \ -H "Content-Type: application/json" \ -d '{"contents":[{"parts":[{"text":"Reply with the word VERIFIED."}]}]}'A successful response returns HTTP 200 with candidates[0].content.parts[0].text containing your reply.
Available models and quotas
Section titled “Available models and quotas”| Model | Context | Free tier (RPM / RPD) | Best for |
|---|---|---|---|
gemini-2.5-pro |
1 M tokens | 5 / 25 | Complex reasoning, long-document analysis |
gemini-2.5-flash |
1 M tokens | 15 / 1,500 | Fast general-purpose tasks |
gemini-2.0-flash |
1 M tokens | 15 / 1,500 | High-volume applications |
text-embedding-004 |
2,048 tokens | 1,500 / — | Semantic search, RAG |
Free-tier quotas and model availability change. Check ai.google.dev/pricing for current figures before building production rate-limit logic.
Managing keys
Section titled “Managing keys”View keys: The AI Studio keys page and the Cloud Console credentials page both show your keys. The Cloud Console also shows last-used timestamps.
Delete a key: Click the trash icon in AI Studio or the Cloud Console and confirm. Applications using that key immediately receive API_KEY_INVALID errors.
Enabling the API: If you created the project manually rather than through AI Studio, you may need to enable the Generative Language API first. Go to console.cloud.google.com/apis/library, search for Generative Language API, and click Enable.
Troubleshooting
Section titled “Troubleshooting”| Error | Likely cause | Fix |
|---|---|---|
API_KEY_INVALID |
Key copied incorrectly or deleted | Verify the key value in the AI Studio keys page. |
PERMISSION_DENIED |
Generative Language API not enabled | Enable the API in Cloud Console › APIs & Services. |
RESOURCE_EXHAUSTED |
Free-tier quota exceeded | Wait for the quota to reset, or upgrade to a paid tier. |
Invalid API key restrictions |
IP or referrer not in allowlist | Update restrictions in Cloud Console › Credentials. |
Next steps
Section titled “Next steps”- Google Models guide — Gemini 2.5 family, Imagen, and Veo capabilities
- Google Antigravity IDE — Gemini-powered agentic development environment
- API Tokens overview — comparison of all supported providers
Official references
Section titled “Official references”- Google AI Studio — key creation and model playground
- Gemini API documentation — versioned REST and SDK reference
- Pricing and quotas — free and paid tier limits
- Google Cloud credentials — key restriction settings
- Python SDK —
google-generativeaipackage on PyPI