Environment Variables
The SSP Backend API uses environment variables for configuration. This guide provides a complete reference of all available variables and their usage.Environment File Setup
Create a.env file in the project root:
Complete Configuration Template
Here’s a complete.env template with all available variables:
Database Configuration
PostgreSQL database connection settings.DB_HOST
Description: PostgreSQL server hostname or IP address Type: String Default:localhost
Examples:
src/app.module.ts:16
DB_PORT
Description: PostgreSQL server port Type: Number Default:5432
Examples:
src/app.module.ts:17
DB_USERNAME
Description: PostgreSQL username for authentication Type: String Required: Yes Examples:src/app.module.ts:18
DB_PASSWORD
Description: PostgreSQL password for authentication Type: String Required: Yes Security: Examples:src/app.module.ts:19
DB_NAME
Description: PostgreSQL database name Type: String Required: Yes Examples:src/app.module.ts:20
JWT Configuration
JSON Web Token authentication settings.JWT_SECRET
Description: Secret key used to sign and verify JWT tokens Type: String Required: Yes (falls back to insecure default) Default:'dev_secret_change_me' (development only)
Minimum Length: 32 characters recommended
Security:
Generating Secure Secrets:
src/shared/auth/auth.module.ts:19-20 and src/shared/auth/jwt.strategy.ts:11
JWT_EXPIRES_IN
Description: JWT token expiration time Type: String (time span) Default:'1d' (1 day)
Format: Uses ms format
Examples:
Short Expiration (1h-12h)
Pros: More secure, limits token theft impactCons: Users must login more frequently
Medium Expiration (1d-7d)
Pros: Balance of security and convenienceCons: Stolen tokens valid longer
Long Expiration (30d+)
Pros: Better user experienceCons: Security risk if token is compromised
Recommendation
1 day (24h) provides good balanceAdjust based on security requirements
src/shared/auth/auth.module.ts:21-22
Server Configuration
Application server settings.PORT
Description: Port number for the HTTP server Type: Number Default:3000
Range: 1-65535 (recommend 3000-9999 for development)
Examples:
src/main.ts:8
- Ports below 1024 typically require root/admin privileges
- Ensure the port is not already in use
- In production, typically use port 80 (HTTP) or 443 (HTTPS) behind a reverse proxy
Seeding Configuration
Database seeding settings.SEED_ADMIN_PASSWORD
Description: Password for the initial admin user created by seed scripts Type: String Default:'Admin1234'
Examples:
src/seeds/seed-admin.ts:31 and src/seeds/seeder.service.ts:35
Environment-Specific Configurations
Development Environment
Staging Environment
Production Environment
Loading Environment Variables
The application loads environment variables using the@nestjs/config package.
Configuration Module
Fromsrc/app.module.ts:10:
ConfigService available globally in all modules.
Accessing Variables in Code
InjectConfigService to access environment variables:
Best Practices
Environment-Specific Files
Use different
.env files for each environment:.env.development.env.staging.env.production
Docker Compose Configuration
When using Docker Compose, you can define environment variables indocker-compose.yml:
Troubleshooting
Environment variables not loading
Environment variables not loading
Ensure:
.envfile is in the project root- File is named exactly
.env(not.env.txt) - No spaces around
=signs:DB_HOST=localhostnotDB_HOST = localhost - Variables don’t use quotes unless values contain spaces
- Application is restarted after changing
.env
Database connection fails
Database connection fails
Check:
- All
DB_*variables are set correctly - PostgreSQL is running
- Database exists:
psql -l | grep ssp_db - User has access:
psql -U ssp_user -d ssp_db - Network connectivity if using remote database
JWT token errors
JWT token errors
Verify:
JWT_SECRETis set and at least 32 charactersJWT_EXPIRES_INuses valid format (1h, 1d, etc.)- Same secret is used across all app instances
- Secret hasn’t changed (invalidates existing tokens)
What’s Next?
Database Setup
Configure PostgreSQL and TypeORM
Authentication
Understand JWT configuration and usage
Running Locally
Set up your development environment
Deployment
Deploy with environment-specific configurations
