Managing API Keys
An API key lets your code use a service and charge requests to an account. You’ll copy
the course API key into a .env file in your repo on the Yens, check that Git
ignores it, and load it in Python.
Start in your SSH terminal connected to the Yens. A JupyterHub terminal works too. You’ll switch to a notebook in Step 5.
Step 1: Check the Shared Credential File
The instructor provides the course key in a shared file on the Yens. View it in your own terminal:
cat /scratch/shared/yens-onboarding-2026/.env
You’ll see ANTHROPIC_API_KEY= followed by the course key. In Step 3, you’ll copy this
file into your repo.
Step 2: Keep the Key Out of Your Code
Do not do this. This is an example of what to avoid, not a step to run.
Do not copy the code below into your notebook or replace the placeholder with your key:
# WRONG: putting the key directly in your code client = anthropic.Anthropic(api_key="REPLACE_WITH_REAL_KEY")Anyone who receives that code would receive the key too. Deleting it later does not remove earlier copies or Git commits.
The .env file you just viewed stores the key outside your Python code as a
NAME=value line. Your code reads the value by name when it runs. You’ll load it in
Step 5; there is no Python code to run in this step.
Three different meanings of “environment”
- Virtual environment (
.venv/): A directory holding a project’s Python interpreter and packages. It controls which Python and libraries your code uses.- Environment variable: A named value available to a running program. For example,
PATHtells the shell where to look for commands, andANTHROPIC_API_KEYholds a key that the API client can read. Programs look up these values by name instead of hard-coding them into their source..envfile: A text file containingNAME=valuelines. In this exercise,load_dotenv()reads those lines and makes the values available as environment variables in the running Python process.Activating
.venvselects your Python environment. Loading.envsupplies settings such as your API key. They are separate steps.
Step 3: Copy It to the Repository Root
cd ~/yens-onboarding-2026
cp /scratch/shared/yens-onboarding-2026/.env .env
ls -a
Look for .env in the listing. Keep this file at the repo root so the notebook below
can load it.
Files starting with a dot are hidden by default. A plain
lswon’t show.envor.gitignore; usels -ato include them. Hidden files still work normally, and being hidden does not protect their contents.
Step 4: Check That Git Ignores It
The repo’s .gitignore already includes .env. Check that the rule applies:
git check-ignore -v .env
The command should print the matching .gitignore rule. You can also check how Git sees
the file:
git status --short --ignored .env
!! .env means ignored.
If no ignore rule appears, add .env to .gitignore before committing your work.
Step 5: Load It in Python
First, create the notebook folder and move into it from your Yen terminal:
cd ~/yens-onboarding-2026
mkdir -p day1
cd day1
Then open JupyterHub, or return to your existing browser session:
- Open
yens-onboarding-2026/day1in the file browser. - Use the blue “+” Launcher button to create a GSB AI 2026 notebook.
- Name it
anthropic_test.ipynb. You’ll continue using it in the next section. - Run this cell with Shift+Enter:
import os
from dotenv import load_dotenv
load_dotenv("../.env")
print("ANTHROPIC_API_KEY loaded:", bool(os.getenv("ANTHROPIC_API_KEY")))
Expected output: ANTHROPIC_API_KEY loaded: True.
This confirms that the variable has a value. It does not check whether the service accepts the key; the next section tests that with an API call.
Loading the key in a script
A script at the repository root can use:
from dotenv import load_dotenv
load_dotenv()
python-dotenv reads the file and adds its settings to the current Python process’s
environment. It does not change your source code or activate a virtual environment.
By default, it keeps any value already set in that process.
Viewing the file in your own terminal is fine. Keep the key out of shared screenshots, chats, and notebook output, since notebook output is saved with the file.
Step 6: Initialize the API Client
In the same notebook, run a new cell:
import anthropic
client = anthropic.Anthropic()
The client reads ANTHROPIC_API_KEY from the environment, so you do not need to put
the key in the constructor.
Confirm that the object exists without making a paid request:
print(type(client).__name__)
Expected output: Anthropic. Creating the client does not send a model request.
🟢 Green sticky = Git shows
!! .env, the notebook reportsTrue, and the client was created 🔴 Red sticky = I need helpPut a sticky note on your laptop lid so instructors can see where you are.
Continue to the Part 2 checkpoint: Extracting Data with an LLM to make the first API call and check your setup.
Common Errors
| Symptom | Likely cause | Check |
|---|---|---|
ANTHROPIC_API_KEY loaded: False |
Wrong .env path or missing value |
In a notebook cell, run import os; print(os.getcwd()). This notebook belongs in the repo’s day1/ folder and loads ../.env |
HTTP 401 on the next section’s API call |
Invalid or revoked key | Ask the instructor to confirm the key. After replacing .env, restart the notebook kernel and rerun the setup cells |
.env appears in git status |
Ignore rule missing or file was already tracked | Stop and ask an instructor before committing |
Quiz
Answer each one in your head, then open it to check.
1You copied .env into your repo, but ls does not show it. Did the copy fail?
Not necessarily. Files starting with a dot are hidden by default. Run ls -a to
include them in the listing. The leading dot does not encrypt the file or prevent other
programs from reading it.
2You activated .venv. Does that also load the API key from .env?
No. They do different jobs. .venv/ contains your Python environment and packages.
.env is a text file holding settings such as ANTHROPIC_API_KEY. In this exercise,
load_dotenv(...) reads those settings into the Python process.
3Your notebook is running in day1/, and .env is at the repo root. Which path should you give load_dotenv?
"../.env". The .. means the parent directory, one level above day1/:
load_dotenv("../.env")
You can keep one .env at the repo root instead of copying it into each notebook folder.
4The notebook prints ANTHROPIC_API_KEY loaded: True. Have you confirmed that the API accepts the key?
No. You have only confirmed that the variable has a value. An invalid or revoked
key can still produce True. Creating the client also does not test the key with the
service. The API call in the next section checks whether the request is accepted.
5How can you check that Git will normally leave your .env file out of commits?
Check the ignore rule and the file’s status:
git check-ignore -v .env
git status --short --ignored .env
You should see a matching .gitignore rule and !! .env. A hidden filename alone does
not make Git ignore a file. If it was already tracked, adding an ignore rule does not
remove it from tracking.
6You accidentally commit a key, then delete it in the next commit. Is that enough?
No. The earlier commit still contains it. Tell the instructor or key owner so they can replace the key. Removing it from the current file does not make the old credential unavailable to someone who already has a copy.
Skills Learned
- Keep API keys in
.env, not in code or notebook output - Load the key from
.envand check that Python can find it - Check that Git ignores
.envbefore committing - Initialize an API client using the loaded environment variable
- Ask the key owner to replace a key if it is shared accidentally