What if your app's secret keys were accidentally shared with the whole world? Here's how to prevent that.
Why Environment variables and secrets in Supabase? - Purpose & Use Cases
Imagine you have to set up your app on multiple computers by typing passwords and keys every time you start it.
You write them down in your code or share them in chat messages.
It feels like leaving your house keys under the doormat for everyone to find.
Manually typing or storing secrets in code is slow and risky.
You might mistype a password or accidentally share it with others.
Changing a secret means updating many places, which is tiring and error-prone.
Environment variables and secrets let you store sensitive info safely outside your code.
Your app reads them automatically when it runs, so you never expose passwords in your files.
This keeps your secrets safe and makes updates easy.
const apiKey = 'my-secret-key'; // hardcoded in code
const apiKey = process.env.API_KEY; // loaded from environment variablesYou can safely manage and update sensitive data without touching your code, making your app more secure and flexible.
A developer deploys a Supabase app and sets the database password as a secret in the environment.
When the password changes, they update it once in the environment, and the app keeps working without code changes.
Manual secret handling risks leaks and mistakes.
Environment variables keep secrets safe outside code.
They simplify updates and improve security.
Practice
Solution
Step 1: Understand environment variables
Environment variables hold data like API keys or URLs outside the code to keep it safe and flexible.Step 2: Identify their use in Supabase
Supabase uses environment variables to keep secrets safe and avoid hardcoding sensitive info.Final Answer:
To store important information like API keys outside the code -> Option CQuick Check:
Environment variables = store secrets safely [OK]
- Thinking environment variables are for writing SQL
- Confusing environment variables with user data
- Assuming environment variables display data
SUPABASE_URL in a Supabase project?Solution
Step 1: Recall environment variable syntax in JavaScript
In JavaScript, environment variables are accessed usingprocess.env.VARIABLE_NAME.Step 2: Match with Supabase usage
Supabase projects use this standard way to access environment variables likeSUPABASE_URL.Final Answer:
process.env.SUPABASE_URL -> Option BQuick Check:
Access env vars with process.env.VAR [OK]
- Using function calls like getEnv() which don't exist
- Trying to access variables as object properties incorrectly
- Forgetting the process.env prefix
const supabaseUrl = process.env.SUPABASE_URL; const supabaseKey = process.env.SUPABASE_ANON_KEY; console.log(supabaseUrl, supabaseKey);
What will be the output if
SUPABASE_URL is set to https://xyz.supabase.co and SUPABASE_ANON_KEY is not set?Solution
Step 1: Check environment variable values
SUPABASE_URLis set to a URL string, butSUPABASE_ANON_KEYis missing, so it isundefined.Step 2: Understand console.log output
Logging both variables prints the URL string andundefinedfor the missing key.Final Answer:
https://xyz.supabase.co undefined -> Option AQuick Check:
Missing env vars print undefined [OK]
- Expecting an error when env var is missing
- Assuming missing vars print null
- Confusing undefined with empty string
API_SECRET in the Supabase dashboard but your app still logs undefined when accessing process.env.API_SECRET. What is the most likely cause?Solution
Step 1: Understand environment variable loading
Environment variables are loaded when the app starts, so changes require a restart to take effect.Step 2: Identify the cause of undefined
If the app logs undefined, it likely did not reload the updated environment variables after adding the secret.Final Answer:
You forgot to restart the app after adding the secret -> Option DQuick Check:
Restart app to load new env vars [OK]
- Thinking secret names are case-insensitive
- Believing Supabase doesn't support env vars
- Trying to hardcode secrets in code
Solution
Step 1: Understand secure storage of secrets
Secrets like API keys should never be hardcoded or exposed publicly to avoid leaks.Step 2: Use Supabase secrets in Edge Functions
Storing the API key as a secret in the dashboard and accessing it viaprocess.envinside the Edge Function keeps it secure and hidden from clients.Final Answer:
Store the API key as a secret in Supabase dashboard and access it via process.env inside the Edge Function -> Option AQuick Check:
Use secrets in server code, not client [OK]
- Hardcoding keys in code risking exposure
- Sending keys from client risking interception
- Publishing keys publicly causing leaks
Ready to prove it?
Pass the test and this pattern moves unknown
