Course B · Module B1 Production · Lesson 5 of 18
Connect services like Stripe and email — without ever leaking the keys to your kingdom.
The moment you wire your app to Stripe, an email service, or an AI model, you're given a key — a long string of characters that says "this is me, let me in." That key is worth protecting as carefully as your bank login. One careless paste and a stranger can send emails from your domain, charge cards on your account, or run up thousands of dollars in AI API costs. Your single win today: by the end you'll know exactly where keys belong, where they never belong, and what to do the moment one leaks.
Think of a hotel key card. The hotel gave it to you to open your room. If you lose it, whoever finds it can walk into your room. If you hand a copy to a stranger, they can too. The card itself doesn't know it was lost — it just opens the door for whoever holds it.
An API key works the same way. It's a password your app presents to an external service to prove it's authorized. The service doesn't check whether it's really you — it just checks whether the key is valid. Whoever holds the key has your access.
This is the rule that, when broken, causes the painful incidents. When your code is on GitHub — even a private repository — and a key is pasted directly into a file, it can be exposed. Public repos are read by automated bots that scan for keys within seconds of a push.
// config.js — DO NOT DO THIS const stripeKey = "sk_live_51Abc..." const openaiKey = "sk-proj-T4x..."
Keys pasted into source files end up in Git history — and Git history is forever. Deleting the line doesn't remove it from past commits.
// config.js — safe const stripeKey = process.env.STRIPE_SECRET_KEY const openaiKey = process.env.OPENAI_API_KEY
The code references an environment variable by name. The actual key lives in your hosting platform's secret settings — never in the file.
An environment variable is a named value that lives in your
server's configuration, separate from your code. Your app says "give me the value
called STRIPE_SECRET_KEY" — and the server hands it back at runtime.
The key itself never touches your codebase or your Git history.
How to set one up (Vercel example — other platforms are identical):
STRIPE_SECRET_KEY, Value = your actual key.For local development, create a file named .env.local in your
project folder with the same variables. Then add .env.local to your
.gitignore file so Git never tracks it. Your AI coding tool will usually
set this up for you — just make sure the .gitignore line is actually there.
Assume that any key that touches a file, a chat message, or a screenshot has leaked. The fix is fast and the cost of not doing it is high:
This will come up in the upcoming auth, email, and payments lessons — every service you connect (Clerk, Resend, Stripe) will give you keys. The pattern is always the same: store in environment variables, never in code, rotate immediately if anything feels off.
Answer before opening — recalling it is what makes it stick.
Replace the key in the file with an environment variable reference (process.env.STRIPE_SECRET_KEY), store the actual key in your hosting platform's secret settings, and — if this file was ever committed to Git — rotate the key immediately because Git history preserves the old value even after you delete the line.
An environment variable is a named value stored in the server's configuration, outside the codebase. Your code references the variable's name; the server supplies the actual value at runtime. The key never enters a file, so it can't end up in Git history or a public repository.
Rotate the key immediately. Go to the OpenAI dashboard, revoke the current key, and generate a new one. That stops any ongoing unauthorized usage right away. Then check the usage logs to understand what was called, and update your environment variables with the new key.
Read this next (primary source)
About Secret Scanning — GitHub Docs. Explains how GitHub automatically detects leaked keys in your repositories and what happens when one is found. Essential context for why the "never in code" rule matters so much in practice. ~8 min. See RESOURCES.md for more.
New terms from this lesson — API key, environment variable, secret scanning, rotate — are in the course glossary. Keep it open as you go.