Keys (env)
env.NAME is a key of the project — a token, a client secret, a maps key — kept out of the code:
on this machine in the project's .env file (NAME=value, next to app.json, never pushed), on
le.codes in Project Settings → Keys. Server code (*.server.ts,
Server functions) reads every key. The app reads only keys named
PUBLIC_…: an app's bundle is public, so a key the app carries is public by its name, and the
compile refuses any other name in app code. The project's language model needs no key:
ai.
- call a paid API from the server with a token that is not in the code (
env.OPENAI_KEY) - use a maps or analytics key in the app without hardcoding it (
env.PUBLIC_MAPS_KEY) - keep a bot token or a payment key out of the repository and out of the AI's view
- find out which key is missing before the first call fails
- make sure a secret never ends up in the app bundle
At a glance
# .env — next to app.json; the CLI never pushes it. Capital names, one per line.
OPENAI_KEY=sk-… # server only
PUBLIC_MAPS_KEY=AIza… # may be in the app: its name says so// ask.server.ts — runs on the backend: every key is here
export async function ask(question: string) {
const r = await fetch("https://api.openai.com/v1/responses", {
method: "POST",
headers: { authorization: `Bearer ${env.OPENAI_KEY}`, "content-type": "application/json" },
body: JSON.stringify({ model: "gpt-5", input: question }),
})
return (await r.json()).output_text as string
}// main.ts — the app: PUBLIC_ keys only
const places = await fetch(`https://maps.example/places?near=${lat},${lng}&key=${env.PUBLIC_MAPS_KEY}`)A key is a string, never string | undefined. Reading one that is not set throws an EnvError
whose message names the key and where to add it — in .env here, in Settings → Keys on le.codes.
On the server that is a failed call (500, the reason in the server console); in the app, an
exception where the read is. "OPENAI_KEY" in env asks without throwing; Object.keys(env)
lists the keys that are set.
Where a key lives and when it arrives
| where the app runs | server keys (env.NAME) |
app keys (env.PUBLIC_NAME) |
|---|---|---|
lecodes dev, lecodes test, lecodes render, desktop run |
the project's .env, read on every reload — saving .env redeploys the backend |
the project's .env, baked into the bundle at compile |
| le.codes preview and a published app | Project Settings → Keys, sent to the backend with every build | Project Settings → Keys, baked into the bundle by the build |
Names are capital letters, digits and underscores ([A-Z][A-Z0-9_]*, 64 at most). .env holds
the CLI's own LECODES_* / CREATOR_* settings too; those are not keys of the app.
What is checked at compile and at deploy
- App code reads a key that is not
PUBLIC_…— a compile error naming the file:main.ts: env.OPENAI_KEY. Read it in a*.server.tsfile and call an endpoint, or name itPUBLIC_…if the app may carry it. - App code reads a
PUBLIC_…key that is not set — a compile warning; the read throws until the key is added. - Server code reads a key that is not set — a warning when the backend starts (
env: OPENAI_KEY not set), in the dev terminal or the server console; only the calls that read it fail. - A secret's value is in the app bundle — pasted into app code or into an asset: the compile is
refused (
EnvLeakError). Values shorter than 8 characters are not searched for.
The names server code reads are found by the compile (env.NAME, env["NAME"]); a dynamic read,
env[name], is checked at run time only.
Not available
- Different values per build (draft / published): a project has one set of keys.
- A key in a shared module that both sides import: a file that is not
*.server.tsis app code, so onlyPUBLIC_…reads are allowed there.