Overview
PrivyCode server uses environment variables for configuration. In local development, these are loaded from a.env file. In production, they should be set in your hosting platform’s environment settings.
The server automatically loads
.env files when GO_ENV is not set to production. In production environments, set GO_ENV=production to skip .env file loading.Required Variables
These variables must be set for the server to function properly.PORT
The port number the server will listen on.Example:
If not set, the server defaults to port
8080. Some platforms like Railway may override this with their own PORT variable.DATABASE_URL
PostgreSQL connection string for the database.Format:Examples:Used in:
config/config.go:27- Database connection initialization
GITHUB_CLIENT_ID
OAuth application Client ID from GitHub.Example:How to get:
- Go to GitHub Developer Settings
- Click “OAuth Apps” → “New OAuth App”
- Copy the Client ID after creating the app
internal/github/oauth.go:14- OAuth configuration
GITHUB_CLIENT_SECRET
OAuth application Client Secret from GitHub.Example:How to get:
- In your GitHub OAuth App settings
- Click “Generate a new client secret”
- Copy the secret immediately (it won’t be shown again)
internal/github/oauth.go:15- OAuth configuration
GITHUB_CALLBACK_URL
The callback URL GitHub will redirect to after authentication.Format:Examples:Used in:
internal/github/oauth.go:17- OAuth redirect configuration
FRONTEND_URL
The URL of your frontend application.Examples:Used for:
- CORS configuration (must be added to
internal/middleware/cors.go) - Redirecting users after authentication
- Setting cookies with correct domain
If your frontend runs on a different port during development (e.g., Vite default is 5173), update this accordingly.
MOBILE_URL
The URL for mobile app deep linking or mobile web access.Examples:
If you don’t have a separate mobile app, set this to the same value as
FRONTEND_URL.Optional Variables
GO_ENV
Environment mode that controls Code reference:
.env file loading.Values:production- Skips loading.envfile, uses system environment variables- Any other value or unset - Loads
.envfile from project root
config/config.go
Example Configuration Files
.env.example (Included in Repository)
.env.example
Local Development (.env)
.env
Production (Railway)
Production (Docker)
Environment Variable Loading
The server loads environment variables in the following order:Load .env file (development only)
If
GO_ENV != "production", the server attempts to load .env from the project root using github.com/joho/godotenvRead from system environment
All variables are read using
os.Getenv(), which checks system environment variablesCode References
Here’s where each environment variable is used in the codebase:Database Configuration
config/config.go:18-31
GitHub OAuth Configuration
internal/github/oauth.go:11-20
Server Port Configuration
cmd/server/main.go:28-31
Validation Checklist
Use this checklist to ensure your environment is configured correctly:All required variables are set
Required variables
Required variables
-
DATABASE_URL -
GITHUB_CLIENT_ID -
GITHUB_CLIENT_SECRET -
GITHUB_CALLBACK_URL -
FRONTEND_URL -
MOBILE_URL
GitHub OAuth is configured
- Callback URL in
.envmatches GitHub OAuth App settings - Client ID and Secret are correct
- OAuth scopes include
read:userandrepo
Database connection works
- PostgreSQL is running and accessible
-
DATABASE_URLformat is correct - SSL mode is appropriate for your environment
CORS is configured
-
FRONTEND_URLis added to CORS allowed origins ininternal/middleware/cors.go - No trailing slashes in URLs
Security Best Practices
Use .gitignore
Ensure
.env is in your .gitignore fileRotate secrets
Regularly rotate
GITHUB_CLIENT_SECRET and database passwordsUse secrets management
Consider using Railway secrets, AWS Secrets Manager, or similar
Minimal permissions
Use database users with minimal required permissions
Troubleshooting
Error loading .env file
Error loading .env file
Cause:
.env file not found or GO_ENV=production is setSolution:- Ensure
.envexists in project root - Don’t set
GO_ENV=productionin local development - Check file permissions:
chmod 644 .env
Error connecting to DB
Error connecting to DB
Cause: Invalid
DATABASE_URL or database not accessibleSolution:- Verify
DATABASE_URLformat - Test connection:
psql "$DATABASE_URL" - Check if PostgreSQL is running
- Verify network/firewall settings
GitHub OAuth errors
GitHub OAuth errors
Cause: Mismatch between callback URLs or invalid credentialsSolution:
- Verify
GITHUB_CALLBACK_URLexactly matches GitHub settings - Regenerate client secret if needed
- Check OAuth app is not suspended
CORS errors
CORS errors
Cause: Frontend URL not in allowed originsSolution:
- Add your
FRONTEND_URLtointernal/middleware/cors.go - Rebuild and restart server after changes
- Ensure no trailing slashes in URLs
Next Steps
Setup Guide
Follow the complete local setup guide
Deployment
Deploy to production with proper configuration
Security
Learn about security best practices
API Reference
Explore available endpoints