Configuration for someone else's machine
By the end of this lesson you will have an app that will not serve a page without a key — it names EODHD_API_KEY and the file to copy as it starts — an .env.example whose key line is empty, and a git history you have searched for anything shaped like a key.
Your tool has your key, your symbols, your assumptions about ports. This lesson separates what is yours from what is the tool's.
Everything environment-specific comes from environment
API key, base URL, port, store connection. Nothing environment-specific is a literal in the code, and one file lists every variable that exists:
# .env.example — copy to .env.local and fill in
EODHD_API_KEY= # from your EODHD account; a free key: no screener, one year of history
EODHD_API_BASE_URL=https://eodhd.com/api
.env.example is committed and holds no secrets: the key line is empty, and the non-secret lines carry their working defaults, as the base URL above does. .env.local holds your real values and is gitignored. That pair is the whole convention, and the starter repository ships it already.
Fail loudly at boot, not quietly at request time
A missing key should stop the application starting, with a message that names the variable and the file:
if (!process.env.EODHD_API_KEY) {
throw new Error("EODHD_API_KEY is not set — copy .env.example to .env.local");
}
The alternative is a tool that starts, renders, and shows empty panels — and the person concludes the tool is broken rather than unconfigured. Ten seconds of confusion at boot beats twenty minutes of confusion in the browser, and this is the single highest-return five lines in the course.
The key is still not in the bundle
Nothing about shipping changes Build Your First Tool's rule. EODHD_API_KEY has no public prefix, is read only on the server, and the proxy is still the only thing that touches it.
Two extra habits once the repo is public:
- Check the git history, not just the working tree. A key committed and then deleted is still in the history and still needs rotating.
git log -ppiped through the same pattern your pre-commit hook uses takes a minute. - If a key was ever exposed, rotate it. Not "it was only briefly public". Rotating is free and takes a minute; assuming nobody looked is a bet you cannot check.
Sensible defaults so it runs on first try
Every variable except the key should have a working default. A person who supplies one secret and gets a running application has had a good first experience; one who has to fill in six fields before seeing anything has had a chore.
Same principle as the default layout from Build a Dashboard: nobody should meet an empty form before they have seen the thing work.
Try it now
Delete your .env.local, start the app, and read what it tells you. If the message does not name both the variable and the file to create, fix it — that message is the first thing every future user of your tool will read.
The prompt
Mock mode on, plan mode on, and paste:
Make the app refuse to run unconfigured. In src/lib/eodhd.ts, the one
file that reads the key, export configProblem(env): null when
EODHD_MOCK is on or EODHD_API_KEY holds a real value, and otherwise one
sentence naming EODHD_API_KEY, the file to copy, and mock mode. Empty
and the old placeholder your_key_here are not real values. Call it from
register() in src/instrumentation.ts and throw its sentence; read this
Next.js version's guide in node_modules/next/dist/docs/ first. Remove
the home page's own no-key branch in src/app/page.tsx, which can no
longer be reached. In
.env.example, empty the key line and list every variable the code
reads, with its default. Tests first. You may edit .env.example, the one env file that holds no
secrets; do not open .env.local. Done
means npm test, npm run typecheck, npm run lint and npm run check:leak
green; then show me the diff.
A good answer changes src/lib/eodhd.ts, .env.example and the home page, and adds src/instrumentation.ts with a test for configProblem. The other pages and the cockpit carry the same unreachable no-key branch; leave them for a commit of their own, so this one stays one change. Nothing outside src/lib/eodhd.ts reads the key afterwards, which the next unit's CI step depends on.
Check it yourself
- Move
.env.localaside, runnpm run devand open the page. The terminal prints the sentence naming the variable and the file, and no page renders. - Copy
.env.exampleto.env.localwithout editing it: the same sentence, not a page of empty panels. - With
EODHD_MOCK=1and no key, the app starts and shows its mock-mode notice. git log -p --all | grep -En '[0-9a-f]{12,}\.[0-9]{6,}'looks for a key-shaped string anywhere in your history. One hit is expected: the starter's own commitci: fail the build on a key-shaped string anywhere in the tree (#1)contains a made-up key-shaped string on purpose, to prove its scan works, and it is not a key. To see which commit a hit belongs to, ask two ways, because a hit can sit in a commit's message or in the files it changed:git log --all --oneline -E --grep='[0-9a-f]{12,}\.[0-9]{6,}'searches the messages, which is where the starter's is, andgit log --all --oneline -G'[0-9a-f]{12,}\.[0-9]{6,}'searches the changes. Only a hit in a commit you made matters: if you find one, rotate that key first and read the rest of the history second.
Compare with ours
// src/lib/eodhd.ts: the one file that reads the key also says when it is missing
const NOT_A_KEY = new Set(["", "your_key_here"]);
export function configProblem(env: Record<string, string | undefined>): string | null {
if (/^(1|true|yes)$/i.test(env.EODHD_MOCK ?? "")) return null;
if (!NOT_A_KEY.has((env.EODHD_API_KEY ?? "").trim())) return null;
return "EODHD_API_KEY is not set: copy .env.example to .env.local and add your key, " +
"or set EODHD_MOCK=1 to run on fixtures.";
}
// src/instrumentation.ts: runs once as the server starts, before any request
export async function register() {
if (process.env.NEXT_RUNTIME !== "nodejs") return;
const { configProblem } = await import("@/lib/eodhd");
const problem = configProblem(process.env);
if (problem) throw new Error(problem);
}
Under next dev the server stops; under next start it prints the sentence as it starts and answers 500 rather than a page. Either way nobody meets empty panels. The full state at the end of this unit is on the starter's branch ref/ship-your-tool/u1.
Checkpoint
npm test && npm run typecheck
git status # the files you expect, and no .env file
git add -A
git commit -m "Refuse to run without a key, naming the variable and the file to copy"
Then answer this lesson's questions.