Environment variables and secrets help keep important information safe and separate from your code. They make it easy to change settings without changing the code.
When you need to store API keys safely without putting them in your code.
When you want to change database connection details without editing your app.
When you deploy the same app to different places with different settings.
When you want to keep passwords or tokens hidden from public view.
When you want to update secrets without restarting or changing your app code.
Syntax
Supabase
SUPABASE_URL=your-supabase-url
SUPABASE_ANON_KEY=your-anon-key
# In Supabase dashboard, find these in Project Settings > API
Environment variables are key-value pairs stored outside your code.
Secrets like API keys should never be hardcoded or shared publicly.
Examples
Example of setting environment variables for Supabase URL and anonymous key.
Accessing environment variables in a Node.js app to connect to Supabase.
Supabase
// Using environment variables in JavaScript
const supabaseUrl = process.env.SUPABASE_URL;
const supabaseAnonKey = process.env.SUPABASE_ANON_KEY;
How to find your Supabase URL and anon key in the dashboard.
Supabase
# In Supabase dashboard# Go to Project Settings > API# Find your Project URL (SUPABASE_URL) and anon key (SUPABASE_ANON_KEY)
Sample Program
This example shows how to store Supabase URL and anon key in environment variables and use them in a JavaScript app to connect securely. The getUser function fetches the current user info.
What will be the output if SUPABASE_URL is set to https://xyz.supabase.co and SUPABASE_ANON_KEY is not set?
medium
A. https://xyz.supabase.co undefined
B. undefined undefined
C. https://xyz.supabase.co null
D. Error: SUPABASE_ANON_KEY not found
Solution
Step 1: Check environment variable values
SUPABASE_URL is set to a URL string, but SUPABASE_ANON_KEY is missing, so it is undefined.
Step 2: Understand console.log output
Logging both variables prints the URL string and undefined for the missing key.
Final Answer:
https://xyz.supabase.co undefined -> Option A
Quick Check:
Missing env vars print undefined [OK]
Hint: Missing env vars show as undefined in logs [OK]
Common Mistakes:
Expecting an error when env var is missing
Assuming missing vars print null
Confusing undefined with empty string
4. You added a secret API_SECRET in the Supabase dashboard but your app still logs undefined when accessing process.env.API_SECRET. What is the most likely cause?
medium
A. You must include the secret in your code manually
B. The secret name is case-insensitive and should be lowercase
C. Supabase does not support environment variables
D. You forgot to restart the app after adding the secret
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 D
Quick Check:
Restart app to load new env vars [OK]
Hint: Restart app after adding secrets to load them [OK]
Common Mistakes:
Thinking secret names are case-insensitive
Believing Supabase doesn't support env vars
Trying to hardcode secrets in code
5. You want to securely use a third-party API key in your Supabase Edge Function without exposing it in client code. Which approach is best?
hard
A. Store the API key as a secret in Supabase dashboard and access it via process.env inside the Edge Function
B. Hardcode the API key directly in the Edge Function code
C. Send the API key from the client app to the Edge Function on every request
D. Publish the API key in a public GitHub repo for easy access
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 via process.env inside 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 A
Quick Check:
Use secrets in server code, not client [OK]
Hint: Keep API keys in secrets, access only server-side [OK]
Common Mistakes:
Hardcoding keys in code risking exposure
Sending keys from client risking interception
Publishing keys publicly causing leaks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.