📖 Scenario: You are building a simple web app using Supabase. You need to securely store and use environment variables and secrets like API keys and database URLs.
🎯 Goal: Create a Supabase project configuration that uses environment variables to store sensitive information and access them securely in your app.
📋 What You'll Learn
Create environment variables for SUPABASE_URL and SUPABASE_ANON_KEY
Add a configuration file that reads these environment variables
Initialize the Supabase client using the environment variables
Ensure the secrets are not hardcoded in the code
💡 Why This Matters
🌍 Real World
Storing API keys and secrets in environment variables is a common practice to keep sensitive data safe and separate from code.
💼 Career
Understanding environment variables and secret management is essential for cloud developers and DevOps engineers to build secure applications.
Progress0 / 4 steps
1
Create environment variables
Create two environment variables called SUPABASE_URL and SUPABASE_ANON_KEY with these exact values: https://xyzcompany.supabase.co and public-anon-key-123 respectively.
Supabase
Hint
Environment variables are usually stored in a file named .env in your project root.
2
Add configuration to read environment variables
In a file called supabaseClient.js, import createClient from @supabase/supabase-js and create two constants supabaseUrl and supabaseAnonKey that read from process.env.SUPABASE_URL and process.env.SUPABASE_ANON_KEY respectively.
Supabase
Hint
Use process.env.VARIABLE_NAME to access environment variables in Node.js.
3
Initialize Supabase client
Use createClient with supabaseUrl and supabaseAnonKey to create a constant called supabase.
Supabase
Hint
Pass the URL and anon key to createClient to initialize Supabase.
4
Export the Supabase client
Export the constant supabase as the default export from supabaseClient.js.
Supabase
Hint
Use export default supabase; to export the client for use in other files.
Practice
(1/5)
1. What is the main purpose of environment variables in Supabase?
easy
A. To display data on the website
B. To write SQL queries directly in the dashboard
C. To store important information like API keys outside the code
D. To create user accounts automatically
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 C
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.