To get a Gemini API key, go to Google AI Studio, click Get API key to open the API Keys page, then click Create API key. It's free to start: new users get a default project and API key after accepting the Terms of Service, and you only add billing if you want the paid tier. Copy the key, store it as the GEMINI_API_KEY environment variable, and never put it in frontend code.
If you want to use Google’s Gemini AI, you need a Gemini API Key.
It’s like a password that lets your apps talk to the AI models.
Getting a key is easy. You can start for free and test the AI without paying anything.
In this guide, you will learn how to generate your Gemini API Key step by step. You will also see how to use it safely and avoid common mistakes.
So, let's get started!
What Is a Gemini API Key?
A Gemini API Key is a unique secret code that allows your application to access Google’s Gemini AI models. Every request you send to Gemini must include this key. Google uses it to identify your project, apply usage limits, and control access.
With a valid key, you can:
- Generate text
- Analyze content
- Use multimodal Gemini features
The free tier gives limited but practical access to certain models, which is enough for learning, testing, and small projects.
How to Generate Gemini API Key in Google AI Studio – 5 Easy Steps for Beginners

Generating a Gemini API Key is simple. You can do it in a few clicks. Follow these steps to get your key quickly.
- Sign in to Google AI Studio
- Navigate to the API Keys section
- Create a new project or select an existing one
- Click “Create API key”
- Copy your API key and save it securely
Once you have your key, you can start using Gemini AI in your apps or experiments right away. Keep it safe and don’t share it publicly.
Now, let's get into the details!
1. Sign in to Google AI Studio
The first step is to open Google AI Studio. Use your Google account to sign in. If you don’t have one, you will need to create it.
If this is your first time visiting Google AI Studio, a pop-up will appear asking you to accept the Terms of Service for Generative AI. This is a mandatory, quick legal step. Read them over and click “Accept” or “Continue” to proceed to the main dashboard.
Click on the Get started option to log in or sign up.

Once you are signed in, you will see the main dashboard. This is where you manage projects and API keys. Being logged in with your account is necessary. Google uses it to link your API key to you.
If you are a new user, Google AI Studio creates a default Google Cloud project and an API key for you as soon as you accept the Terms of Service. You can rename that project later in the Projects view.
2. Navigate to the API Keys Section
After signing in, click Get API key, or open the Dashboard from the left panel and select API Keys. You will see any existing keys there, including the default key if AI Studio created one for you.
This is also where you can create new keys. The API Keys page keeps your Gemini keys organized in one place. It shows unrestricted keys and keys restricted to the Gemini API, up to 100 keys.

3. Create a New Project or Select an Existing One
On the API Key management page, you will see a prominent button that says “Create API key.”

Before you can generate a Gemini API Key, you need a project in Google AI Studio. Click on the ‘Create API key‘ and you'll have two options:
- Select an existing project if you already have one. This keeps everything in the same place.
- Create a new project if you want to start fresh. Click “New Project” and give it a name that is easy to remember.

If you are a beginner and have never created a project before, click on the ‘+ Create project' and a new interface will pop up.
Give your project a name and hit the ‘Create project‘ button.

If your project is created successfully, you'll get it in the drop-down list.

Already use Google Cloud? AI Studio doesn't create a default project for existing Google Cloud accounts. Import your project first:
- Open the Dashboard from the left panel and select Projects.
- Click Import projects, search for your project, select it, and click Import.
- Go back to the API Keys page and create a key in that project.
Two things to know about extra projects:
- Verification: Additional projects, or projects created outside Google AI Studio, may need verification before they can use the free tier. Click Verify project on the Projects page, then Verify device, and scan the QR code with your Android phone or iPhone. Google says this works without sharing a phone number.
- Permissions: If Create API key is greyed out, your Google account needs IAM permissions such as Project Editor on that project. Ask your admin, or create a Cloud project that isn't tied to an organization.
4. Create a Gemini API Key
Now select the project name from the drop-down list and give your API key a name. Then hit the ‘Create Key‘ button.

After clicking the button, Google will instantly generate a new key for your project. The key is a long string of letters, numbers, and symbols. New keys are created as auth keys by default.
This key is what your apps or scripts use to access Gemini AI. It tells Google which account and project are making the requests. Without it, your app cannot communicate with the AI models.
Your key works with the models listed in Google's Gemini models docs. For new projects, Google recommends starting with Gemini 4 Hydrogen (gemini-4-hydrogen) or Gemini 3.5 Flash-Lite (gemini-3.5-flash-lite). Both are free of charge on the free tier. Some models, such as Gemini 4 Carbon, Argon and Neon and Gemini 3.1 Pro Preview, are paid tier only.
5. Copy Your API Key and Save It Securely
After creating your API key, you will see it displayed on the screen. It looks like a long mix of letters, numbers, and symbols. This key is unique to your account and project.

Copy it immediately and store it somewhere safe. You can save it in a password manager, a secure document, or as an environment variable in your computer. Treat it like a password because anyone with this key can access your Gemini AI account.
Do not share it publicly. Avoid posting it on forums, GitHub, or social media. If the key is exposed, someone else could use your free tier or cause unexpected charges.
For extra safety, Google allows you to restrict your API key. You can limit it to certain IP addresses, apps, or projects. This adds a layer of security so only your apps can use it.
If your key shows as Unrestricted on the API Keys page, click Add restrictions, choose Restrict to Gemini API only, then click Restrict key. This matters: Google now rejects unrestricted standard keys, and dormant unrestricted keys have been marked Blocked since May 7, 2026.
Once your key is safely stored, you are ready to start using Gemini AI in your apps, scripts, or experiments.
Important: Anyone with your API key can use your Gemini quota. Never share it publicly or commit it to GitHub. If your key is exposed, revoke it immediately and generate a new one.
Security Best Practice: How to Protect Your Gemini API Key

Once you have your unique key, the next and most critical step is ensuring it never falls into the wrong hands. A compromised API key can lead to unauthorized usage and unexpected billing charges.
Follow these best practices to keep your credentials safe.
A. The Danger of Hardcoding
Hardcoding means embedding the key directly within your application code, like this:
const apiKey = "AIzaSyB-Your-Key-Should-Not-Be-Here";// ... code continuesThis is the number one security risk. If you hardcode the key:
- Public Exposure: If you ever upload your code to a public repository (like GitHub), the key is instantly exposed to the world.
- Client-Side Leakage: In client-side environments (like pure JavaScript in a browser or a mobile app), the code can be easily viewed and the key extracted by anyone using the application.
The Golden Rule: Never commit your API key to any version control system (Git) or expose it on the client side.
B. The Solution: Environment Variables (Recommended)
The most secure way to handle your API key in a development environment is by using environment variables. This keeps the key external to your source code, so you can share your code without sharing your secret key.
The official Gemini client libraries (like Python's google-genai and JavaScript's @google/genai) automatically look for the GEMINI_API_KEY or GOOGLE_API_KEY variable. If both are set, GOOGLE_API_KEY takes priority, so set only one to avoid confusion.
1. How to Set GEMINI_API_KEY on macOS/Linux (Bash/Zsh)
You set the variable within your shell's profile file (e.g., ~/.zshrc or ~/.bashrc) to make it persistent across terminal sessions.
- Open your shell configuration file:
# For Zsh (default on newer macOS): nano ~/.zshrc # For Bash (common on older Linux/macOS): nano ~/.bashrc - Add the export command to the end of the file:
export GEMINI_API_KEY="AIzaSyB-Your-Actual-Key-Goes-Here" - Save the file and apply the changes by running:
source ~/.zshrc # or source ~/.bashrc
2. How to Set GEMINI_API_KEY on Windows
- Search for “Environment Variables” in the Windows search bar and select “Edit the system environment variables.”
- In the System Properties dialog, click the “Environment Variables…” button.
- Under “User variables” (for your account only), click “New…”.
- Set the Variable name to
GEMINI_API_KEY. - Set the Variable value to your actual Gemini API key.
- Click OK on all dialogs and open a new Command Prompt or PowerShell window to make the variable available.
Note for 2026 Developers: The latest google-genai Python SDK is now ‘zero-config.' If you name your environment variable GEMINI_API_KEY, the library will detect it automatically when you create a client with genai.Client(), so you don't pass the key in your code.
For production, Google recommends going one step further and storing keys in Google Cloud Secret Manager instead of plain environment variables.
C. Advanced Tip: API Key Restrictions
For production applications, you should restrict your API key usage to minimize damage if the key is ever leaked. You do this in the Google AI Studio or Google Cloud Console.
-
API Restrictions: Make sure your key is restricted to the Gemini API only. In AI Studio, an Unrestricted key shows an Add restrictions option. Choose Restrict to Gemini API only, then Restrict key. You need the
apikeys.keys.updatepermission, which comes with the API Keys Admin or Editor roles. Don't reuse a key that is shared with other Google APIs, because Google doesn't recommend it. -
Client Restrictions (Optional but Recommended): Limit where the key can be used:
- HTTP Referrers: If the key is used on a website, you can limit it only to your domain (e.g.,
*.yourdomain.com/*). - IP Addresses: If the key is used from a server, you can restrict it to the static IP address of that server.
- Mobile Apps: For native Android/iOS apps, you can restrict it by the application ID and signing certificate hash.
- HTTP Referrers: If the key is used on a website, you can limit it only to your domain (e.g.,
Pro Tip: Use a Server-Side Proxy If you are building a front-end website (like a React or Vue app) that needs to use the key, the most secure method is to route all API calls through your own backend server. The key is stored securely on the server as an environment variable, and the front-end only communicates with your secure endpoint, never touching the Gemini API key itself.
D. Set Billing Alerts and Know Your Leak Plan
If you enable billing, set billing alerts in the Google Cloud console so a leaked key can't run up a surprise bill.
If a key does leak, Google's recommended order is:
- Create a new key.
- Deploy it to your apps.
- Disable the old key once the new one works.
- Audit usage in the Google Cloud console.
Don't delete the old key until the new one is live, or your app will break in between. Google also blocks keys that are reported as leaked, so a leaked key may stop working on its own.
E. Migrate Old "Standard" Keys
Check the Key Type column on the API Keys page. If any key says Standard, create a new auth key, update your apps and deployments, test them, then delete or revoke the old key.
How to Validate Your Newly Created Gemini API Key
Once you've securely saved your key as an environment variable, it's time to confirm that it's active and working correctly. This verification step ensures you can connect to the Gemini API before you start writing complex code.
Verification
The most straightforward way to verify your key is to run a simple API call. Since the key is now stored as the GEMINI_API_KEY environment variable on your system, you can use a command-line utility like cURL to make a direct HTTP request to the Generative Language API.
A successful response (HTTP status code 200) containing generated text confirms that:
- Your key is valid.
- Your connection to the Gemini API is functional.
- The specified model (
gemini-4-hydrogen) is accessible.
A quick models.list call is not enough on its own. Google notes that a 200 OK from models.list only confirms the key exists. It doesn't confirm your project can actually generate content, so test with a real generation request like the one below.
B. Test Command (using cURL)
Before writing real application code, it’s best to confirm that your key works with a simple test request.
Open a new terminal window (macOS/Linux) and execute the following command. It matches the request format in Google's current API key docs, which use the Interactions endpoint and pass your key in the x-goog-api-key header:
curl "https://generativelanguage.googleapis.com/v1beta/interactions" \ -H 'Content-Type: application/json' \ -H "x-goog-api-key: $GEMINI_API_KEY" \ -H "Api-Revision: 2026-05-20" \ -X POST \ -d '{ "model": "gemini-4-hydrogen", "input": "Explain how AI works in a few words" }'Expected Successful Output (abbreviated): If successful, you will receive a JSON response with fields such as id, model, status, steps and usage. The model's answer sits in a text field inside steps:
{ "id": "...", "model": "gemini-4-hydrogen", "status": "...", "steps": [ { "type": "model_output", "content": [ { "type": "text", "text": "AI learns patterns from data to make predictions." } ] } ], "usage": { ... }}If you prefer the older generateContent method, it is still listed in Google's API reference at POST /v1beta/models/{model}:generateContent, and it uses the same x-goog-api-key header.
If you receive an HTTP 400 or 403 error, carefully re-check that you correctly copied your key and set the environment variable as described in the previous section. The error section below covers each status code in detail.
Gemini API Key Free Tier Limits and Pricing
The Gemini API offers a Free Tier designed for developers, students, and hobbyists to learn, experiment, and prototype applications. New accounts start on the Free Tier. Google's billing docs only ask for a payment method when you set up the paid tier, and the AI Studio usage itself "remains free of charge unless users link a paid API key."
A. Free Tier Highlights and Limitations
The Free Tier is meant for development, not high-traffic production use. Google describes it as "limited access to certain models" with free input and output tokens. Your access is governed by rate limits, which are usually measured across three dimensions and vary by the specific model you call:
- Requests Per Minute (RPM): The number of API calls you can make in a 60-second window.
- Tokens Per Minute (TPM): The number of input tokens processed in a 60-second window.
- Requests Per Day (RPD): The total number of requests allowed per day. RPD quotas reset at midnight Pacific Time.
Some models also have other limits, such as images per minute or tokens per day.
What are the exact free tier numbers? Google's rate limits page does not publish a general table of free-tier RPM, TPM and RPD for each model. Instead, it says to view your active rate limits in AI Studio. Open your project in AI Studio to see the limits that apply to you right now. Treat any fixed numbers you see on other sites with caution, because they change often.
Here is which popular models are free, based on Google's official pricing page as of November 2026 (paid prices are per 1 million tokens, in USD, standard text pricing):
| Model | Free Tier | Paid input | Paid output |
|---|---|---|---|
| Gemini 4 Hydrogen | Free of charge | $1.50 | $7.50 |
| Gemini 3.8 Flash | Free of charge | $0.75 ($1.50 from Jan 1, 2027) | $3.75 ($7.50 from Jan 1, 2027) |
| Gemini 3.5 Flash-Lite | Free of charge | $0.30 | $2.50 |
| Gemini 3.1 Flash-Lite | Free of charge | $0.25 | $1.50 |
| Gemini 4 Carbon | Not available | $2.00 ($4.00 from Dec 1, 2026) | $10.00 ($20.00 from Dec 1, 2026) |
| Gemini 3.1 Pro Preview | Not available | $2.00 (prompts up to 200k tokens) | $12.00 (prompts up to 200k tokens) |
*Note: Free tier limits and prices change often. Check the pricing and rate limits pages before you build on any number here.
Pro Tip: On the free tier, Google says your content is used to improve its products. On the paid tier, it is not. Keep private or customer data off a free-tier key.
Grounding with Google Search: For Gemini 3 and newer models, Google's pricing page lists Search grounding as "Not available" in the Free Tier column. On the paid tier, you get 5,000 free search requests per month (shared across Gemini 3 and newer models), then $14 per 1,000 requests.
Important Note: If you exceed any of these limits, the API will return a rate limit error (HTTP 429). If you consistently hit these limits, you can click Set up billing on the API Keys or Projects page to move to a paid tier with higher limits.
Usage Tiers at a Glance
According to Google's rate limits page, your tier depends on your billing setup and spending:
| Tier | How you qualify | Billing cap |
|---|---|---|
| Free | Active project or free trial; some projects need verification | None |
| Tier 1 | Set up and link an active billing account | $250 |
| Tier 2 | Paid $100 + 3 days since first successful payment | $2,000 |
| Tier 3 | Paid $1,000 + 30 days since first successful payment | $20,000 to $100,000+ |
A few billing facts worth knowing before you upgrade:
- Moving to the paid tier means linking a billing account and prepaying at least $5 (Prepay), or choosing Postpay where offered.
- If your prepaid balance hits $0, API keys in linked projects stop working and return HTTP 402 until you add credits. Your project does not fall back to the free tier automatically.
- The $300 Google Cloud Welcome credit cannot be used for Gemini API usage. Since March 2026, Gemini API costs are excluded from the Google Cloud Free Trial.
- Requests that fail with a 400 or 500 error are not charged, but they still count against your quota.
B. When to Upgrade to Production (Vertex AI)
The Gemini Developer API (accessed via Google AI Studio and your API key) is the “fast path” for building. However, for serious, large-scale, or enterprise applications, the recommended step is to transition to the Vertex AI Gemini API.
| Feature | Gemini Developer API (AI Studio) | Vertex AI Gemini API (Google Cloud) |
|---|---|---|
| Use Case | Prototyping, hobby projects, rapid development | Enterprise, mission-critical applications, large-scale deployment |
| Authentication | Simple API Key | Identity and Access Management (IAM) service accounts |
| Data Control | Data may be used for product improvement (in the Free Tier) | Enterprise-grade security, data residency, and compliance controls |
| Scalability | Rate limits require billing configuration to increase | Designed for massive scale with guaranteed Service Level Agreements (SLAs) |
The decision is simple: If your application is moving beyond prototyping and needs guaranteed performance, advanced security, or compliance features, migrate to Vertex AI.
Common Gemini API Key Errors and How to Fix Them
Most problems with a new key show up as an HTTP status code. Here is what each one means and what to do, based on Google's troubleshooting guide and API errors reference.
| Status | What it means | How to fix it |
|---|---|---|
| 400 | The request is malformed, has an invalid or unknown parameter, or a prerequisite (like billing) isn't met | Check your request body and parameters against the API reference. Remove unknown fields. Confirm billing if your setup needs it. Don't retry unchanged. |
| 402 | Your prepaid credit balance is used up | Add credits or turn on auto-reload. Don't retry until credits are added. |
| 403 | Permission denied: the key lacks access, the project needs verification, the free tier isn't available for the project, billing has an outstanding balance, or the key was reported as leaked | Check the key's permissions and restrictions, verify the project, and check billing. If the message says the key was leaked, create a new key in AI Studio. |
| 404 | The resource or model wasn't found | Check the model name against the models page and make sure you're calling the right API version (/v1 or /v1beta). Beta features only work on /v1beta. |
| 429 | Too many requests: you hit a per-minute limit or your daily quota | Wait and retry with exponential backoff. For daily quota, wait for the reset at midnight Pacific Time, or move to a paid tier. |
| 500 | An unexpected error on Google's side | Retry. If it keeps happening, contact support. |
| 503 | The service is temporarily overloaded or down | Wait and retry with exponential backoff, or try again later. |
| 504 | The request didn't finish before its deadline | Adjust your client deadline or use the server default. |
"Your API key was reported as leaked"
If you see this message (usually with a 403 PERMISSION_DENIED), Google has blocked the key. It won't work again. Generate a new key in Google AI Studio, update your credentials everywhere, and check the key's status on the API Keys page. Then find out how it leaked, for example a public GitHub repo or frontend code.
My key is "Blocked" or rejected
Google now rejects unrestricted standard keys, and dormant unrestricted keys have been marked Blocked since May 7, 2026. Create a new auth key, or restrict your key to the Gemini API only, as shown in the security section above.
When to retry and when not to
Google recommends retrying only transient errors: 429, 503, 408 and other 5xx codes. Use exponential backoff with jitter and a maximum retry count. Don't retry client errors such as 400, 402 or 403, because the same request will fail again until you fix the cause.
If a key or quota problem left you with unexpected charges, Google says to submit a billing support case through the Google Cloud console.
FAQs – How to Generate a Gemini API Key Without a Credit Card
How long does a Gemini API key last?
A Gemini API key does not expire automatically. It stays active until you delete it, restrict it, or regenerate it manually in Google AI Studio. If a key is compromised, you should revoke it immediately and create a new one. One exception: Google has blocked dormant unrestricted keys since May 7, 2026, so restrict your key to the Gemini API to keep it working.
Can I use one Gemini API key for multiple apps?
Yes, you can use one Gemini API key across multiple applications as long as they belong to the same Google AI Studio project. However, for better security and tracking, it’s recommended to create separate keys for different apps or environments.
What happens if my Gemini API key is exposed?
If your Gemini API key is exposed, anyone can use it to make API requests under your quota. This can exhaust your free tier limits or lead to unexpected charges if billing is enabled. You should revoke the exposed key immediately and generate a new one.
Is a credit card required to use the Gemini API free tier?
No, Google's docs don't ask for a credit card to use the free tier. New accounts start on the Free Tier, and a payment method only comes up when you click Set up billing to move to the paid tier. Some extra projects need verification first, which is done by scanning a QR code with your phone, not with a card.
Can I restrict where my Gemini API key is used?
Yes, Google allows you to restrict your Gemini API key. You can limit usage by HTTP referrers, IP addresses, or specific applications. Restrictions reduce the risk of misuse if the key is leaked.
Why am I getting a 403 or 429 error from the Gemini API?
A 403 error means permission was denied. Your key may lack access, your project may need verification or have a billing problem, or the key may have been reported as leaked. A 429 error means you’ve exceeded your rate limits, such as requests per minute or requests per day. Check your key settings and your active rate limits in Google AI Studio.
Should I use my Gemini API key in frontend JavaScript?
No, you should not expose your Gemini API key in frontend JavaScript. Client-side code can be viewed and copied by anyone. The safest approach is to store the key on a backend server and route requests through a secure API endpoint.
Is the Gemini API free to use?
Yes, you can use the Gemini API for free on the Free Tier, within rate limits. Free access covers certain models, such as Gemini 4 Hydrogen, Gemini 3.8 Flash and Gemini 3.5 Flash-Lite. Some models, like Gemini 4 Carbon and Gemini 3.1 Pro Preview, are paid only. On the free tier, Google may use your content to improve its products.
Why is my Gemini API key not working?
First, check that the key is copied correctly and set as GEMINI_API_KEY or GOOGLE_API_KEY. If both variables are set, GOOGLE_API_KEY wins, which can load an old key. Next, check the key on the API Keys page: unrestricted standard keys are now rejected, and keys reported as leaked are blocked. Finally, make sure your project doesn't need verification and that you're calling a model your tier can use.
Where can I find my existing Gemini API key?
Open Google AI Studio and go to the API Keys page from the Dashboard. It lists unrestricted keys and keys restricted to the Gemini API, up to 100 keys. If you don't see a key there, check the Credentials page in the Google Cloud console. For an existing Google Cloud project, you may need to import the project into AI Studio first.
Start Using Your Gemini API Key Today
Generating a Gemini API Key is simple. You can create it in a few steps and start exploring Google’s AI models right away.
Keep your key safe and follow the security tips. Test it to make sure it works, and be aware of free tier limits to avoid issues.
Now that you have your key, you can start building apps, running experiments, or learning AI. Grab your Gemini API Key and start creating today!
If you want to create the ChatGPT API Key as well, follow our step-by-step tutorial on generating ChatGPT API Key and create it with ease!
Don't forget: your new API key also works with Nano Banana, Google’s image generation model (the current version is Nano Banana 2.1). You can generate 4K visuals and perform ‘Conversational Editing', where you tell the AI to change specific parts of an image using just text, all through the same API connection. Note that Google's pricing page lists Nano Banana 2.1 as paid tier only, so you'll need billing set up to use it.
That being said, if you face any complications while creating a Gemini API Key for your project, feel free to share that with us using the comment box below. We would love to address your questions. Take care!






