How to Get a Gemini API Key in 2026 and Make Your First Call
Need a Gemini API key? Here's how to create one free in Google AI Studio, make your first API call, keep the key safe and know what it costs in 2026.
To get a Gemini API key, sign in to Google AI Studio at aistudio.google.com with a Google account, open the API Keys page and click Create API key. It's free, takes about two minutes, and new accounts get a default project and key as soon as they accept the terms. You only pay if you turn on billing.
The key is the easy part. What trips people up is everything around it: which model name to use, why a call that worked yesterday now returns a 429, whether the free tier is fine for client data, and how to stop a leaked key from running up a bill. This guide walks through getting the key, making a first call in Python, JavaScript or the command line, what the free and paid tiers include, and how to keep the key safe.
How to get a Gemini API key in Google AI Studio
You create Gemini API keys in Google AI Studio, Google's free web tool for the Gemini API. Every key belongs to a Google Cloud project, and AI Studio creates that project for you if you don't have one. Here are the steps from Google's API key guide:
- Go to aistudio.google.com and sign in with your Google account.
- Accept the terms of service if it's your first visit. New users get a default project and an API key at this point.
- Click Get API key, or open the API Keys page from the left panel.
- Click Create API key and choose the project it belongs to.
- Copy the key and store it somewhere safe, such as a password manager or your computer's environment variables (steps below).
Since May 28, 2026, keys created in AI Studio are what Google calls auth keys: they're tied to a service account and restricted to the Gemini API by default. If your Key Type column says Standard, you have an older key, and Google recommends creating a new one and moving your apps over.
If you already use Google Cloud
AI Studio doesn't create a default project for existing Google Cloud accounts. You import a project instead:
- In AI Studio, open Dashboard, then Projects.
- Click Import projects, select your Cloud project and click Import.
- Go back to API Keys and create a key in that project.
If you see "You do not have permission to create a key in this project," your Google account doesn't have the right role on that project. Ask whoever manages your company's Google Cloud for a role such as Project Editor. You can have up to 10 projects at a time.
Not every country is supported
The Gemini API and AI Studio work in a list of countries and territories that includes the EU, the UK and Switzerland, but not everywhere. If yours isn't listed, Google points to its enterprise platform on Google Cloud instead.
How to store your Gemini API key safely
Store the key in an environment variable called GEMINI_API_KEY, and never paste it into code you share. Google's client libraries look for GEMINI_API_KEY or GOOGLE_API_KEY automatically, and if both are set, GOOGLE_API_KEY wins, per Google's guide.
On a Mac or Linux
Add one line to your shell's settings file. On a recent Mac that's ~/.zshrc; on most Linux machines it's ~/.bashrc.
export GEMINI_API_KEY=your-key-hereThen run source ~/.zshrc (or source ~/.bashrc) or open a new terminal window so the change takes effect.
On Windows
- Search for Environment Variables and open Edit the system environment variables, then click Environment Variables.
- Under User variables, click New....
- Enter GEMINI_API_KEY as the name and your key as the value, then click OK.
- Open a new terminal so it picks up the variable.
Here's why this matters. Say Wes is a freelance developer in Denver building a quote generator for a roofing client. If he hard-codes the key in his script and pushes it to a public GitHub repository, anyone who finds it can run up charges on his project. With the key in an environment variable, the code he shares never contains it.
How to make your first Gemini API call
The quickest first call is a few lines in Python or JavaScript using Google's Gen AI SDK, which reads your key from GEMINI_API_KEY. Google's quickstart uses the Interactions API and the gemini-3.8-flash model.
Python
Install the library with pip install -U google-genai, then run:
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.8-flash",
input="Write a two-sentence welcome note for new customers of a bike repair shop."
)
print(interaction.output_text)JavaScript (Node.js)
Install the library with npm install @google/genai, then run:
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
const interaction = await ai.interactions.create({
model: "gemini-3.8-flash",
input: "Write a two-sentence welcome note for new customers of a bike repair shop.",
});
console.log(interaction.output_text);From the command line (REST)
If you just want to confirm the key works, a single curl request does it. The key goes in the x-goog-api-key header.
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": "Say hello in five words."
}'If you get text back, your key works. If you get an error, jump to the troubleshooting table below.
What about generateContent?
Older tutorials use generate_content (Python) or generateContent (REST). That still works: Google's migration guide says generateContent "remains fully supported," but recommends the Interactions API for new projects. The Interactions API can keep a conversation's history on Google's side, so you don't resend every earlier message. If you're starting fresh, use Interactions; if you have working code on generateContent, there's no rush to change it.
Which Gemini model should you call?
For most new projects, start with gemini-3.8-flash, and move to gemini-3.5-flash-lite for high-volume, simple jobs or gemini-4-argon for the hardest reasoning. Google's models page recommends 3.5 Flash-Lite or 3.8 Flash for new projects, and says most production apps should name a specific stable model.
Model | Model code | Good for | Free tier |
|---|---|---|---|
Gemini 3.8 Flash | gemini-3.8-flash | Most apps: chat, writing, summaries, extraction | Yes |
Gemini 3.5 Flash-Lite | gemini-3.5-flash-lite | Large volumes of simple, cheap tasks | Yes |
Gemini 4 Argon | gemini-4-argon | Hard reasoning and very long outputs | No |
There's also a gemini-flash-latest alias that always points to the newest Flash model. It's handy for experiments, but Google swaps what it points to, so an app that worked last month can start behaving differently. I'd pin the exact model code in anything a customer sees.
Say Brianna is a product manager at a 30-person software company in Atlanta, prototyping a feature that tags incoming support tickets by topic. Flash-Lite is the sensible starting point: the task is simple and the volume is high. If the tags come back sloppy on long, messy tickets, she tries Flash on the same sample before paying for anything bigger.
Is the Gemini API free?
Yes, the Gemini API has a free tier for many models, and Google AI Studio itself is free to use, per Google's pricing page. The catch is that on the free tier, Google lists your content as used to improve its products. On the paid tier, it isn't.
That one line decides a lot. Free is fine for learning, side projects and public data. For customer emails, contracts, health details or anything under an NDA, I'd turn on billing before the first real record goes through, even if your usage would fit in the free limits.
Paid prices
Paid use is charged per million tokens of input and output. A token is a short chunk of text, roughly a few characters. These are Google's standard text prices as listed in October 2026:
Model | Input | Output | Note |
|---|---|---|---|
Gemini 3.8 Flash | $0.75 | $3.75 | Rises to $1.50 and $7.50 on January 1, 2027 |
Gemini 3.5 Flash-Lite | $0.30 | $2.50 | |
Gemini 4 Argon | $2.00 | $10.00 | Rises to $4.00 and $20.00 on December 1, 2026 |
Google's Argon announcement called the lower Argon price introductory. Batch requests, where you send work to be processed later rather than instantly, cost half the standard price on all three models.
The $300 Cloud credit doesn't cover it
Google's billing page says the $300 Google Cloud free trial no longer applies to Gemini API usage as of March 2026, and welcome credits on accounts opened after March 2, 2026, can't be used for it either.
Gemini API rate limits and usage tiers
Every Gemini API project has rate limits on requests per minute, input tokens per minute and requests per day, and hitting any one of them returns an error. Google's rate limit guide says limits apply per project, not per key, so creating a second key in the same project doesn't give you more.
- Daily limits reset at midnight Pacific time.
- Your exact limits are on the rate limit page in AI Studio (aistudio.google.com/rate-limit), and they differ by model.
- Your tier shows on the AI Studio Projects page.
Tiers go up automatically as you spend:
Tier | How you get it | Account spend cap |
|---|---|---|
Free | An active project | None |
Tier 1 | Link a billing account | $250 |
Tier 2 | $100 paid and 3 days since your first payment | $2,000 |
Tier 3 | $1,000 paid and 30 days since your first payment | $20,000 to $100,000+ |
When an account reaches its tier cap, Google pauses service for all its linked projects until the first of the next month, unless you ask for an increase. Plan for that if you're launching something with a traffic spike.
How to turn on billing and cap your spending
You turn on billing from AI Studio by clicking Set up billing in the Billing Tier column next to your project, then linking or creating a Cloud Billing account. According to Google's billing page:
- Open the API Keys or Projects page in AI Studio.
- Click Set up billing next to the project.
- Pick an existing Cloud Billing account or create one with your country, contact and payment details.
- Finish by prepaying at least $5 or choosing a plan, depending on what your account is offered.
Most new accounts end up on Prepay: you buy credit between $5 and $5,000, usage comes off the balance, and calls fail with a 402 error when it hits zero. Credits expire after 12 months. You can't switch from Prepay to Postpay later.
Then set a ceiling. On the AI Studio Spend page, go to Monthly spend cap and click Edit spend cap. Google labels the feature experimental and warns that about 10 minutes of usage can slip past the cap because of billing delays, so set it a little under what you can actually afford.
Say Andre runs operations at a 12-person accounting firm in Minneapolis and has a script that drafts client reminder emails from a spreadsheet. A $40 prepaid balance with a $30 monthly cap means a runaway loop costs him lunch money, not a week of the firm's revenue.
How to protect your key if it leaks
If a Gemini API key leaks, create a new key, switch your apps to it, then disable the old one. Google's key guide recommends doing it in that order so nothing breaks, and checking the Cloud console for usage you don't recognize.
Prevention is cheaper. Google's own rules of thumb, which I'd follow to the letter:
- Never commit keys to Git, even in a private repository.
- Never put the key in a website or mobile app. Anyone can read it from the code. Send requests through your own server instead.
- Restrict the key. In AI Studio, hover over an Unrestricted label, click Add restrictions, choose Restrict to Gemini API only and click Restrict key. In the Cloud console you can also limit it to your server's IP addresses.
- Use a secret manager in production, such as Google Cloud Secret Manager, rather than a file on the server.
- Set billing alerts in the Google Cloud console.
Two dates to know: since May 7, 2026, unrestricted keys that sit unused too long are blocked and show a Blocked tag, and unrestricted standard keys are rejected. If an old project suddenly stops working, check the key type first.
Common Gemini API key errors and how to fix them
Most Gemini API errors come from the key, the model name, billing or rate limits, and the error code tells you which. These are from Google's error reference and troubleshooting guide:
Code | What it means | What to do |
|---|---|---|
400 | The request is malformed, or a prerequisite such as billing isn't met | Check the request and your billing status; don't just retry |
401 | The key is missing, invalid or expired | Check the key is set in GEMINI_API_KEY and copied in full |
402 | Your prepaid balance is used up | Add credit or turn on auto-reload |
403 | The key doesn't have permission | Check the key's restrictions and the project it belongs to |
404 | The model name doesn't exist | Check the model code against Google's models page |
429 | You hit a per-minute limit or the daily quota | Wait and retry with increasing delays, or move up a tier |
503 | Google's service is overloaded | Wait and retry with increasing delays |
If you see "Your API key was reported as leaked. Please use another API key," Google has blocked it. Create a new key in AI Studio and follow the leak steps above.
One more that catches people: you set the environment variable but your code still can't find it. The terminal you're running in was usually opened before you added it. Close it, open a new one, and try again.
Before you build: a short checklist
- Create the key in its own project for each app, so limits and bills stay separate.
- Put the key in GEMINI_API_KEY, not in your code.
- Restrict it to the Gemini API.
- Decide whether your data can go through the free tier. If not, turn on billing first.
- Pin a specific model code, such as gemini-3.8-flash.
- Set a monthly spend cap a little below your real limit.
- Write your code to retry 429 and 503 errors with increasing delays.
If you're still deciding between Google's tools and OpenAI's for the app side of your work, my Gemini vs. ChatGPT comparison covers the consumer plans and what each is better at.
Frequently asked questions
Is a Gemini API key free?
Yes. Creating a key in Google AI Studio is free, and many models have a free tier. You pay only if you turn on billing and use paid models or go beyond the free limits.
Where do I find my Gemini API key?
Sign in at aistudio.google.com and open the API Keys page from the left panel. Every key you've created is listed there with its project and key type.
Does the free Gemini API use my data for training?
Google's pricing page lists free-tier content as used to improve its products, and paid-tier content as not used. Use the paid tier for anything confidential.
Is the Gemini API included in Google AI Pro?
No. Google AI plans like AI Pro cover the Gemini app, and the API is billed separately through Google AI Studio and Google Cloud.
Why am I getting a 429 error from the Gemini API?
You've hit a rate limit, such as requests per minute or the daily quota. Wait and retry with increasing delays; daily limits reset at midnight Pacific time, and higher tiers get higher limits.
Can I use one Gemini API key for several apps?
You can, but limits and costs apply per project, so one busy app can starve the others. A separate project and key per app keeps them apart.