Skip to content

Install & config guide: your AI provider API key (and .env)

Needed from M4 onward. An API key is a secret password that lets your code call an AI model over the internet. This guide walks you through making one and storing it safely. Read the secrets-hygiene box at the end, keeping keys out of your code is a real security habit, not a formality.

This course uses Claude (by Anthropic). The steps are the same idea for any provider; we walk Claude in detail. Official docs (verify they open): Anthropic Console, https://console.anthropic.com · Python SDK, https://github.com/anthropics/anthropic-sdk-python.

The Console's exact buttons may have changed since this was written, if a label doesn't match, look for the nearest equivalent (Settings → API keys → Create) and tell your instructor.


Step 1: Create an account

Go to https://console.anthropic.com and sign up (email or Google). Verify your email if asked.

You should now see: the Anthropic Console dashboard after signing in.

Step 2: Add a little credit (and a spend cap)

API usage is pay-as-you-go, billed per word ("token"). Open Billing (or Plans & Billing), add a small amount (a few dollars is plenty for this whole course), and, importantly, set a monthly spend limit so you can never be surprised.

You should now see: a positive credit balance and, ideally, a spend limit you chose. (A few dollars covers thousands of small learning calls. Each tiny message costs a fraction of a cent.)

Cost-savvy tip: the cheapest current model, claude-haiku-4-5, costs about 5× less than the default claude-opus-4-8. You change models by editing one string in your code (M6 goes deeper). For lots of practice runs, Haiku is gentle on your balance.

Step 3: Create an API key

Open API keys (often under Settings). Click Create Key, give it a name like ai-course, and create it.

You should now see: a key starting with sk-ant-.... Copy it now: the Console only shows it once. (If you lose it, just delete it and make a new one, no harm done.)

Step 4: Put the key in a .env file (never in your code)

In your project folder, copy the provided .env.example to a new file named exactly .env:

macOS / Linux:

cp .env.example .env
Windows (PowerShell):
Copy-Item .env.example .env
Open .env in your editor and paste your real key after the =:
ANTHROPIC_API_KEY=sk-ant-...your real key...
Save it.

You should now see: a .env file containing your key, and .env.example still holding only the placeholder. Do not put the key anywhere else.

Step 5: Install the libraries

With your virtual environment active (you see (.venv), see python-venv.md):

pip install anthropic python-dotenv
You should now see: Successfully installed anthropic-… python-dotenv-…. anthropic is the Claude SDK; python-dotenv loads your .env file.

Step 6: Confirm with a tiny call

Run the provided check (it makes the smallest possible request):

python check_setup.py
You should now see: It works! Claude says: followed by a short friendly sentence. If you see that, your key, your .env, and your code all work, you're ready to build.


Troubleshooting

Symptom Fix
No ANTHROPIC_API_KEY found Your .env is missing, misnamed, or in the wrong folder. It must be named exactly .env (not .env.txt) and sit in the folder you run python from.
AuthenticationError / 401 The key is wrong or has a typo. Re-copy it from the Console, or delete it and make a new one. No space before/after the = in .env.
PermissionDenied / 403, or "credit balance too low" Add a little credit in Billing (Step 2).
RateLimitError / 429 You're sending too fast, wait a moment and retry. (Rare while learning.)
ModuleNotFoundError: anthropic The venv isn't active, or you didn't install it. Re-activate ((.venv) in the prompt) and re-run Step 5.

Secrets hygiene: read this once, practice it always

An API key is like a password to a service you pay for. If it leaks (e.g. pushed to GitHub), someone else can run up your bill. So:

  • Keep keys in .env, loaded with python-dotenv, never typed into your .py files.
  • Never commit .env. This course's .gitignore already ignores it; .env.example (with a placeholder) is the only key file that belongs in Git.
  • Share .env.example, never .env. Teammates copy the example and paste their own key.
  • If a key ever leaks, delete it in the Console immediately and make a new one. Deleting a key instantly stops it working, damage controlled.
  • Set a spend limit (Step 2) as a backstop.