Getting an API key safely
Free tiers, spending limits and keeping keys secret.
🎬 Video version coming soon — read it now
An API key is like your ATM PIN plus your debit card: anyone who has it can spend your money. Treat it that way from day one.
Option 1: Google AI Studio (free, recommended for students)
- Go to aistudio.google.com and sign in with your Google account.
- Click Get API key → Create API key.
- Copy it. Save it as
GEMINI_API_KEY(see below).
The free tier has daily and per-minute limits that are plenty for this course. Two things to know: on the free tier, Google may use your prompts to improve its products, so never send private or personal data; and limits and available models change, so check the AI Studio page for the current ones.
Option 2: OpenRouter (one key, many models)
- Sign up at openrouter.ai and create a key under Keys.
- Save it as
OPENROUTER_API_KEY. - On the Models page, models marked free cost nothing (with daily limits). Paid models need credits.
OpenRouter is great for trying the same prompt on different models and picking what works best for your budget.
Option 3: Anthropic, OpenAI and other paid providers
- Sign up at the provider's console (for Claude, console.anthropic.com).
- Add billing, or use any free credits offered.
- Go to API Keys → Create key, name it something like "python-course", and copy it. You usually can't see it again.
Set a spending limit first
On any paid account, set a monthly spend limit in the billing settings before you write code. A bug like an infinite loop of API calls then stops at your limit instead of draining your account. Free tiers protect you automatically: you just hit the limit and wait.
Never put the key in your code
# NEVER do this
client = anthropic.Anthropic(api_key="sk-ant-...")Code gets shared, screenshotted and pushed to GitHub. Bots scan GitHub for leaked keys within minutes.
In Colab: use Secrets
- Click the key icon (Secrets) in Colab's left sidebar.
- Add a secret named after your provider (
GEMINI_API_KEY,OPENROUTER_API_KEYorANTHROPIC_API_KEY) and paste your key as the value. - Turn on notebook access, then:
import os
from google.colab import userdata
os.environ["GEMINI_API_KEY"] = userdata.get("GEMINI_API_KEY") # use your secret's nameThe key lives in your Google account, not in the notebook, so sharing the notebook doesn't share the key.
On your laptop: environment variables
# Mac / Linux terminal
export ANTHROPIC_API_KEY="sk-ant-..."
# Windows PowerShell
$env:ANTHROPIC_API_KEY="sk-ant-..."Or keep it in a .env file and load it with the python-dotenv package. Then add .env to .gitignore before your first commit.
The official SDKs read their key variable automatically (the Gemini SDK reads GEMINI_API_KEY, the Anthropic SDK reads ANTHROPIC_API_KEY), so your code never mentions the key at all.
If a key leaks
Go to the console, delete the key immediately and create a new one. Deleting it is instant and costs nothing; a leaked key can cost a lot.
In one line
Set a spending limit, keep your key in Colab Secrets or an environment variable, never in code, and delete it at once if it leaks.
Share with a friend