Why your API key must never reach the browser
By the end of this lesson you will have watched npm run check:leak pass on your copy of the starter, fail on a made-up key you planted in a browser file on purpose, and pass again once the code is put back.
This is the lesson that decides whether your project is safe, and it comes third because the mistake it prevents is the one people make in their first hour.
The shape of the mistake
The EODHD API authenticates with a token in the URL:
https://eodhd.com/api/eod/AAPL.US?fmt=json&api_token=YOUR_TOKEN
The obvious thing to do in a web app is to call that from the page. It works instantly, which is the problem. Anything the browser fetches, the person using the browser can read — and so can any extension they have installed, any proxy in the way, and anyone who opens developer tools. If you ship a build with the key in it, the key is not "a bit exposed". It is published.
Public keys get found. Not by a person reading your source: by automated scanners that crawl deployments and public repositories continuously and cost nothing to run. The gap between "I deployed it" and "someone else is using my quota" is measured in days.
A leaked key on a paid plan is somebody else spending your money. A leaked key on a free plan is your tool silently breaking, because your daily allowance is gone by the time you wake up.
The fix: a server between you and the API
Put a small piece of your own server in the middle. The browser asks your server; your server adds the key and asks EODHD; the answer comes back. The key exists only in the server's environment and never travels to the client.
browser → /api/proxy/eod/AAPL.US (no key anywhere)
↓ your server adds api_token
https://eodhd.com/api/eod/AAPL.US?api_token=...
That is the whole idea, and it is about thirty lines. In the starter repository it lives in two files:
src/lib/eodhd.ts— the only place in the codebase that reads the key.src/app/api/proxy/[...path]/route.ts— the route the browser is allowed to call.
Two details in that proxy are worth more than the rest of it put together.
It drops any token the caller supplies. If someone calls your proxy with ?api_token=something, that parameter is thrown away before the request goes upstream. Without this, your proxy becomes a free relay for other people's keys — and, worse, a way to make requests that look like they came from you.
It is the only route out. No component fetches EODHD directly. The moment one does, the rule has a hole in it, and holes in this rule do not announce themselves.
Where the key actually lives
Locally, in .env.local, which is in .gitignore:
EODHD_API_KEY=your_key_here
In production, in your host's environment settings — on Vercel, the project's environment variables.
One naming rule that has burned many people: in Next.js, any variable prefixed NEXT_PUBLIC_ is deliberately inlined into the browser bundle. That prefix is a feature, and it is a loaded gun pointed at exactly this. EODHD_API_KEY has no prefix, and it must never get one.
Do not trust yourself — check the build
Rules are only as good as the check that enforces them. After a production build, search the output for your key's actual value:
npm run build
export $(grep -E '^EODHD_API_KEY=' .env.local) # the shell does not read .env.local
grep -rF "$EODHD_API_KEY" .next/ && echo "LEAK" || echo "clean"
These lines are bash: on Windows run them in Git Bash. The export line is the part people drop, and dropping it inverts the check:
Next.js reads .env.local, your shell does not, so an unset $EODHD_API_KEY
makes grep search for the empty string, match every line in the build, and
print LEAK on a perfectly clean one.
On the starter repository, built with a real key in the environment on 2026-08-25, that search finds nothing — not in the server bundle, not in a single client chunk.
You do not have to remember any of that. The starter turns it into one command, npm run check:leak: it builds with a made-up key — in EODHD_API_KEY and in NEXT_PUBLIC_EODHD_API_KEY, the classic mistake — and fails if that value turns up in any file the build produces under .next. A made-up key means you can run it before you have a real one, and it never prints yours. It is part of rule 6 of the house rules, so your agent runs it too. Stop npm run dev (Ctrl-C in its terminal) before any leak check, in this lesson and every later one: the check builds the app into the same .next folder the dev server is using, and two programs writing there at once can break the dev server. Start it again afterwards. Read scripts/check-leak.mjs before you rely on it, including the part it deliberately skips — a check you have not read is a promise, not a check.
The git hooks that npm install switched on back it up: before a commit, a guard refuses anything key-shaped; before a push, the tests, the type check and the leak check run again. A seatbelt, not a lock: git commit --no-verify skips them, they cannot see a key you never staged, and they cannot see one already in your history. If a key does reach a public place, rotate it. Deleting the commit is not enough — it was public, and the scanners are faster than you.
What npm install just did
You ran npm install a moment ago. package.json lists the packages you
chose; the command fetches those and everything they depend on, pins the exact
versions in package-lock.json, and unpacks them into node_modules/. Commit
the lockfile so every machine installs the same thing; never node_modules.
Count both yourself: the dozen or so names in package.json, against the
hundreds that npm ls --all prints. Every one of them runs with your
permissions. Ship Your Tool audits them.
One more thing that is not about secrets
The data you pull is licensed to you, not through you. EODHD's own wording is that the plans on the pricing page are "intended for personal use only as commercial use requires a more thorough approach to licensing and data use". Running this tool for yourself is personal use. Putting it on a public URL where other people read the numbers is a different conversation, and the licence page in this lesson's sources is where it starts.
Try it now
The prompt
Mock mode can stay on: the leak check plants its own made-up key and needs neither yours nor the network. In the starter folder, paste:
Run npm run check:leak and show me its last line. Then read
scripts/check-leak.mjs and tell me in three sentences what it builds
with, what it searches for, and which folders it deliberately skips and
why. Change nothing, and spend no API calls.
A good answer changes no file and ends with a line beginning "Leak check clean". Its three sentences say: a production build with a random made-up key in both EODHD_API_KEY and NEXT_PUBLIC_EODHD_API_KEY; every file under .next searched for that value; the build caches skipped, because Next.js records environment values there.
Check it yourself
- The last line of the run starts with "Leak check clean" and says how many files it searched.
- Open
scripts/check-leak.mjsand find the made-up key and the two skipped folders yourself. The agent's sentences match the file. git statusis clean afterwards: the check writes only into.next, which is ignored.grep -rn NEXT_PUBLIC_ srcprints nothing: no file in the starter reads a browser-visible variable.
Break it on purpose
Use a fake value, never your key. Add NEXT_PUBLIC_EODHD_API_KEY=fake-1234567890 to .env.local by hand, then ask the agent:
For an exercise I will undo: create src/components/LeakDemo.tsx, a
"use client" component that renders
process.env.NEXT_PUBLIC_EODHD_API_KEY, and render it once at the bottom
of src/app/page.tsx. Touch no env file, and show me the diff.
Run npm run build, then grep -rl fake-1234567890 .next/static. Seeing it sitting in a browser chunk is a thirty-second exercise you will remember for years. Now run npm run check:leak against the same broken code and read which files it names. Then change everything back and run it once more: clean. A check you have seen fail on purpose is one you can believe when it passes.
Compare with ours
The demo, and the undo
// src/components/LeakDemo.tsx: exactly the mistake. Never commit it.
"use client";
export function LeakDemo() {
return <p>{process.env.NEXT_PUBLIC_EODHD_API_KEY}</p>;
}
rm src/components/LeakDemo.tsx
git restore src/app/page.tsx
npm run check:leak
Then delete the NEXT_PUBLIC_ line from .env.local by hand. Two lines of code were enough to publish a key, and nothing from this exercise is committed. The full state at the end of this unit is on the starter's branch ref/build-your-first-tool/u1.
Checkpoint
Nothing to commit: the exercise ends with the code put back, and git status is clean. If it is not, the undo is unfinished. Then answer this lesson's questions.