Overview
Sistema Financiero uses environment variables to store sensitive configuration like API keys and database URLs. This keeps secrets out of your code and allows different configurations for development and production.Environment File Setup
Copy Example File
Create your environment file from the template:
Terminal
.env.local is automatically loaded by Next.js and takes precedence over .env.Required Variables
These environment variables are mandatory for the application to function:NEXT_PUBLIC_SUPABASE_URL
Description: Your Supabase project URL Where to find it:- Go to your Supabase Dashboard
- Select your project
- Go to Settings → API
- Copy the Project URL
The
NEXT_PUBLIC_ prefix makes this variable available in browser code. This is safe because the URL is public.NEXT_PUBLIC_SUPABASE_ANON_KEY
Description: Your Supabase anonymous (public) API key Where to find it:- Go to your Supabase Dashboard
- Select your project
- Go to Settings → API
- Copy the anon public key (not the service_role key!)
NEXT_PUBLIC_APP_URL
Description: The base URL where your app is deployed Development:This is used for OpenRouter API attribution and generating absolute URLs (e.g., for receipts).
Optional Variables
These variables enable additional features:OPENROUTER_API_KEY
Description: API key for AI-powered chat features Required for:- AI chat agent (
/agente-mejoradopage) - OCR receipt scanning (
/upload-imageAPI) - Natural language transaction entry
- Go to OpenRouter.ai
- Sign up for a free account
- Go to Keys page
- Click Create Key
- Copy the key (starts with
sk-or-v1-)
This variable does NOT have the
NEXT_PUBLIC_ prefix - it’s server-side only for security.- OpenRouter offers a free tier with $0.05 initial credit
- Most models cost 0.01 per request
- Sistema Financiero uses
google/gemini-2.5-flash(very affordable)
Environment Variable Precedence
Next.js loads environment files in this order (later files override earlier):.env- Default values for all environments.env.local- Local overrides (git-ignored).env.development- Development-specific.env.development.local- Local dev overrides.env.production- Production-specific.env.production.local- Local prod overrides
Accessing Variables in Code
Client-Side (Browser)
Variables withNEXT_PUBLIC_ prefix:
components/Example.tsx
Server-Side (API Routes / Server Components)
All variables are accessible:app/api/example/route.ts
Production Deployment
Vercel
Add environment variables in the Vercel dashboard:Go to Project Settings
- Open your project in Vercel Dashboard
- Click Settings
- Click Environment Variables
Add Variables
For each variable:
- Key: Variable name (e.g.,
NEXT_PUBLIC_SUPABASE_URL) - Value: Variable value
- Environment: Select
Production,Preview, andDevelopment - Click Save
Checking all three environments ensures variables work in preview deployments and local development with Vercel CLI.
Railway
Add variables in the Railway dashboard:Open Service Settings
- Go to Railway Dashboard
- Select your project
- Click on your service
- Go to Variables tab
Add Variables
Click New Variable and add each variable:
NEXT_PUBLIC_SUPABASE_URLNEXT_PUBLIC_SUPABASE_ANON_KEYOPENROUTER_API_KEYNEXT_PUBLIC_APP_URL(use your Railway URL, e.g.,https://your-app.up.railway.app)
Other Platforms
Netlify
Netlify
- Go to Site settings → Build & deploy → Environment
- Click Edit variables
- Add each variable
- Redeploy your site
Render
Render
- Go to your service dashboard
- Click Environment tab
- Add each variable
- Click Save Changes
- Manually trigger a deploy from the Manual Deploy dropdown
Docker
Docker
Pass variables using Or use a
-e flag:.env file:Security Best Practices
Never Commit Secrets
- Add
.env.localto.gitignore - Use
.env.examplefor documentation - Rotate keys if accidentally committed
Use NEXT_PUBLIC_ Carefully
- Only for non-sensitive data
- Exposed to all website visitors
- Examples: Supabase URL, app version
Rotate Keys Regularly
- Change API keys every 90 days
- Use Supabase’s key rotation feature
- Update all deployment environments
Different Keys per Environment
- Use separate Supabase projects for dev/prod
- Different OpenRouter keys for testing
- Prevents accidental data mixing
Validating Your Setup
Check Environment Variables
Create a test API route to verify variables are loaded:app/api/test-env/route.ts
http://localhost:3000/api/test-env to see:
Response
Test Supabase Connection
app/api/test-supabase/route.ts
Troubleshooting
Error: Missing Supabase environment variables
Error: Missing Supabase environment variables
Problem: App crashes with “Missing Supabase environment variables”Solution:
- Verify
.env.localexists and is in the project root - Check variable names match exactly (case-sensitive)
- Restart dev server after adding variables
- Ensure no extra spaces around
=sign:
process.env.VARIABLE returns undefined
process.env.VARIABLE returns undefined
Problem: Variable shows undefined in codeSolution:
- Client-side: Add
NEXT_PUBLIC_prefix - Server-side: Restart dev server (Next.js caches env vars)
- Check for typos in variable name
- Verify
.env.localis in project root (notsrc/orapp/)
Variables work locally but not in production
Variables work locally but not in production
Problem: App works on localhost but fails on Vercel/RailwaySolution:
- Add variables to deployment platform’s UI (not just
.env.local) - Check you added them to “Production” environment
- Trigger a new deployment after saving variables
- Check deployment logs for specific error messages
OpenRouter API calls fail
OpenRouter API calls fail
Problem: AI chat doesn’t work, 401/403 errorsSolution:
- Verify
OPENROUTER_API_KEYis set (noNEXT_PUBLIC_prefix) - Check you have credits at OpenRouter dashboard
- Ensure key starts with
sk-or-v1- - Test with curl:
Next Steps
Environment configured! Now deploy your application:Deployment
Deploy Sistema Financiero to Vercel, Railway, or other platforms