Overview
AniDojo requires specific environment variables to connect to Supabase and function properly in production. This guide covers all required and optional environment variables.Required Environment Variables
These variables are essential for AniDojo to function:Supabase Configuration
Your Supabase project URLExample:
https://abcdefghijklmnop.supabase.coWhere to find:- Go to your Supabase Dashboard
- Select your project
- Navigate to Settings → API
- Copy the Project URL
Your Supabase anonymous/public API keyExample:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...Where to find:- Go to your Supabase Dashboard
- Select your project
- Navigate to Settings → API
- Copy the anon public key
This key is safe to use in client-side code. It’s protected by Row Level Security (RLS) policies in your database.
Local Development Setup
Production Setup (Vercel)
For production deployments on Vercel:Navigate to Project Settings
- Go to your project dashboard on Vercel
- Click Settings
- Select Environment Variables from the sidebar
Add Each Variable
For each required environment variable:
- Enter the Key (e.g.,
NEXT_PUBLIC_SUPABASE_URL) - Enter the Value (your actual credential)
- Select environments:
- ✅ Production (required)
- ✅ Preview (recommended)
- ⬜ Development (optional)
- Click Save
Adding variables to Preview environments ensures pull request deployments work correctly.
Environment Variable Types
Public vs Private Variables
Next.js has two types of environment variables: Public Variables (prefixed withNEXT_PUBLIC_)
- Exposed to the browser
- Can be used in client-side code
- Bundled into your JavaScript
- Example:
NEXT_PUBLIC_SUPABASE_URL
- Only available on the server
- Cannot be accessed in client-side code
- Used for sensitive secrets
- Example:
DATABASE_PASSWORD(if needed)
Verifying Configuration
Local Development
Verify your environment variables are loaded correctly:Production
Test your production environment:- Deploy your application
- Open browser DevTools → Console
- Run:
console.log(process.env.NEXT_PUBLIC_SUPABASE_URL) - Verify the correct URL is displayed
If you see
undefined, your environment variables are not configured correctly in Vercel.Using Environment Variables
In Client Components
src/lib/supabase/client.ts
In Server Components
src/lib/supabase/server.ts
In Middleware
src/lib/supabase/middleware.ts
Troubleshooting
”Cannot read property of undefined”
Cause: Environment variable not set or misspelled Solution:- Check variable names match exactly (including
NEXT_PUBLIC_prefix) - Verify variables are set in
.env.local(local) or Vercel dashboard (production) - Restart development server after changing
.env.local - Redeploy after updating Vercel environment variables
”Invalid API key” from Supabase
Cause: Incorrect or expired Supabase credentials Solution:- Verify you copied the correct keys from Supabase dashboard
- Check for extra spaces or newlines in the values
- Ensure you’re using the anon public key, not the service role key
- Verify your Supabase project is active (not paused)
Variables Not Updating
Local Development:- Restart your development server (
npm run dev) - Clear Next.js cache:
rm -rf .next
- Trigger a new deployment after updating variables
- Clear build cache in Vercel settings if issues persist
Security Concerns
“Is it safe to expose the anon key?” Yes! The Supabase anonymous key is designed to be public. Security is enforced through:- Row Level Security (RLS): Database policies control data access
- JWT verification: Supabase verifies user authentication tokens
- Rate limiting: Built-in protection against abuse
Best Practices
Use different Supabase projects for development and production
Use different Supabase projects for development and production
- Create separate Supabase projects for dev and prod
- Use different environment variables for each
- Prevents test data from polluting production
- Example:
dev.env.localand production variables in Vercel
Rotate keys periodically
Rotate keys periodically
- Rotate your Supabase anon key every few months
- Update environment variables in all environments
- Redeploy to apply changes
- Document key rotation dates
Document required variables
Document required variables
- Create an
.env.examplefile with dummy values - Commit it to version control
- Helps new developers set up their environment
- Example:
.env.example
Use type-safe environment variables
Use type-safe environment variables
Create a helper to validate environment variables:
lib/env.ts
Next Steps
Deploy to Vercel
Deploy your application to production
Database Migrations
Set up your production database