Skip to main content

Environment Variables

Rahat uses environment variables for configuration across its different services. This document lists the essential environment variables needed for development and deployment.

Core Environment Variables

API Service (packages/api/.env)

# Server Configuration
PORT=3001
NODE_ENV=development
API_PREFIX=api
CORS_ORIGINS=http://localhost:3000,https://app.rahat.io

# Database Configuration
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/rahat_db
DATABASE_SCHEMA=public
DATABASE_SYNC=true

# JWT Authentication
JWT_SECRET=your_jwt_secret_here
JWT_EXPIRES_IN=1d

# Blockchain Configuration
BLOCKCHAIN_NETWORK=localhost
BLOCKCHAIN_RPC_URL=http://localhost:8545
BLOCKCHAIN_EXPLORER_URL=http://localhost:8545

# Contract Addresses (after deployment)
CONTRACT_RAHAT_TOKEN=0x...
CONTRACT_RAHAT_REGISTRY=0x...
CONTRACT_RAHAT_CLAIM=0x...

# Admin Wallet
ADMIN_WALLET_PRIVATE_KEY=0x...

# SMS Service
SMS_SERVICE_ENABLED=false
SMS_SERVICE_URL=http://localhost:3002
SMS_API_KEY=your_sms_api_key

# File Storage
FILE_STORAGE_TYPE=local # or 's3', 'ipfs'
FILE_STORAGE_PATH=./uploads

Web Application (packages/app/.env)

# Server Configuration
NEXT_PUBLIC_API_URL=http://localhost:3001/api
NEXT_PUBLIC_CHAIN_ID=1337

# Authentication
NEXT_PUBLIC_JWT_NAME=rahat_token

# Feature Flags
NEXT_PUBLIC_FEATURE_SMS=false
NEXT_PUBLIC_FEATURE_ROLES=true
NEXT_PUBLIC_FEATURE_CLAIMS=true

# UI Configuration
NEXT_PUBLIC_APP_NAME=Rahat
NEXT_PUBLIC_DEFAULT_LOCALE=en
NEXT_PUBLIC_DEFAULT_THEME=light

# Contract Configuration
NEXT_PUBLIC_BLOCK_EXPLORER_URL=http://localhost:8545
NEXT_PUBLIC_RPC_URL=http://localhost:8545

Smart Contracts (packages/contracts/.env)

# Network Configuration
NETWORK_NAME=localhost
RPC_URL=http://localhost:8545
CHAIN_ID=1337

# Deployment
DEPLOYER_PRIVATE_KEY=0x...
ETHERSCAN_API_KEY=

# Gas Configuration
GAS_PRICE=auto
GAS_LIMIT=auto

# Contract Configuration
INITIAL_SUPPLY=1000000
TOKEN_NAME=Rahat Token
TOKEN_SYMBOL=RHT

Environment Variable Management

Development

For development environments, you can:

  1. Copy the example environment files: cp .env.example .env
  2. Update values in the .env file as needed
  3. Use pnpm run dev which will automatically load environment variables

Production

For production environments:

  1. Store sensitive environment variables in a secure environment variable management system
  2. For containerized deployments, inject environment variables at runtime
  3. Never commit production environment variables to the repository

Environment Variable Precedence

Environment variables are loaded in the following order (later sources override earlier ones):

  1. Default values hardcoded in the application
  2. .env files
  3. Environment variables set in the shell or deployment environment

Required vs Optional Variables

Variables marked as required must be set for the application to function properly. Optional variables have default values or can be omitted.

Testing Environment Variables

For test environments, create .env.test files with mock values suitable for testing scenarios.

NODE_ENV=test
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/rahat_test_db
JWT_SECRET=test_secret

Troubleshooting

If you encounter issues related to environment variables:

  1. Ensure all required variables are set
  2. Check for typos in variable names
  3. Verify that environment files are properly loaded
  4. Check that variable values are properly formatted (no trailing spaces, quotes, etc.)