Day 1 - Set Up Your OpenAI API Key and .env File for the First Lab
Week 1 · Day 1 · Lecture 7. Windows, Mac and Linux paths converge here. This is step 4 of the setup: an OpenAI account, billing, an API key, and the .env file that feeds it to your code.
Lab file: 1_foundations/1_lab1.ipynb
First: the API is not ChatGPT
A ChatGPT subscription does not give you API access, and API credit does not give you ChatGPT. They are separate products with separate billing.
- ChatGPT — the end-user product.
- The OpenAI API — the developer interface this course uses.
If that distinction is new, read the technical foundations guide in the guides/ folder. And if you would rather not use OpenAI at all, Guide 9 covers OpenRouter, Gemini and local Ollama models — follow that instead and skip to the .env section.
Practical steps
1. Create or sign in to an OpenAI account
Go to platform.openai.com. Signing in with Google is the smoothest route. First-time signup asks for email verification, then your name and age.
2. Set up your organization
You are asked for an organization name and what best describes you. For personal study, something like "<your name> education" and Student is perfectly appropriate. Create the organization, then choose I'll invite my team later.
3. Add billing credit
The API requires a positive balance. $5 is the minimum top-up, and $5 is realistically more than this whole course will consume.
Go to Settings → Billing, add payment details, and load $5.
The signup flow may offer billing and a key inline. Either route is fine — the settings screens are shown here because they are where you will return later.
4. Create an API key
Go to Settings → API keys and press Create new secret key.
Make it an organization-level key, and select Default project. A project-scoped key restricts what it can reach and produces confusing permission failures later. Name it anything — my test key is fine.
Copy the key immediately. OpenAI shows it once.
5. Put the key in your .env file
In the project root — the folder containing pyproject.toml — create a file named exactly .env:
OPENAI_API_KEY=sk-proj-...
Spell it exactly. OPENAI_API_KEY, not OPEN_API_KEY. Paste the variable name rather than typing it. No quotes, no spaces around the =, no trailing blank.
.env is already in .gitignore. Never commit it, and never paste a key into a notebook cell.
You are done when
- Your OpenAI account has a positive balance.
- You hold an organization-level key on the default project.
.envsits in the project root with the correctly spelled variable.1_foundations/1_lab1.ipynbloads the key and reports it found.
Resource links
- OpenAI Platform — account and dashboard
- Billing · API keys · Usage
guides/04_technical_foundations.ipynb— ChatGPT versus the APIguides/09_ai_apis_and_ollama.ipynb— Guide 9: OpenRouter, Gemini, DeepSeek, Ollama1_foundations/1_lab1.ipynb— the lab this key is for
Next
Lecture 1.8 makes your first API call and chains two calls together.