API Keys and Secrets

Updated · 5 min read
mediumPython 3.13aiapisecuritysecretspython

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

Safe places, never places, and what to do after a leak
PlaceSafe?Description
Environment variableYesIt is used to give the key to a program without writing it in a file
.env file in .gitignoreYes, locallyIt is used to keep local settings in one file that is never committed
Secrets managerYes, for serversIt is used to store keys encrypted, with access control and audit logs
Python code or gitNoAnyone who sees the code or the repository's history gets the key
Browser or mobile appNoAnyone can read it from the app; call the API from your server instead

Example: Loading a Key Safely

Python
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 itself

Running this without the variable set prints:

Output
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

SystemCommandDescription
macOS and Linuxexport 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
.gitignoreAdd a line .envIt 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:

  1. Why is a leaked key a cost risk?
  2. Why can't a web page hold a key safely?
  3. What is the first step after a leak?

Quick check: API keys and secrets

4 questions
1Where should a key go for local development?
2Why shouldn't a mobile app contain your API key?
3A key was committed and then deleted from the code. Is it safe?
4What should a program print after loading a key?

Key Takeaways

Quick reference

DoDon't
os.environ.get("MODEL_API_KEY")Write the key in code
Add .env to .gitignoreCommit .env
Separate keys and spending limitsOne key everywhere
Revoke a leaked key at onceJust delete it from code

Was this tutorial helpful?

Lesson 89 of 186