Required Variables
Supabase Connection
These variables are required for the application to connect to your Supabase project:Variable Descriptions
NEXT_PUBLIC_SUPABASE_URL
- Type: Public (exposed to browser)
- Required: Yes
- Description: Your Supabase project URL
- Example:
https://xyzcompany.supabase.co - Used in: Client-side and server-side Supabase connections
- Go to your Supabase Dashboard
- Select your project
- Navigate to Project Settings → API
- Copy the Project URL
NEXT_PUBLIC_SUPABASE_ANON_KEY
- Type: Public (exposed to browser)
- Required: Yes
- Description: Supabase anonymous/public key for client-side operations
- Used in: Client-side Supabase connections, respects Row Level Security (RLS)
- Go to Project Settings → API
- Copy the anon public key (under “Project API keys”)
The anon key is safe to expose publicly because it respects Row Level Security policies. All database access is controlled by RLS.
SUPABASE_SERVICE_ROLE_KEY
- Type: Secret (server-side only)
- Required: Yes (for admin operations)
- Description: Supabase service role key with full database access, bypasses RLS
- Used in: Server-side admin operations like user management
- Go to Project Settings → API
- Copy the service_role key
- Click “Reveal” to see the full key
lib/actions/users.ts
Setup Instructions
Create Local Environment File
Create a
.env.local file in your project root:.env.local is automatically ignored by git (included in .gitignore). Never commit this file.How Variables Are Used
Client-Side Connection
The browser client uses public variables:lib/supabase/client.ts
Server-Side Connection
Server components use the same public variables but with cookie handling:lib/supabase/server.ts
Middleware Connection
Middleware protects routes and manages authentication:lib/supabase/middleware.ts
Production Configuration
For production deployments, set environment variables in your hosting platform:Vercel
- Go to your project in Vercel dashboard
- Navigate to Settings → Environment Variables
- Add all three variables
- Select environment: Production, Preview, and Development
- Click Save
Vercel automatically encrypts environment variables and only exposes them at build and runtime.
Other Platforms
For other hosting platforms (Netlify, Railway, Render, etc.):- Refer to their environment variable configuration documentation
- Ensure service role key is marked as secret or encrypted
- Test the configuration in a staging environment first
Security Best Practices
Never Commit Secrets
Add
.env.local to .gitignore and never commit it to version control.Use Service Role Carefully
Only use
SUPABASE_SERVICE_ROLE_KEY in server-side code for admin operations.Rotate Keys Regularly
Periodically rotate your service role key in Supabase settings.
Separate Environments
Use different Supabase projects for development, staging, and production.
Troubleshooting
”Invalid API key” Error
- Verify you copied the complete key without truncation
- Check for extra spaces or line breaks in the
.env.localfile - Ensure variable names match exactly (case-sensitive)
Variables Not Loading
- Restart your Next.js development server after changing
.env.local - Check that the file is named exactly
.env.local(not.envor.env.development) - Verify the file is in the project root directory
Authentication Not Working
- Confirm
NEXT_PUBLIC_SUPABASE_URLandNEXT_PUBLIC_SUPABASE_ANON_KEYare correct - Check Supabase dashboard for any project issues
- Verify authentication providers are enabled in Supabase
Next Steps
Production Deployment
Deploy your application to production
Supabase Setup
Learn about the Supabase configuration
