Overview
The BE Monorepo uses a structured approach to environment variable management, ensuring type-safety and clear separation between development and production configurations. Each application maintains its own environment files while following consistent patterns.Environment Files Structure
Environment variables are managed using multiple.env files:
Hono App Environment Variables
Required Variables
The following variables are required for the Hono app to run:.env.example
Variable Descriptions
Application Variables
Application Variables
APP_TITLE
- Type:
string - Description: Display name for the API
- Example:
"Hono API" - Required: Yes
- Type:
string - Description: Base URL where the API is hosted
- Development:
http://localhost:3333 - Production: Your deployed API URL
- Required: Yes
Database Variables
Database Variables
DATABASE_URL
- Type:
string - Description: PostgreSQL connection string
- Format:
postgres://[user]:[password]@[host]:[port]/[database] - Example:
postgres://admin:secretpass@localhost:5432/hono_db - Required: Yes
- Used by: Drizzle ORM for database connections
Authentication Variables
Authentication Variables
BETTER_AUTH_SECRET
- Type:
string - Description: Secret key for Better Auth JWT signing
- Minimum length: 32 characters
- Required: Yes
- Security: Must be cryptographically random
Observability Variables
Observability Variables
OTEL_LOG_LEVEL
- Type:
enum - Description: OpenTelemetry logging level
- Options:
"ALL","VERBOSE","DEBUG","INFO","WARN","ERROR","NONE" - Default:
"INFO" - Required: Yes
- Development: Use
"DEBUG"for detailed logs - Production: Use
"INFO"or"WARN"
Type-Safe Environment Variables
The Hono app uses @t3-oss/env-core for runtime validation and type-safety:apps/hono/src/core/constants/env.ts
Benefits
- Type Safety: Full TypeScript autocomplete and type checking
- Runtime Validation: Fails fast if required variables are missing
- Schema Documentation: Zod schema serves as documentation
- Development Experience: Immediate feedback on configuration errors
Usage in Code
apps/hono/src/index.ts
Development vs Production
Development Environment
Use.env.dev for local development:
.env.dev
Production Environment
Use.env.prod for production:
.env.prod
Production secrets should be stored securely using environment variable management tools provided by your hosting platform (Vercel, Railway, AWS Secrets Manager, etc.).
Package Scripts with Environment Files
Thepackage.json includes convenience scripts for different environments:
apps/hono/package.json
Docker Environment Variables
For Docker deployments, use thedocker/.env.example file:
docker/.env.example
CI/CD Environment Variables
GitHub Actions Setup
The project uses GitHub Environments for CI/CD:-
Create two environments in your GitHub repository:
devenvironmentprodenvironment
-
Add environment secrets:
- Secret name:
HONO_ENV_FILE - Dev value: Contents of
.env.dev - Prod value: Contents of
.env.prod
- Secret name:
GitHub Actions Workflow
.github/workflows/ci.yml
The workflow references
secrets.HONO_ENV_FILE which pulls the environment file content from GitHub Secrets.Best Practices
1. Environment File Management
2. Secret Generation
Generate cryptographically secure secrets:3. Validation on Startup
The app validates environment variables on startup and fails fast:4. Source of Truth
Local Environment Files
.env.dev and .env.prod files in your local repository are the source of truth.5. Environment-Specific Behavior
Adding New Environment Variables
To add a new environment variable:Troubleshooting
Error: Missing required environment variable
Error: Missing required environment variable
Cause: A required environment variable is not set or is empty.Solution:
- Check if the variable exists in your
.env.devor.env.prodfile - Ensure the file is being loaded with
--env-fileflag - Verify the variable name matches exactly (case-sensitive)
- Check that the value is not empty
Environment file not loading
Environment file not loading
Cause: The
--env-file flag is missing or pointing to wrong file.Solution:Different behavior in CI vs local
Different behavior in CI vs local
Cause: CI environment secrets don’t match local env files.Solution:
- Compare local
.env.devwith GitHub Secret content - Update GitHub Secrets to match:
Next Steps
TypeScript Config
Learn about the shared TypeScript configuration
Monorepo Architecture
Understand the workspace structure
