Registering for Google Gemini and Creating Your API Key
What this lesson covers
Create a Gemini API key in Google AI Studio, understand the Google Cloud project sitting behind it, store it correctly, and call Gemini three ways — raw REST, the official google-genai SDK, and the OpenAI-compatible endpoint. Gemini has a genuinely usable free tier, so you can finish every lab here without a card. Budget about 25 minutes.
Time-sensitive: Standard keys are being switched off. Google is migrating the Gemini API to authorization keys — keys bound to a Google Cloud service account, restricted to the Generative Language API, with fast leaked-key enforcement. Unrestricted standard keys are already rejected, and after September 2026 the API rejects all standard keys, restricted or not. Every key created in AI Studio today is an authorization key by default, so a new key is fine — but if you are reusing a key from an older tutorial, check the Key Type column on the API keys page before you debug anything else.
AI Studio or Vertex AI?
Two front doors to the same models. Google AI Studio (aistudio.google.com) gives you an API key in about thirty seconds and a free tier — the right choice for this course and for prototyping. Vertex AI is the Google Cloud enterprise path, with IAM, regional control and no simple key. Start with AI Studio; migrate later if you need the governance.
Before you start
- A Google account.
- A terminal, and Python 3 if you want the SDK labs.
- No payment method required — the free tier covers everything below.
Free tier trade-off, worth knowing up front: on the free tier Google may use your prompts and responses to improve its products. On the paid tier it does not. Never send confidential or client data through a free-tier key.
AI Studio tour
| Page | What it is for |
|---|---|
| API keys | Create and delete keys, and check the all-important Key Type column. |
| Studio / prompt editor | Try prompts against a model, tune parameters, then click Get code for a working snippet. |
| Usage and billing | Your current tier, request counts and spend. |
| Model catalogue | Exact model ids, context windows and capabilities. |
| Pricing | Per-token costs and the free-vs-paid data policy. |
Lab 1 — Create the key
- Go to https://aistudio.google.com/apikey and sign in with your Google account.
- Click Create API key. You will be asked to pick or create a Google Cloud project — every Gemini key belongs to one, and that project is what carries billing, quotas and collaborators. Let AI Studio create one for you if you have none.
- Copy the key.
- Check the Key Type column now reads Authorization key. If you are looking at an older key marked Standard, create a fresh one and delete the old one once your app works.
Done when: the keys list shows one authorization key, with the project name next to it.
Lab 2 — Store the key
The SDKs read GEMINI_API_KEY from the environment, so no key ever needs to appear in your code.
export GEMINI_API_KEY="..." # macOS / Linux
$env:GEMINI_API_KEY = "..." # Windows PowerShell
In a project, a .env file plus a .gitignore entry in the same commit:
# .env
GEMINI_API_KEY=...
GOOGLE_API_KEY=...
# .gitignore
.env
Why two variables? Some libraries look for GEMINI_API_KEY and others for GOOGLE_API_KEY. Set both to the same value and neither can catch you out. If both are present, GOOGLE_API_KEY wins.
Done when: echo $GEMINI_API_KEY prints the key and git status does not list .env.
Lab 3 — First call with curl
Gemini's key travels in a Google-specific header, x-goog-api-key — not a bearer token, and not x-api-key:
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3.8-flash",
"input": "Reply with the words: gemini works."
}'
Done when: you get a JSON response containing the model's reply.
Lab 4 — The official SDK
Install the current library. Note the package name: google-genai, not the older google-generativeai.
pip install -U google-genai
from google import genai
client = genai.Client() # reads GEMINI_API_KEY from the environment
interaction = client.interactions.create(
model="gemini-3.8-flash",
input="Explain how AI works in a few words",
)
print(interaction.output_text)
JavaScript is the same shape (npm install @google/genai).
Done when: the script prints a response without the key appearing anywhere in the file.
Lab 5 — Drop Gemini into OpenAI-shaped code
Google publishes an OpenAI-compatible endpoint, so existing code moves over by changing three lines — key, base URL, model id:
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
)
response = client.chat.completions.create(
model="gemini-3.8-flash",
messages=[{"role": "user", "content": "Explain how AI works"}],
)
print(response.choices[0].message)
In real code, read the key from the environment rather than pasting a literal. This route is the quickest way to add Gemini to anything already written against OpenAI — at the cost of Gemini-specific features the compatibility layer does not expose.
Done when: the same script runs against both an OpenAI model and a Gemini model with only the three lines changed.
Lab 6 — Compare models and find your tier
- Re-run Lab 4 against three models and notice the latency difference:
gemini-3.8-flash— the current default; strongest Flash model.gemini-3.5-flash-lite— fastest and cheapest ($0.30 in / $2.50 out per million tokens).gemini-2.5-pro— deepest reasoning, slowest, dearest.
- Open the usage page and confirm your tier. Free tier has no spend-based limits; Tier 1 arrives the moment you link an active billing account, and higher tiers unlock on cumulative Google Cloud spend plus elapsed time.
- In the Studio prompt editor, build a prompt you like and press Get code — it emits a runnable snippet in your language, which is the fastest way to learn the request shape.
Model ids move fast. The ids above were current when this lesson was written. Check the model catalogue rather than trusting any tutorial, including this one.
Done when: you have three responses, you know your tier, and you have generated one snippet from the Studio.
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
403 / API_KEY_INVALID on a key that used to work | Standard key being phased out | Check Key Type on the API keys page; create an authorization key and swap it in. |
400 API key not valid | Truncated paste, or the key sent as a bearer token | Gemini uses the x-goog-api-key header, not Authorization: Bearer. |
403 SERVICE_DISABLED | Generative Language API not enabled on the Cloud project | Enable it on that project, or create the key from AI Studio, which does it for you. |
429 RESOURCE_EXHAUSTED | Free-tier or per-tier rate limit | Back off and retry, use a Flash-Lite model, or link billing to reach Tier 1. |
404 on the model | Retired or misspelled model id | Copy it from the model catalogue. |
| SDK cannot find the key | Variable not exported in this shell | Export GEMINI_API_KEY, or load .env before constructing the client. |
ImportError: google.genai | Old package installed | pip install -U google-genai — google-generativeai is the previous library. |
Knowledge check
Q1. Which header carries a Gemini key, and how does it differ from Anthropic and OpenAI?
x-goog-api-key. Anthropic uses x-api-key, OpenAI and OpenRouter use Authorization: Bearer. Three vendors, three conventions.
Q2. What is an authorization key and why does it matter right now?
A key bound to a Google Cloud service account and restricted to the Generative Language API. Standard keys are rejected after September 2026, so anything older must be replaced.
Q3. Your Gemini key works but a colleague's identical-looking code returns 403 SERVICE_DISABLED. Why?
Their key belongs to a Cloud project that does not have the Generative Language API enabled. Keys are per-project.
Q4. Which environment variable wins if both are set?
GOOGLE_API_KEY takes precedence over GEMINI_API_KEY. Set both to the same value.
Q5. What is the real cost of the free tier?
Google may use free-tier prompts and responses to improve its products. Paid-tier content is not used that way.
Q6. You have a working OpenAI app and want to try Gemini. What is the minimum change?
Three lines: api_key, base_url to https://generativelanguage.googleapis.com/v1beta/openai/, and a Gemini model id.
Q7. Which package do you install today?
google-genai. google-generativeai is the older library and imports differently.
Q8. How do you get from Free to Tier 1?
Link an active billing account — the upgrade is usually instant. Higher tiers need cumulative Cloud spend plus elapsed days.
FAQ
Is Gemini really free?
The free tier is real and generous enough for learning, with rate limits rather than a bill. The trade-off is the data policy above, and lower throughput.
Do I need a Google Cloud account?
Not a billing account, no — but every key lives in a Cloud project, and AI Studio will create one silently if you have none. That project is where quotas and, later, billing attach.
AI Studio key or Vertex AI?
AI Studio for prototyping and this course. Vertex AI when you need IAM roles, VPC controls, data residency or enterprise support — it uses Google Cloud auth rather than a simple key.
Can I call Gemini from the browser?
No. Google's own guidance is explicit: keys compiled into web or mobile apps can be extracted — run a backend proxy instead. The Next.js, Node.js and PHP lessons in this section all use that pattern.
What if a key leaks?
Create a replacement, update your app, confirm it works, then delete the old key. Authorization keys also come with fast automated leaked-key enforcement.
Where should production keys live?
Google Cloud Secret Manager, not a config file and not a repo. Set billing alerts on the project so unusual usage surfaces quickly.
Which model should I default to?
gemini-3.8-flash. Drop to gemini-3.5-flash-lite for high-volume, latency-sensitive work; reach for gemini-2.5-pro only when a task genuinely needs deep reasoning.
Does Gemini do embeddings?
Yes — gemini-embedding-001 for semantic search and RAG, on the same key.
Resources
- Get an API key — https://aistudio.google.com/apikey
- Google AI Studio — https://aistudio.google.com
- Usage and tier — https://aistudio.google.com/usage
- Quickstart — https://ai.google.dev/gemini-api/docs/quickstart
- API keys and security — https://ai.google.dev/gemini-api/docs/api-key
- Model catalogue — https://ai.google.dev/gemini-api/docs/models
- Rate limits and tiers — https://ai.google.dev/gemini-api/docs/rate-limits
- Pricing and data policy — https://ai.google.dev/gemini-api/docs/pricing
- OpenAI compatibility — https://ai.google.dev/gemini-api/docs/openai
Next
Key in place — Calling the Google Gemini API with cURL and REST Basics takes the Lab 3 request apart field by field.