How to get a Gemini API key and make your first call in Python

Day 2, 30 minutes · beginner

Goal of this lessonCreate a Gemini API key, keep it out of your code and git, and run a Python script that sends one request to Gemini 4 Hydrogen.

Why it matters

An API key works like a password that also pays the bills. Anyone who has it can send requests as you and use up your limits. The most common way a key leaks is the dullest one: it is pasted into a script, and the script lands on GitHub.

Today Wilmot's helper gets its first real code. You create a key in AI Studio, store it where code can read it but git cannot see it, and run a short Python script that sends one question to Gemini and prints the answer. You finish with a project folder that is safe to put in a repository, and a first call that prints its own token use.

What the key does

Every Gemini API key belongs to a Google Cloud project. The project decides your limits and, on the paid tier, your billing. AI Studio makes a default project and key for new users once they accept the terms of service, and you can copy it from the API keys page. If you need another key, you click Create API key and follow the dialog.

Two changes to know about, both from Google's key page. Since 28 May 2026, new keys made in AI Studio are authorization keys, which are tied to a service account and are limited to the Gemini API by default. And the Gemini API rejects requests that use an unrestricted standard key. If a key from an old tutorial stops working, that may be why: make a new one.

Keep the key out of your code

Google's recommended way is an environment variable called GEMINI_API_KEY (or GOOGLE_API_KEY). The client library looks for either one. If both are set, GOOGLE_API_KEY wins, so a forgotten old variable can silently override the key you just made.

An environment variable set in a terminal disappears when you close it. A common habit is a .env file: a plain text file in your project folder with one line per setting. A small package called python-dotenv (it is not from Google) copies those lines into the environment when your script starts. This is the same idea as in the Claude API for Beginners course, so if you did that one, you already know it.

The important part is the order:

  1. Write .gitignore first. It lists files git must leave alone, and .env goes on it.
  2. Then create .env with the key.
  3. Then start git and run git status. The .env file must not appear in the list.

If a key was ever committed, ignoring the file later does not remove it from history. Make a new key and remove the old one on the API keys page.

The SDK and the first call

Google's official Python library is google-genai, and you import it as from google import genai. Do not use the older google-generativeai package. Google now calls the Interactions API the recommended interface for new projects. It became generally available in June 2026 and needs google-genai version 2.3.0 or later. The older generateContent API still works, and you will meet it in older code, but this course uses client.interactions.create.

The first script sets four things:

  • model: gemini-4-hydrogen, which is free on the free tier.
  • system_instruction: the standing rules, as in AI Studio.
  • input: the visitor's question.
  • generation_config: settings for this call. Here thinking_level is low. Hydrogen thinks before it answers, and its default level is high. Thinking tokens are billed like output, so low is quicker and cheaper for simple questions. Gemini 4 models ignore temperature, top_p and top_k, so leave those out.

The answer is in interaction.output_text. The script also prints status (it should say completed) and usage, which lists the tokens used. Watching usage from day one is a good habit.

If something fails

  • 403 with FREE_TIER_VERIFICATION_REQUIRED: the project needs the one-time phone check. The error message includes a link.
  • 429: you hit a limit. Wait and try again later.
  • No key found: the terminal does not see your .env. Run the script from the project folder.

The steps

  1. Create the key and store it in a password manager.
  2. Make the project folder and a virtual environment.
  3. Write .gitignore, then .env, then check with git status.
  4. Install the two packages.
  5. Write config.py, the shared setup the later lessons reuse.
  6. Write and run first_call.py.

Pick your tool below and follow the steps.

Common mistakes

  • Putting the key in the Python file "just for now".
  • Creating .gitignore after the first commit.
  • Writing spaces or quotes in .env. Write GEMINI_API_KEY= directly followed by the key.
  • Installing the wrong package name.
  • Forgetting to turn on the virtual environment in a new terminal.

Today's actions

Ticks are not saved without an account

Do it in Gemini API

Gemini API

  1. Open aistudio.google.com/api-keys. A new account usually already has a default project and key; if you need one, click Create API key and follow the dialog. Copy the key into your password manager. Never paste it into a chat, an email or a code file.
  2. In a terminal, make a project folder and a virtual environment: mkdir skerrimoor-desk, cd skerrimoor-desk, python -m venv .venv. Turn it on with source .venv/bin/activate on macOS or Linux, or .venv\Scripts\Activate.ps1 on Windows.
  3. Create .gitignore first, with this content. It tells git to ignore your key file, the virtual environment and caches.
    .env
    .venv/
    __pycache__/
  4. Create .env with one line, using your real key in place of the fake text. No spaces and no quotes. Then run git init and git status: .env must not be in the list.
    GEMINI_API_KEY=paste-your-key-here
  5. Install the two packages: pip install -U google-genai python-dotenv. Then run pip show google-genai and check that the version is 2.3.0 or higher.
  6. Create config.py. Every later lesson imports the model name and the client from here.
    """Shared setup for the Skerrimoor desk helper."""
    from dotenv import load_dotenv
    from google import genai
    
    load_dotenv()  # copies GEMINI_API_KEY from your .env file into the environment
    
    MODEL = "gemini-4-hydrogen"
    client = genai.Client()  # finds the key in the environment, never in this file
  7. Create first_call.py with this code. It sends one question with standing rules and a low thinking level.
    from config import MODEL, client
    
    interaction = client.interactions.create(
        model=MODEL,
        system_instruction=(
            "You answer questions for Skerrimoor Lighthouse Visitor Centre. "
            "Be friendly and keep every answer under 60 words."
        ),
        input="Is the lighthouse tour suitable for a six year old?",
        generation_config={"thinking_level": "low"},
    )
    
    print(interaction.output_text)
    print("status:", interaction.status)
    print("usage:", interaction.usage)
  8. Run python first_call.py. You should see a short answer, status: completed and a usage line with token counts. If you see a 403 about verification, follow the link in the message.

Note: If both GEMINI_API_KEY and GOOGLE_API_KEY are set, GOOGLE_API_KEY wins. New AI Studio keys are authorization keys since 28 May 2026, and unrestricted standard keys are rejected. If a key leaks, make a new one and remove the old one. python-dotenv is a common third-party package, not from Google.

Checked on 4 December 2026 · Source: Google AI Studio docs: Using Gemini API keys (opens in a new tab)

Create a free account to save your progress. Sign in or sign up