Step 1: Access Google AI Studio
Navigate to aistudio.google.com and sign in with your Google account. AI Studio is the fastest path to a working Gemini API key. For most teams, the AI Studio path is fine for production too. The Vertex AI path (Google Cloud) is the right choice if you need GCP IAM, regional endpoints, audit logs, or VPC controls.
Step 2: Accept Terms and Conditions
On your first visit, you'll need to:
- Review and accept Google's Generative AI terms of service
- Confirm your region (the Gemini API is not available in every country)
- Acknowledge the data usage and privacy policies
Step 3: Where to Find Your Gemini API Key

Click "Get API key" in the left sidebar of AI Studio (or go directly to aistudio.google.com/app/apikey).
The interface displays:
- Your existing API keys (if any)
- The Google Cloud project each key is tied to
- Usage statistics per key
- Buttons to create new keys or delete old ones
Step 4: Create Your Gemini API Key

Click "Create API key". You'll see two options:
Option A: Create key in new project (default for first-time users)
- AI Studio creates a new Google Cloud project automatically
- Zero configuration, fastest path to a working key
- Best for prototyping and side projects
Option B: Create key in existing project
- Pick any Google Cloud project you have access to
- Required if you want to integrate with other GCP services
- Required if your org's billing is consolidated under one project
Pick one and click "Create API key".
Step 5: Copy and Save Your Gemini API Key

The key appears immediately. It starts with AIza followed by alphanumeric characters:
AIza_____________________

Unlike OpenAI and Anthropic, Google lets you view the key again later from the AI Studio dashboard. That's a usability win, but it doesn't change the security advice. Treat it like a password from the moment it appears.
The fastest way to use it is to set it as an environment variable:
export GEMINI_API_KEY="AIza..."
Storage recommendations:
- Use environment variables in development (
.envfiles) - Use Google Secret Manager for production deployments
- Add
.envto.gitignoreimmediately - Never commit a key to a repo, even a private one
The official Google Gen AI SDKs (Python, Node, Go, Java) automatically pick up GEMINI_API_KEY. They will also pick up GOOGLE_API_KEY if set. If both are present, GOOGLE_API_KEY takes precedence.
Step 6: Restrict Your API Key (Recommended)
Google AI Studio keys are powerful by default. Lock them down:
- Click on your newly created key in the AI Studio console
- Open the linked Google Cloud project's Credentials page
- Configure:
- Application restrictions: Limit by IP address, HTTP referrer, or Android/iOS app
- API restrictions: Restrict the key to only the Generative Language API
- Quotas: Set per-minute and per-day request limits
A key with no restrictions, leaked to a public repo, can drain a budget overnight. The first restriction to set is "API restrictions → Restrict key → Generative Language API only".
Step 7: Test Your Gemini API Key
With your key set as an environment variable, run a single curl request:
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-X POST \
-d '{
"contents": [{
"parts": [{
"text": "Hello, Gemini!"
}]
}]
}'
A few things to know:
- The
x-goog-api-keyheader is the current standard. You'll see older guides showing?key=YOUR_API_KEYas a URL parameter. That still works for backwards compatibility, but the header form is what Google's official docs use. - The API endpoint is
v1beta. Yes,v1beta, notv1. Both versions exist, but most current model features land inv1betafirst. Google is unlikely to break it. gemini-2.5-flashis a stable model alias at the time of writing. If you want the newest preview models, usegemini-3-flash-previeworgemini-3.1-pro-previewinstead.
A successful response returns a candidates array with the generated text.
Is the Gemini API Free?
Yes, with caveats. Google AI Studio offers a free tier for the Gemini API. As of Q2 2026, the free tier limits are roughly:
- Gemini 2.5 Flash: 10 requests per minute, 500 requests per day
- Gemini 2.5 Flash-Lite: higher RPM, suited for high-volume light tasks
- Gemini 3.x preview models: paid tier only
Google reduced free tier limits significantly in late 2025 to combat abuse. If you're reading older guides showing 60 RPM and 1M tokens per month, those numbers are stale.
For up-to-date limits, check your AI Studio dashboard. Google now publishes specific limits there rather than on a static docs page, because the numbers change.
If you need higher throughput, enable billing on the Google Cloud project linked to your key. The free tier and paid tier use the same API, the same key format, the same endpoint. You just stop hitting rate limits.
Which Gemini Model Should You Use?
The current production lineup as of Q2 2026:
- Gemini 3.1 Pro (preview, paid tier): the most capable Gemini model. Strong at complex reasoning, multimodal understanding, agentic coding. Use it when quality matters more than cost.
- Gemini 3 Flash (preview, paid tier): frontier-class performance at a fraction of the cost of Pro. The sweet spot for most production workloads.
- Gemini 3.1 Flash-Lite (preview, paid tier): high-volume, cost-sensitive workhorse. Good for classification, extraction, simple chat.
- Gemini 2.5 Flash (stable, free tier eligible): the daily driver for the free tier. Available on both free and paid plans.
- Gemini 2.5 Pro (stable, paid tier): high-capability model with adaptive thinking and a 1 million token context window.
Older models you might see referenced in tutorials are all retired or retiring soon:
gemini-proandgemini-pro-vision: retired- All Gemini 1.0 and 1.5 models: retired (return 404 if called)
- Gemini 2.0 Flash and 2.0 Flash-Lite: scheduled to shut down June 1, 2026
If you have legacy code targeting any of these, migrate to Gemini 2.5 Flash or newer.
Gemini API Key Format
Gemini API keys all start with the prefix AIza followed by a long alphanumeric string. This is the standard Google API key format used across many Google APIs, not just Gemini. If your key doesn't start with AIza, it's not a Gemini key.
Security Best Practices
A leaked Gemini API key, like any other, can drain your budget. The basics:
- Always configure key restrictions. Limit by IP and restrict to the Generative Language API only.
- Rotate quarterly, not just when you suspect a leak.
- Use a different key per environment. Dev, staging, prod should never share a key.
- Monitor usage daily in the Cloud Console.
- Set hard quotas. A runaway loop hitting a free key is annoying. A runaway loop hitting a paid key with no cap is expensive.
- Use service accounts for production servers if you're on Vertex AI. They're more secure than API keys.
- Never expose Gemini API keys in frontend code. A key in a React bundle is a key in the wild.
Migrating from OpenAI or Anthropic to Gemini
If you're switching providers, the moving parts are:
| OpenAI | Anthropic | Gemini | Grok | Perplexity | |
|---|---|---|---|---|---|
| Auth header | Authorization: Bearer | x-api-key | x-goog-api-key | Authorization: Bearer | Authorization: Bearer |
| Endpoint | api.openai.com/v1/responses | api.anthropic.com/v1/messages | generativelanguage.googleapis.com/v1beta/... | api.x.ai/v1/chat/completions | api.perplexity.ai/chat/completions |
| Request shape | messages array | messages + system | contents with parts | messages (OpenAI-compatible) | messages (OpenAI-compatible) |
| Env var | OPENAI_API_KEY | ANTHROPIC_API_KEY | GEMINI_API_KEY | XAI_API_KEY | PERPLEXITY_API_KEY |
| Key format | sk-... | sk-ant-... | AIza... | xai-... | pplx-... |
| Built-in web search? | no | no | no | optional via tools | yes (Sonar) |
| Citations included? | no | no | no | optional | yes (Sonar) |
| OpenAI SDK works? | ✓ | ✗ | ✗ | ✓ | ✓ |
Gemini's request shape is the most different of the bunch. Where OpenAI and Anthropic both use a messages array of {role, content} objects, Gemini uses a contents array of {role, parts} objects, and each part is a typed object (text, inline_data, file_data). It's more verbose but more explicit about modality.
When to Move to Vertex AI
You should migrate to Vertex AI (Google Cloud) when:
- You need IAM-controlled access (service accounts, IAM roles, audit logs)
- You need regional endpoints for data residency
- Your security team requires VPC Service Controls
- You want consolidated GCP billing across all Google services
- You're using fine-tuning, custom model serving, or batch prediction
You don't need Vertex AI just because you're "going to production." A standard AI Studio API key with restrictions and a budget cap is enough for most production workloads.
Troubleshooting Common Errors
API key not valid. Please pass a valid API key. — The key is wrong, deleted, or doesn't have the Generative Language API enabled. Check the key, check that the Generative Language API is enabled in the linked GCP project, and verify there's no extra whitespace.
404 NOT_FOUND: models/{model_name} is not found — You're calling a retired or non-existent model. The big offenders are gemini-pro, gemini-pro-vision, and all gemini-1.0-* and gemini-1.5-* strings. Migrate to gemini-2.5-flash or newer.
429 RESOURCE_EXHAUSTED — You've hit a quota. Free tier is 10 RPM / 500 RPD on Gemini 2.5 Flash. Either wait, enable billing, or implement exponential backoff. Check the AI Studio dashboard for current limits.
400 INVALID_ARGUMENT — The request shape is wrong. Most common cause: copy-pasted an OpenAI-style messages array instead of Gemini's contents array.
Geographic / regional access errors — The Gemini API is unavailable in a handful of countries. Check Google's available regions list. VPNs are not a supported workaround for production.
FAQ
Where do I find my Gemini API key? At aistudio.google.com/app/apikey. Existing keys are listed there and the secret value remains visible (unlike OpenAI and Anthropic, which only show keys at creation).
How do I get a Gemini API key for free? Sign in to AI Studio with a Google account, accept the terms, and click "Create API key". Free tier rate limits apply unless you enable billing.
Is the Gemini API free? There is a free tier with limited rate limits (10 RPM, 500 RPD on Gemini 2.5 Flash as of Q2 2026). The newest 3.x models are paid tier only. Free tier limits were significantly reduced in late 2025.
What's the difference between Google AI Studio and Vertex AI? AI Studio is the developer-friendly path: sign in with a Google account, get a key, start building. Vertex AI is the enterprise path: GCP IAM, regional endpoints, audit logs, VPC controls. Same models, same underlying infrastructure, different access models.
What does a Gemini API key look like?
A string starting with AIza followed by alphanumeric characters. This is Google's standard API key format used across many Google APIs.
How do I set the GEMINI_API_KEY environment variable in Python?
export GEMINI_API_KEY="AIza..."
Then in Python:
from google import genai
client = genai.Client() # picks up GEMINI_API_KEY from env
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Hello, Gemini!"
)
print(response.text)
Why does the API endpoint say v1beta if it's stable?
Google's Gemini API has used v1beta as the primary versioned path for a long time, and most new features land there before (or instead of) appearing in v1. It's stable in practice. Both endpoints work for the current models.
Can I use a Gemini API key with Vertex AI? No. Vertex AI uses Google Cloud authentication (service accounts, OAuth tokens), not AI Studio API keys. The models are the same, the auth mechanism is different.
Next Steps
With your key working, the official docs are the source of truth:
If you're integrating Gemini across multiple SaaS tools (CRM, accounting, HRIS, file storage), look at how unified APIs reduce the per-vendor integration work.
Ready to get started?
Scale your integration strategy and deliver the integrations your customers need in record time.








