Overview
Music Store uses environment variables to securely configure the Supabase connection. These variables are loaded at build time using Vite’s environment variable system.Required Variables
The application requires two environment variables to connect to Supabase:VITE_SUPABASE_URL
VITE_SUPABASE_URL
Description: Your unique Supabase project URLFormat:
https://xxxxx.supabase.coWhere to find it:- Go to your Supabase Dashboard
- Select your project
- Navigate to Settings > API
- Copy the Project URL
VITE_SUPABASE_KEY
VITE_SUPABASE_KEY
Description: Your Supabase anonymous (public) API keyFormat: Long alphanumeric string (JWT token)Where to find it:
- Go to your Supabase Dashboard
- Select your project
- Navigate to Settings > API
- Copy the Project API keys > anon public key
Setup Instructions
Create .env.local File
In the root of your project (same directory as
package.json), create a file named .env.local:The file must be named exactly
.env.local with the dot prefix. Do not name it env.local or .env.Add Your Variables
Open Replace the placeholder values with your actual Supabase project URL and anon key from the dashboard.
.env.local and add your Supabase credentials:.env.local
Verify .gitignore
Ensure This prevents accidentally committing sensitive credentials to version control.
.env.local is listed in your .gitignore file:.gitignore
How It Works
The Supabase client inbackend/supabaseClient.js reads these variables using Vite’s import.meta.env:
backend/supabaseClient.js
Vite Requirement: Environment variables must be prefixed with
VITE_ to be exposed to client-side code. Variables without this prefix are not accessible.Environment Variable Naming
Correct
Incorrect
Security Best Practices
Use the Correct API Key
Use the Correct API Key
Always use the
anon (public) key in your frontend:- ✅ anon (public) key - Safe for client-side use, respects Row Level Security
- ❌ service_role key - Full admin access, bypasses all security rules
service_role key should only be used in secure backend environments, never in frontend code.Never Commit .env.local
Never Commit .env.local
Add Use a template file like
.env.local to your .gitignore:.gitignore
.env.example to document required variables:.env.example
Rotate Keys if Exposed
Rotate Keys if Exposed
If your
anon key is accidentally committed:- Go to Settings > API in Supabase Dashboard
- Click Reset API Key (for anon key)
- Update your
.env.localwith the new key - Restart your dev server
Use Row Level Security
Use Row Level Security
Enable RLS on your Supabase tables to control data access:This ensures the
anon key can only access data as intended, even if exposed.Deployment
For production deployments, set environment variables in your hosting platform:- Vercel
- Netlify
- GitHub Actions
- Go to your project settings
- Navigate to Environment Variables
- Add:
VITE_SUPABASE_URL→ your Supabase URLVITE_SUPABASE_KEY→ your anon key
- Redeploy your application
Troubleshooting
Error: supabaseUrl is required
Error: supabaseUrl is required
Cause: Environment variables not loaded properlySolutions:
- Verify file is named exactly
.env.local - Check that variables start with
VITE_prefix - Restart the dev server (
npm run dev) - Ensure
.env.localis in the project root (same level aspackage.json)
Variables are undefined in code
Variables are undefined in code
Cause: Missing
VITE_ prefix or server not restartedSolutions:- Add
VITE_prefix to all variables - Stop and restart the development server
- Check for typos in variable names
- Verify you’re using
import.meta.env.VITE_*notprocess.env.*
Connection fails in production
Connection fails in production
Cause: Environment variables not set in hosting platformSolutions:
- Add variables to your hosting platform’s environment settings
- Verify variable names match exactly (case-sensitive)
- Trigger a fresh deployment after adding variables
- Check build logs for “undefined” warnings
Invalid API key error
Invalid API key error
Cause: Wrong key or typo in
.env.localSolutions:- Copy the key directly from Supabase Dashboard
- Ensure you’re using the
anonkey, notservice_role - Check for extra spaces or line breaks in the key
- Verify the key corresponds to the correct project URL
Example .env.local File
Here’s a complete example of a properly formatted.env.local file:
.env.local
Next Steps
Supabase Setup
Configure your Supabase project and get your API keys
Quick Start
Run the application locally
Project Structure
Learn where environment variables are used
Deployment
Deploy your application to production
