Module 10 · Lesson 3 of 6
Environment variables and secrets
Where API keys and passwords belong, why they never go in code or chats, and what to do in the hour after one leaks.
8 min
Almost every useful app talks to other services: a database, an email sender, an AI model, a CRM. Each of those services asks for proof that the request is allowed, usually a long string called an API key, a token or a password. Whoever holds that string can act as your brokerage on that service. They can read your leads, send email in your name, or run up an AI bill.
This lesson is about where those strings live. The short version: in environment variables on your host, never in code, never in a chat, never in a screenshot. The longer version below covers why, how to set different values for testing and live use, how to give each key as little power as possible, and what to do in the first hour after one leaks.
- Key term
- Environment variable
A named setting stored outside the code, such as
DATABASE_URLorOPENAI_API_KEY. The code asks for the value by name when it runs; the host supplies it. Change the value on the host and the code doesn't change at all.
- Key term
- Secret
Any value that grants access: API keys, tokens, database passwords and connection strings, webhook signing secrets. A secret should be readable by the app that uses it and by as few people as possible.
Why keys never go in code or chats
Treat an API key like the code to a lockbox on a vacant listing. You'd never write it on a flyer, and you'd change it the moment it got out.
Keys in code end up everywhere code goes: GitHub, every laptop that clones the repository, every coding agent and service that reads it, and the full Git history forever (lesson 1).
Keys in chats are just as exposed. Messages to an AI assistant are stored by the provider, may appear in shared links or team workspaces, and can be remembered across conversations. A key pasted into a group text, an email or a Slack channel sits in every member's history and backup. Screenshots of dashboards often show keys too.
The rule for coding agents is simple: the agent writes code that reads process.env.SOME_NAME, and you put the real value into the host's settings yourself. If an agent asks you to paste a key into the chat, say no and tell it the variable name instead.
Where secrets go: per-environment values
On Vercel, open a project and go to Settings → Environment Variables. Each variable has a name, a value, and the environments it applies to:
- Production — the live site.
- Preview — deployments from every other branch.
- Development — the copy running on someone's laptop. The Vercel command-line tool can download Config values (below) for it, but never Secret values.
Use different values per environment where it matters. Production gets the real database and the real email key. Preview gets a test database full of fictional records and a test key that can only email the team. Then a reviewer clicking around a preview can never touch a real client.
As of October 2026, Vercel asks you to mark each variable as Config or Secret. Config values stay readable to project members, which is fine for things like a public site name. Secret values become write-only once saved: nobody can view them in the dashboard again, only replace them. Use Secret for every key and password. Vercel encrypts environment variables at rest either way.
Two details catch people out:
- Changes only apply to new deployments. After you add or change a variable, redeploy (Deployments, the three-dot menu on the latest deployment, Redeploy) or the app keeps using the old value.
- Some names are deliberately public. In a Next.js app, any variable whose name starts with
NEXT_PUBLIC_is copied into the code sent to every visitor's browser. That is correct for things meant to be public, and a leak for anything else. A secret must never have that prefix. Supabase's publishable key is designed to be public; its secret key is not (lesson 4).
The .env.local file
For local testing, values live in a file called .env.local on your computer. It must stay out of Git (lesson 1). Don't attach it to emails, don't paste it into chats, and delete it from laptops that leave the brokerage.
Least privilege
Least privilege means each key can do only what its app needs, and nothing more. If it leaks, the damage is small.
- One key per app and environment. The open-house page and the listing-alert tool each get their own AI key. If one leaks, you rotate one key and the other app keeps running.
- Narrow permissions. Many services let you create restricted keys: send-only email keys, read-only database users, keys limited to one project. Use them.
- Spend limits. AI providers let you cap monthly spend per project or workspace (lesson 6). A leaked key with a US$50 cap is an annoyance. A leaked key with no cap can become a large bill.
- Short lists of people. The integration partner and one brokerage owner can see the values. Agents don't need them.
Secret scanning
People make mistakes, so add tools that catch them.
- GitHub push protection blocks a push that contains a recognized secret pattern. As of October 2026 it is on by default for pushes to public repositories. Repository-level push protection for private organization repositories needs GitHub Secret Protection, a paid add-on.
- Vercel build logs hide Secret values that are 32 characters or longer if they ever get printed during a build, and record the event in the activity log.
- Your own eyes. Before merging a pull request, scan the changed files for anything named
.env,credentials,keyorsecret.
Rotating a leaked key
Assume a key has leaked if it was committed to a repository, pasted in any chat or email, shown in a screenshot or recording, or held by someone who has left. Don't debate whether anyone saw it. Rotate it.
- 1. TriggerKey spotted somewhere it should not be
- 2. HumanTell the account owner right away
- 3. ToolCreate a new key at the provider
- 4. StoreUpdate the Vercel environment variable
- 5. ActionRedeploy every app that uses it
- 6. CheckConfirm the app works
- 7. ActionRevoke the old key
- 8. StoreLog what happened and why
The order matters. Vercel's own guidance is to create and install the new credential first, redeploy, confirm it works, and only then revoke the old one. That way the live app never breaks. If you think the key is being actively misused (strange charges, unknown emails going out), revoke it immediately and accept a few minutes of downtime.
Then check the provider's usage or audit logs for activity you don't recognize. If the key could reach personal information, such as a database of leads, record the incident and assess it under privacy law. Module 11 explains when a breach must be reported.
In practice
At Maple & Main Realty, an agent troubleshooting the brokerage's listing-description tool pastes the tool's AI key into the team group chat so a colleague can "try it at home". The ops lead sees it twenty minutes later.
They don't ask who has opened the chat. They log in to the AI provider's console, create a new key for the listing tool's project, and paste it into Vercel as a Secret for Production only. They redeploy, generate one test description for a fictional 123 Example Street listing, and confirm it works. Then they revoke the old key and check the usage page: no unusual calls.
Finally they write three lines in the brokerage's incident log (what leaked, when, what was done), delete the message from the group chat, and add a sentence to the AI use policy: "Never share keys. Ask ops for access instead." Elapsed time: about 25 minutes.
Common mistakes
- Pasting a key into an AI chat "just so the agent can test it".
- Using the same key for every app, so one leak means rotating everything.
- Adding a variable on Vercel and forgetting to redeploy.
- Giving a secret a
NEXT_PUBLIC_name, which ships it to every visitor's browser. - Revoking the old key before the new one is installed and the app is redeployed.
Key takeaways
- Secrets live in the host's environment variables, marked Secret, never in code, chats or screenshots.
- Use separate values for Production, Preview and Development, with test data behind previews.
- Give every key the least power and the lowest spend cap that works.
- A key seen anywhere it shouldn't be is leaked: create new, install, redeploy, test, revoke, log.
Finished reading? Move on when you're ready.