Environment Variables
Note: This guide is for developers and administrators setting up the FG API platform. End users don't need to configure environment variables - they can use the platform directly.
This guide covers all environment variables used in the FG API platform.
Backend Environment Variables (Railway)
Required Variables
NODE_ENV
Description: Environment mode
Values:
development,production,testExample:
productionUsage: Determines CORS settings and logging levels
DATABASE_URL
Description: PostgreSQL connection string
Format:
postgresql://user:password@host:port/databaseExample:
postgresql://user:password@host.railway.app:5432/databaseUsage: Prisma database connection
JWT_SECRET
Description: Secret key for JWT access tokens
Requirements: Strong, random string (32+ characters)
Example:
your-very-strong-jwt-secret-key-hereUsage: Signing and verifying access tokens
REFRESH_TOKEN_SECRET
Description: Secret key for refresh tokens
Requirements: Different from JWT_SECRET, strong and random
Example:
your-very-strong-refresh-token-secretUsage: Signing and verifying refresh tokens
REGISTRATION_APPROVAL_CODE
Description: Code required for user registration
Requirements: Secure code for account approval
Example:
APPROVAL123Usage: Validates registration requests
Optional Variables
CLOUDFLARE_PAGES_URL
Description: Frontend URL for CORS configuration
Format:
https://your-domain.comExample:
https://your-frontend.pages.devUsage: Allows cross-origin requests from frontend
CLIENT_URL
Description: Alternative client URL
Format:
https://your-domain.comExample:
https://your-custom-domain.comUsage: Additional CORS origin
RAILWAY_STATIC_URL
Description: Railway static URL
Format:
https://your-app.railway.appExample:
https://your-backend.railway.appUsage: Railway-specific CORS origin
Frontend Environment Variables (Cloudflare Pages)
Required Variables
VITE_BASE_URL
Description: Backend API base URL
Format:
https://your-backend.railway.appExample:
https://your-backend.railway.appUsage: API request base URL
Optional Variables
VITE_LOGIN_URL
Description: Custom login endpoint URL
Format:
https://your-backend.railway.app/app/loginExample:
https://fg-api-server.railway.app/app/loginUsage: Override default login URL
VITE_REGISTER_URL
Description: Custom register endpoint URL
Format:
https://your-backend.railway.app/app/registerExample:
https://fg-api-server.railway.app/app/registerUsage: Override default register URL
VITE_REFRESH_URL
Description: Custom refresh endpoint URL
Format:
https://your-backend.railway.app/app/refreshExample:
https://fg-api-server.railway.app/app/refreshUsage: Override default refresh URL
Environment Setup
Development Setup
Create a .env file in your server directory:
# Server .env file
NODE_ENV=development
DATABASE_URL=postgresql://postgres:password@localhost:5432/fg_api_dev
JWT_SECRET=your-development-jwt-secret
REFRESH_TOKEN_SECRET=your-development-refresh-secret
REGISTRATION_APPROVAL_CODE=DEV123
CLOUDFLARE_PAGES_URL=http://localhost:5173Create a .env file in your client directory:
# Client .env file
VITE_BASE_URL=http://localhost:3000Production Setup
Railway (Backend)
Set environment variables in Railway dashboard:
NODE_ENV=production
DATABASE_URL=postgresql://user:password@host.railway.app:5432/database
JWT_SECRET=your-production-jwt-secret-very-strong
REFRESH_TOKEN_SECRET=your-production-refresh-secret-very-strong
REGISTRATION_APPROVAL_CODE=PROD123
CLOUDFLARE_PAGES_URL=https://your-frontend.pages.devCloudflare Pages (Frontend)
Set environment variables in Cloudflare Pages dashboard:
VITE_BASE_URL=https://your-backend.railway.appSecurity Best Practices
Secret Generation
Generate Strong Secrets
# Generate JWT secret
openssl rand -base64 32
# Generate refresh token secret
openssl rand -base64 32
# Generate approval code
openssl rand -hex 16Secret Requirements
Length: At least 32 characters
Complexity: Mix of letters, numbers, and symbols
Uniqueness: Different secrets for different environments
Rotation: Change secrets regularly
Environment Security
Never Commit Secrets
# Add to .gitignore
.env
.env.local
.env.production
server/.env
client/.envUse Different Secrets
Development: Use simple, memorable secrets
Production: Use strong, random secrets
Testing: Use dedicated test secrets
Configuration Examples
Complete Development Setup
Server Configuration
# server/.env
NODE_ENV=development
DATABASE_URL=postgresql://postgres:password@localhost:5432/fg_api_dev
JWT_SECRET=dev-jwt-secret-key-12345
REFRESH_TOKEN_SECRET=dev-refresh-secret-key-67890
REGISTRATION_APPROVAL_CODE=DEV123
CLOUDFLARE_PAGES_URL=http://localhost:5173Client Configuration
# client/.env
VITE_BASE_URL=http://localhost:3000Complete Production Setup
Railway Configuration
NODE_ENV=production
DATABASE_URL=postgresql://user:password@host.railway.app:5432/database
JWT_SECRET=prod-jwt-secret-very-strong-and-secure-12345
REFRESH_TOKEN_SECRET=prod-refresh-secret-very-strong-and-secure-67890
REGISTRATION_APPROVAL_CODE=PROD123
CLOUDFLARE_PAGES_URL=https://your-frontend.pages.devCloudflare Pages Configuration
VITE_BASE_URL=https://your-backend.railway.appTroubleshooting
Common Issues
Environment Variables Not Loading
Check variable names (case-sensitive)
Verify file location (.env in correct directory)
Restart development server after changes
CORS Errors
Verify CLOUDFLARE_PAGES_URL is set correctly
Check frontend URL matches backend CORS configuration
Ensure URLs include protocol (https://)
Database Connection Errors
Verify DATABASE_URL format
Check database credentials
Ensure database is running and accessible
Authentication Errors
Verify JWT_SECRET and REFRESH_TOKEN_SECRET are set
Check secret strength and uniqueness
Ensure secrets are different between environments
Validation
Check Environment Variables
// Server-side validation
const requiredVars = [
'NODE_ENV',
'DATABASE_URL',
'JWT_SECRET',
'REFRESH_TOKEN_SECRET',
'REGISTRATION_APPROVAL_CODE'
];
requiredVars.forEach(varName => {
if (!process.env[varName]) {
throw new Error(`Missing required environment variable: ${varName}`);
}
});Test Configuration
# Test database connection
npm run setup
# Test authentication
curl -X POST https://your-api.com/app/login \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"password"}'Getting Help
Documentation
Check this environment variables guide
Review deployment documentation
Look at security best practices
Support
Contact support through the platform
Check error logs for specific issues
Verify environment variable configuration