Setup: keys, env vars, .gitignore, virtual envs
The single most expensive beginner mistake in AI is committing an API key to GitHub. Bots scan within minutes. Bills hit within hours. This lesson prevents that.
API keys are house keys. You do not tape them to your front door. You do not paste them in a public chat. You do not push them to GitHub.
The safe setup pattern:
- Generate an API key from the provider dashboard.
- Store it in a local
.envfile. - Add
.envto.gitignoreBEFORE the first commit. - Load it via
python-dotenvoros.environ. - Rotate keys quarterly.
- Use spending limits in the provider dashboard.
.env file (never commit):
OPENAI_API_KEY=sk-...
GEMINI_API_KEY=...
ANTHROPIC_API_KEY=sk-ant-...
.gitignore (commit this):
.env
__pycache__/
*.pyc
.venv/
Loader code:
from dotenv import load_dotenv
import os
load_dotenv()
key = os.environ["OPENAI_API_KEY"]
Virtual env setup:
python -m venv .venv
source .venv/bin/activate # Mac/Linux
.venv\Scripts\activate # Windows
pip install openai python-dotenv
Quick recall
3 prompts · think before you flip
Prompt 1 of 3
Why use `.env` instead of hard-coding?
Quiz time
1 question · tap an answer to check it
1. If you commit an API key by accident, the first move is
Finished lesson 5.2?
Mark complete to update your module progress and unlock the streak.
Loading