Day 2, 30 minutes · beginner
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:
- Write
.gitignorefirst. It lists files git must leave alone, and.envgoes on it. - Then create
.envwith the key. - Then start git and run
git status. The.envfile 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_levelislow. Hydrogen thinks before it answers, and its default level ishigh. Thinking tokens are billed like output, solowis quicker and cheaper for simple questions. Gemini 4 models ignoretemperature,top_pandtop_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
- Create the key and store it in a password manager.
- Make the project folder and a virtual environment.
- Write
.gitignore, then.env, then check withgit status. - Install the two packages.
- Write
config.py, the shared setup the later lessons reuse. - 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
.gitignoreafter the first commit. - Writing spaces or quotes in
.env. WriteGEMINI_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 accountDo it in Gemini API
Gemini API
- 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. - In a terminal, make a project folder and a virtual environment:
mkdir skerrimoor-desk,cd skerrimoor-desk,python -m venv .venv. Turn it on withsource .venv/bin/activateon macOS or Linux, or.venv\Scripts\Activate.ps1on Windows. - Create
.gitignorefirst, with this content. It tells git to ignore your key file, the virtual environment and caches..env .venv/ __pycache__/ - Create
.envwith one line, using your real key in place of the fake text. No spaces and no quotes. Then rungit initandgit status:.envmust not be in the list.GEMINI_API_KEY=paste-your-key-here - Install the two packages:
pip install -U google-genai python-dotenv. Then runpip show google-genaiand check that the version is 2.3.0 or higher. - 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 - Create
first_call.pywith 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) - Run
python first_call.py. You should see a short answer,status: completedand 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.
Create a free account to save your progress
Free, and no payment details. Your ticks and finished lessons are kept, on every device.
Create a free account I already have an accountCreate a free account to save your progress. Sign in or sign up