Missing secrets and environment variables
A key that reads as undefined is almost never a code bug. It is a name mismatch, a value in the wrong place, or a function that never saw the update.

Key takeaways
- The most common cause is a name that does not match exactly: the code asks for one spelling and the secret is saved under another.
- Server secrets belong in Backend, Secrets and are read by edge functions; public frontend values with VITE_ or EXPO_PUBLIC_ go in the app .env.
- Never "fix" a missing secret by adding a public prefix: that ships the key to every visitor.
- Check the function logs for the exact error, then ask the agent to fail loudly with the missing name.
When an app says an API key is undefined, a provider answers 401 Unauthorized, or a feature that calls an outside service silently returns nothing, the usual cause is a missing or misnamed secret: the code reads a variable such as EMAIL_API_KEY and the place it runs has no value under that exact name. Less often the value is in the wrong place (a server key in the frontend, or a frontend value in the backend store), or it was saved correctly but the code that reads it has not been redeployed. The checks below go from most to least likely and take a few minutes each.
What a missing secret looks like
The symptom depends on who reads the value. Learn the pattern and you know where to look before opening a single file.
- In an edge function: the function returns a 500, or its log shows a line like "Missing EMAIL_API_KEY", "apiKey is required" or a 401 from the provider. The page that called it shows a generic error or a spinner that never ends.
- In the frontend: a config value prints as undefined in the preview console, a map or payment widget shows "invalid key", or a URL is built as "undefined/api/items".
- Only in one place: the feature works in preview but not after publishing, or the other way round. That points at a value that exists in one environment and not the other.
- Only for some calls: text generation works but email does not. Each integration usually reads its own key, so one missing name breaks one feature.
If you want the background on why there are two kinds of variables, the glossary entry on environment variables explains public versus secret values in five minutes.
Causes, most likely first
1. The name does not match exactly
Names are case sensitive and unforgiving. RESEND_KEY is not RESEND_API_KEY, Stripe_Secret_Key is not STRIPE_SECRET_KEY, and a trailing space pasted into the name field makes it a different name. This accounts for most "I added the key and it still fails" reports.
Check: open the function or file that reads the value and copy the exact name it asks for. Compare it letter by letter with the list in Backend, Secrets. Startbase masks values in that list, but the names are visible, along with when each was last updated.
Fix: add a secret under the exact name the code expects, or ask the agent to change the code to the name you saved. Pick one convention (all caps, words joined by underscores, the provider name first) and keep it.
2. The value is in the wrong store
There are two homes for configuration, and they are not interchangeable. Backend secrets are encrypted, read by edge functions as environment variables and never exposed to client-side code. Public frontend values, with names starting VITE_ for web apps or EXPO_PUBLIC_ for Expo apps, live in the app's .env in the workspace and are copied into the JavaScript every visitor downloads. Startbase rejects names with those public prefixes in Backend, Secrets for exactly that reason (secrets docs).
- A secret key (payments, email, an AI provider, anything that bills you) read directly in a React component will be undefined, because the browser cannot see backend secrets. That is working as intended.
- A public value (a publishable payment key, a public map token, your backend URL) saved as a backend secret is invisible to the frontend that needs it.
Fix: move the call that needs the secret into an edge function and have the page call that function. Put public values in the app .env with the right prefix. The article on API keys and secrets has the full split by key type.
3. The secret was never added
The agent wrote code that reads OPENWEATHER_API_KEY and told you to add it, and the message scrolled away. Or the project was copied and secrets, which are write-only and never travel with code, stayed behind. A code download also leaves environment files out on purpose.
Check: search the code for the name. If it appears in code but not in the Secrets list, it was never added. Fix: get the key from the provider's dashboard and add it. You cannot read a saved value back, so keep the original in your password manager.
4. Updated, but the function has not picked it up
You replaced a wrong value and the error continues. Whether a running function sees a new value immediately depends on the platform, so do not assume it. Check: look at the function's recent log lines after your change. Fix: redeploy the function. The Startbase troubleshooting page suggests a small change to the function code to trigger a redeploy, or ask the agent to redeploy it.
5. The key is present but wrong
A 401 or 403 with the name correct usually means the value itself is the problem: a test key against live mode, a key from a different account, one that was revoked or rotated, or a key missing a permission scope. Check: the provider's error text usually says which ("invalid API key", "key does not have access to this resource"). Fix: create a fresh key with the scopes you need and save it under the same name.
6. A managed variable was edited by code
Secrets starting with SUPABASE_ are provisioned and rotated by Startbase Cloud and marked "Managed by Startbase". If code hard-codes an old backend URL or key instead of reading the managed values, it breaks after a rotation. Ask the agent to read the managed values and remove any hard-coded copies.
A five-minute check
- 01Reproduce the failure once, then open Backend, Logs, pick Functions as the source and a 15 minute range (logs docs).
- 02Expand the error row. If it names a variable or says 401, you have your cause. The Fix with AI button on that row sends the event to the agent.
- 03Write down every secret name the failing code reads. Compare with Backend, Secrets.
- 04If a value is meant to be public, confirm it has the VITE_ or EXPO_PUBLIC_ prefix and sits in the app .env, then reload the preview.
- 05If it works in preview but not live, republish: publishing builds a snapshot, and frontend values are baked in at build time.
The message to send the agent
Ask for diagnosis and a guard that fails loudly, so the next missing key tells you its name instead of a vague 500:
The weather card shows "Something went wrong" and the edge function get-forecast returns 500. List every environment variable and secret this project reads, with the file that reads each one and whether it must be server-only or public. Tell me which ones are missing from Backend Secrets or the app .env. Then add a check at the top of each edge function that returns a clear error naming any missing secret, and never move a server key into frontend code or give it a VITE_ prefix.
After you add the missing value, send a short follow-up: "I added WEATHER_API_KEY. Redeploy get-forecast and test it once." The agent runs and checks its work, so it can confirm the function answers before you test it yourself.
Keep it from happening again
- Keep a short list of every secret name and what it is for in your project knowledge, so the agent reuses names instead of inventing new ones.
- Use separate test and live keys, and name them clearly if you keep both (STRIPE_SECRET_KEY_TEST is better than a comment in a chat).
- Rotate any key that was ever pasted into frontend code or a public repository. Deleting the line does not remove it from history.
- Read the pre-publish security scan before you go live: High findings for exposed secrets deserve a fix first.
For how server code reads secrets in the first place, see edge functions explained.
Frequently asked questions
Why is my environment variable undefined in the browser?
Frontend build tools only expose variables with a public prefix, such as VITE_ in Vite apps or EXPO_PUBLIC_ in Expo. Anything else stays undefined in the browser. If the value is a real secret, that is correct: call it from an edge function instead.
Can I see the value of a secret I saved?
Not in Startbase. Backend secrets are write-only: the list shows names and when each was last updated, and saving a new value replaces the old one. Keep originals in a password manager.
My key works in preview but not on the published site. Why?
Frontend values are baked into the build when you publish, so a value added after your last publish needs a republish. For server secrets, check that the published site calls the same function and that its logs show the same error.
Is it safe to put an API key in the .env file?
Only if it is meant to be public, like a publishable payment key. Values with VITE_ or EXPO_PUBLIC_ end up in the code visitors download. Secret keys belong in Backend, Secrets.

