Skip to content

Lesson 2 of 6

Get Ready: Google AI Studio and API Key

30 minUpdated 13 Nov 2026Part of Build Your First Google Gemini AI Agent
On this page
  1. What you will learn
  2. What Google AI Studio is for
  3. Create your API key
  4. Quotas, free tier, billing and model availability
  5. Keeping the key safe
  6. Set up the project folder
  7. Expected output
  8. Common errors and fixes
  9. Validation checklist
  10. Hands-on exercise
  11. Knowledge check
  12. References

Before you can write any agent code you need two things: access to the Gemini API, and a safe way to hold the secret that proves it is you. This lesson covers both. It is mostly clicking and configuring, but the habits you form here matter more than any code later. A leaked API key is one of the most common and most avoidable mistakes in AI projects.

You will meet Google AI Studio, create an API key, learn why limits and billing differ between people, and set up the gemini-order-helper project folder with a .env file and a .gitignore. Nothing in this lesson sends a request to Gemini; that begins in lesson 3.

What you will learn

  • Explain what Google AI Studio is used for and how it differs from the Gemini API.
  • Create an API key by following the current official steps.
  • Explain why quotas, free-tier access, billing and model availability can vary.
  • Store the key in a .env file excluded from version control and keep it out of frontend code.

Before you start

  • A Google account you are allowed to use for this work.
  • A terminal and Python 3.10 or newer. Check with python --version, or python3 --version on macOS and Linux.
  • A text editor.

What Google AI Studio is for

Google AI Studio is a web workspace for trying Gemini models without writing code. You can type prompts, adjust settings, compare responses and, importantly for us, create and manage API keys. Think of it as a workshop bench: good for experiments and for getting your credentials, but not where your finished agent lives.

The Gemini API is different. It is the web service your own program talks to. Your agent will send requests to it, authenticated with your key. AI Studio and the API are connected because the key you make in one is used in the other, but they are two separate things: one is an interface for people, the other an interface for programs.

Create your API key

The exact buttons and screens can change, so use Google's current page as your source of truth: the API key documentation (opens in a new tab). At the time of writing, the path looks like this.

  1. Step 1: Sign in to Google AI Studio

    Visit aistudio.google.com while signed in to your Google account and accept the terms if asked. New users are normally given a default project and key as part of this, according to the documentation.

    What you should see: You can see the AI Studio workspace.

  2. Step 2: Open the API keys page

    Follow the link to the API keys page named in the official documentation, then choose the option to create a key.

    What you should see: A key appears in a list, shown with the project it belongs to.

  3. Step 3: Copy the key once, into a secure place

    Copy it straight into the .env file you create below. Do not paste it into a chat, a document or a screenshot. If you do so by accident, treat the key as leaked and replace it.

    What you should see: The key is in your .env file and nowhere else.

  4. Step 4: Note the project and any restrictions

    The documentation describes options for restricting what a key may do. For a learning project, the defaults on the key page are fine; if the page shows restriction options, read them and consider the narrowest setting that works.

    What you should see: You know which project the key belongs to.

Quotas, free tier, billing and model availability

Beginners often ask: "Is this free, and how much can I use?" The honest answer is that it depends, and it changes. This course states no figures, because any figure written here would go out of date or be wrong for your account.

  • Quotas limit how many requests or how much text you can send in a period. They can differ by model, by account and by whether billing is set up.
  • Free-tier access may or may not apply to your account, region or model. Google's documentation says that free-tier access and billing setup vary, and that some projects may need verification first.
  • Billing pays for usage beyond what is free, where it applies. Being able to use a chat product with a subscription is separate from being able to use the API.
  • Model availability varies. Some models are not offered in every region or to every account, and models are retired over time. A model that works for a tutorial today may be replaced later.

Practical consequences: check your own limits in your account, treat the model name in your code as something to update, and keep requests small while learning. If you decide to enable billing, look for a way to set budget alerts in Google's cloud console, as the documentation recommends, so that a bug in a loop cannot silently run up a bill.

Keeping the key safe

An API key is a password for spending your quota and, possibly, your money. Anyone holding it can make requests as you. Three rules cover almost every case.

  1. Never put the key in public frontend code. Anything shipped to a browser or mobile app can be read by anyone. A web or mobile app should call your own server, and only that server holds the key.
  2. Never commit the key to a repository, even a private one. Repositories get shared, forked and copied, and history keeps secrets forever.
  3. Keep it in configuration outside the code, such as an environment variable, so you can change or revoke it without editing your program. The Twelve-Factor App (opens in a new tab) explains this idea.

Our project is a terminal program on your own computer, so the key stays on your machine. The approach is a .env file: a plain text file of NAME=value lines. A small library, python-dotenv, reads it when the program starts and loads the values into the environment. A .gitignore file tells Git never to track .env. For more on this pattern, see managing API keys locally.

Google's SDK reads the key from an environment variable. The documentation names GEMINI_API_KEY and also GOOGLE_API_KEY, and says that if both are set, the second one wins. To avoid a confusing clash, use only GEMINI_API_KEY and make sure GOOGLE_API_KEY is not set in your shell.

Set up the project folder

Now create the folder, a virtual environment and the two small files. A virtual environment is a private copy of Python's packages for one project, so installing the SDK here does not affect anything else on your computer.

  1. Step 1: Create the folder and enter it

    Run this in a terminal. The same commands work on macOS, Linux and Windows.

    Bashterminal
    mkdir gemini-order-helper
    cd gemini-order-helper

    What you should see: Your terminal prompt shows the new folder.

  2. Step 2: Create a virtual environment

    On macOS and Linux you may need python3 instead of python. The command is the same on Windows.

    Bashterminal
    python -m venv .venv

    What you should see: A hidden folder named .venv appears in your project.

  3. Step 3: Activate it

    Activation differs by system. After it, your prompt usually starts with (.venv). If PowerShell blocks the script, the policy note in the official venv documentation explains how to allow it for your user.

    Bashterminal
    # macOS and Linux
    source .venv/bin/activate
    
    # Windows PowerShell
    .venv\Scripts\Activate.ps1
    
    # Windows Command Prompt
    .venv\Scripts\activate.bat

    What you should see: The prompt shows (.venv).

  4. Step 4: Create .gitignore before .env

    Do this first so that you can never commit the key by accident. Create a file named .gitignore with these lines.

    Text.gitignore
    # Secrets: never commit these
    .env
    
    # Local tooling
    .venv/
    __pycache__/

    What you should see: A .gitignore file exists in gemini-order-helper.

  5. Step 5: Create .env with your key

    Replace the placeholder with the key you copied. There are no quotes and no spaces around the equals sign.

    Text.env
    GEMINI_API_KEY=paste-your-key-here

    What you should see: A .env file exists, and git status (if you use Git) does not list it.

What your folder should look likeIllustrative — your output will differ
gemini-order-helper/
  .venv/
  .env
  .gitignore

Expected output and limitations

No program output yet. Success is a project folder with an active virtual environment, a .gitignore that lists .env and a .env file holding a single GEMINI_API_KEY line.

Sample responseIllustrative — your output will differ
(.venv) $ ls -a
.  ..  .env  .gitignore  .venv

Limitations to keep in mind

  • AI Studio's screens and the documentation's wording can change, so your steps may differ slightly from the description here.
  • Whether you can use the API for free, and with what limits, depends on your own account.

Common errors and how to fix them

The Create API key button is missing or greyed out.

Why it happens: Your account may lack permission in the project, or you may be using an organisation account with restrictions.

Fix: Read the API key documentation's section on permissions and ask your account administrator, or use a personal account that is allowed for this purpose.

Activate.ps1 cannot be loaded because running scripts is disabled.

Why it happens: PowerShell's execution policy blocks local scripts by default on some machines.

Fix: Use Command Prompt and activate.bat instead, or follow the official venv documentation's note on changing the policy for your user only.

git status lists .env as a new file.

Why it happens: The .gitignore line is missing, misspelt or was created after the file was already tracked.

Fix: Check that .gitignore contains the line .env exactly. If Git already tracks it, remove it from tracking and treat the key as exposed: create a new key and disable the old one.

The key was pasted somewhere public.

Why it happens: It was shared in a chat, screenshot or commit.

Fix: Create a replacement key, update your .env, then disable the old key. The API key documentation describes this order so nothing breaks in between.

Validation checklist

Tick each item once you have confirmed it.

Hands-on exercise

Audit your own setup

Write a short note answering: where could my key be read from right now? List every place it exists (the AI Studio key page, your .env, your shell history, any notes). Decide which of those places could be removed or restricted, and write how you would replace the key if it leaked.

You will need

  • Your project folder.
  • The API key documentation page.

Acceptance criteria

Your work is done when every one of these is true.

Hints (2)
  • Check your terminal history if you ever typed the key into a command.
  • Search your project folder for the first few characters of the key to see whether it appears anywhere unexpected.
Show a model answer

A good note finds that the key is in the AI Studio list and in .env, and perhaps in shell history if it was exported by hand. Only .env is in your project, and .gitignore covers it. The rotation plan is: create a replacement key, update .env, confirm the program works, then disable the old key and check usage for anything unexpected.

Knowledge check

Choose an answer, then check it. Nothing is recorded — this is for you.

  1. 1.Where should the Gemini API key live in a terminal project like this one?
    Show the answer and explanation

    Answer: B. A .env file kept out of version control separates the secret from the code. Hard-coding it is tempting because it is quick, but it ends up in every copy of the file and in Git history.

  2. 2.A friend says the free tier gives everyone the same limits on every model. What is the best response?
    Show the answer and explanation

    Answer: B. Google's documentation says free-tier access and billing vary, and limits depend on several factors. The first option is tempting because it sounds simple, but a single shared rule does not exist.

  3. 3.Why must the key never appear in public frontend code?
    Show the answer and explanation

    Answer: B. Code sent to a browser is readable by every visitor. The usual solution is a backend you control that holds the key. The third option is wrong because frontend code can call APIs; that is the problem, not a protection.

Written by
PromptFable Editorial
Last updated 13 Nov 2026
Review
Reviewed by Waseem Nadir, Technical Reviewer
Last reviewed 27 Nov 2026

References

Primary sources for the technical details in this lesson. Vendor platforms change — check the official page if anything here looks different from what you see.

PromptFable is independent and is not affiliated with, endorsed by or sponsored by Anthropic, Google, OpenAI or any other vendor. Claude, Gemini, Google AI Studio and other product names are trademarks of their respective owners.