Bird
Raised Fist0
Supabasecloud~30 mins

Environment management in Supabase - Mini Project: Build & Apply

Choose your learning style10 modes available
Environment Management with Supabase
📖 Scenario: You are working on a web app project that uses Supabase as its backend. You want to manage different environments like development and production to keep your data and settings separate and safe.
🎯 Goal: Build a simple environment management setup in Supabase by creating environment variables and using them to configure your project connection.
📋 What You'll Learn
Create a dictionary to hold environment variables for development and production
Add a variable to select the current environment
Write a function to get the correct Supabase URL and key based on the selected environment
Use the function to set up the Supabase client configuration
💡 Why This Matters
🌍 Real World
Managing different environments helps keep development and production data separate and secure, preventing accidental data loss or exposure.
💼 Career
Environment management is a key skill for cloud developers and DevOps engineers to ensure safe and reliable deployments.
Progress0 / 4 steps
1
Create environment variables dictionary
Create a dictionary called env_vars with two keys: development and production. Each key should map to another dictionary containing SUPABASE_URL and SUPABASE_KEY with these exact values:

development: SUPABASE_URL = "https://dev.supabase.co", SUPABASE_KEY = "devkey123"
production: SUPABASE_URL = "https://prod.supabase.co", SUPABASE_KEY = "prodkey456"
Supabase
Hint

Use a dictionary with nested dictionaries for each environment.

2
Add current environment selector
Create a variable called current_env and set it to the string "development" to select the current environment.
Supabase
Hint

Just assign the string "development" to current_env.

3
Create function to get environment config
Write a function called get_supabase_config that takes no arguments and returns the dictionary of SUPABASE_URL and SUPABASE_KEY for the current environment using current_env and env_vars.
Supabase
Hint

Return the dictionary for the key current_env from env_vars.

4
Set up Supabase client configuration
Create a variable called supabase_config and set it to the result of calling get_supabase_config(). This will hold the current environment's Supabase URL and key.
Supabase
Hint

Call the function and assign its result to supabase_config.

Practice

(1/5)
1. What is the main purpose of using different environments in Supabase projects?
easy
A. To separate app setups for safety and organization
B. To increase the speed of the database
C. To reduce the size of the project files
D. To allow multiple users to edit the same data simultaneously

Solution

  1. Step 1: Understand environment roles

    Different environments like development, testing, and production keep setups separate.
  2. Step 2: Identify the main benefit

    This separation helps keep data safe and organized, avoiding mistakes.
  3. Final Answer:

    To separate app setups for safety and organization -> Option A
  4. Quick Check:

    Environment separation = safety and organization [OK]
Hint: Think safety and order when you hear 'environment' [OK]
Common Mistakes:
  • Confusing environment with database speed
  • Thinking environment reduces project size
  • Believing environment allows simultaneous data edits
2. Which of the following is the correct way to set an environment variable in Supabase CLI?
easy
A. supabase env create API_KEY=12345
B. supabase set-env API_KEY 12345
C. supabase env add API_KEY 12345
D. supabase env set API_KEY=12345

Solution

  1. Step 1: Recall Supabase CLI syntax

    The correct command to set environment variables uses 'env set' followed by key=value.
  2. Step 2: Match the correct syntax

    supabase env set API_KEY=12345 matches this pattern exactly: 'supabase env set API_KEY=12345'.
  3. Final Answer:

    supabase env set API_KEY=12345 -> Option D
  4. Quick Check:

    Set env variable = 'env set key=value' [OK]
Hint: Remember: 'env set' plus key=value sets variables [OK]
Common Mistakes:
  • Using 'set-env' instead of 'env set'
  • Using 'env add' which is invalid
  • Using 'env create' which is not a command
3. Given this Supabase environment setup code snippet:
supabase env list
supabase env set API_URL=https://dev.api.com
supabase env get API_URL

What will be the output of the last command?
medium
A. API_URL not found
B. https://dev.api.com
C. https://prod.api.com
D. Error: command not recognized

Solution

  1. Step 1: Understand the commands

    The first command lists environment variables, the second sets API_URL to 'https://dev.api.com'.
  2. Step 2: Check the get command output

    The last command retrieves the value of API_URL, which was set just before.
  3. Final Answer:

    https://dev.api.com -> Option B
  4. Quick Check:

    Get env variable = last set value [OK]
Hint: Get returns the last set value for the variable [OK]
Common Mistakes:
  • Expecting default or production URL instead
  • Thinking the variable is not found
  • Assuming command syntax error
4. You tried to access an environment variable in your Supabase project but got an empty value. Which of these is the most likely cause?
medium
A. Your internet connection is down
B. You used the wrong database password
C. You forgot to set the variable using supabase env set
D. You deployed to the wrong region

Solution

  1. Step 1: Identify cause of empty env variable

    An empty environment variable usually means it was never set or loaded.
  2. Step 2: Match cause to options

    Forgetting to set it with 'supabase env set' is the most direct cause.
  3. Final Answer:

    You forgot to set the variable using supabase env set -> Option C
  4. Quick Check:

    Empty env variable = not set [OK]
Hint: Empty env? Check if you set it first [OK]
Common Mistakes:
  • Blaming unrelated issues like internet or region
  • Confusing env variables with database passwords
  • Assuming deployment region affects env variables
5. You want to keep your production and development environments completely separate in Supabase. Which approach best achieves this?
hard
A. Create two separate Supabase projects, one for production and one for development
B. Use one project and switch environment variables manually before each deploy
C. Store all environment variables in a single file and comment out unused ones
D. Use the same database but different tables for production and development

Solution

  1. Step 1: Understand environment separation needs

    Complete separation means no shared resources or data between environments.
  2. Step 2: Evaluate options for separation

    Creating two separate projects ensures full isolation of data, secrets, and settings.
  3. Step 3: Identify drawbacks of other options

    Switching variables manually or sharing database risks mistakes and leaks.
  4. Final Answer:

    Create two separate Supabase projects, one for production and one for development -> Option A
  5. Quick Check:

    Separate projects = full environment isolation [OK]
Hint: Separate projects keep environments fully isolated [OK]
Common Mistakes:
  • Relying on manual variable switching
  • Sharing database tables between environments
  • Using one file with commented variables
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard