API Keys and Secrets
An API key is a secret string that proves a request comes from your account. Anyone who has it can make calls that you pay for, and may see data your account can reach. A secret is any value like this: API keys, passwords and tokens. Keeping secrets out of code, chat messages and version control is one of the first habits of building AI applications.
Where Keys Belong
| Place | Safe? | Description |
|---|---|---|
| Environment variable | Yes | It is used to give the key to a program without writing it in a file |
.env file in .gitignore | Yes, locally | It is used to keep local settings in one file that is never committed |
| Secrets manager | Yes, for servers | It is used to store keys encrypted, with access control and audit logs |
| Python code or git | No | Anyone who sees the code or the repository's history gets the key |
| Browser or mobile app | No | Anyone can read it from the app; call the API from your server instead |
Example: Loading a Key Safely
import os
import sys
key = os.environ.get("MODEL_API_KEY")
if not key:
print("MODEL_API_KEY is not set. Set it in your terminal; never write it in code.")
sys.exit(1)
print("Key loaded.") # never print the key itselfRunning this without the variable set prints:
MODEL_API_KEY is not set. Set it in your terminal; never write it in code.Explanation: os.environ.get() reads the variable and returns None if it's missing, so the program stops with a clear message instead of failing later with a confusing error. The key is never printed, logged or written to a file. The course's ModelClient() reads MODEL_API_KEY the same way, so no key ever appears in the code.
Setting the Variable
| System | Command | Description |
|---|---|---|
| macOS and Linux | export MODEL_API_KEY="..." | It is used to set the key for the current terminal |
| Windows PowerShell | $env:MODEL_API_KEY="..." | It is used to set the key for the current window |
.gitignore | Add a line .env | It is used to stop a .env file from being committed |
Common Mistakes
Pasting a key into an AI chat to debug an error. Share the error message, never the key.
Calling the API directly from a web page. Anyone can open the browser's developer tools and copy the key; send requests through your own server.
Committing a .env file. Add .env to .gitignore before the first commit.
Interview Corner
How should an application store API keys?
Outside the code: in environment variables locally, and in a secrets manager on servers, with separate keys per environment and spending limits. Keys never go into git, browser code or mobile apps.
What do you do if a key is committed to git?
Revoke it immediately and create a new one. Removing it from the code isn't enough, because it remains in the repository's history.
Practice
Exercise: a safe setup. Create a .env file containing your key, add .env to .gitignore, and run git status to confirm the file isn't listed. Then run the loading program with and without the variable set.
Check your understanding:
- Why is a leaked key a cost risk?
- Why can't a web page hold a key safely?
- What is the first step after a leak?
Key Takeaways
Quick reference
| Do | Don't |
|---|---|
os.environ.get("MODEL_API_KEY") | Write the key in code |
Add .env to .gitignore | Commit .env |
| Separate keys and spending limits | One key everywhere |
| Revoke a leaked key at once | Just delete it from code |
Was this tutorial helpful?