Overview
The Water Quality Backend API requires several environment variables for proper configuration. This guide documents all variables, their purpose, and how to configure them.Getting Started
Copy the example environment file and customize it:.env with your actual credentials and configuration.
Environment File Structure
The.env.example file provides a template:
Core Configuration
Authentication & Security
Secret key used to sign JWT tokens with HS256 algorithm.How to generate:Example:
Secret used to HMAC-sign OAuth state parameters to prevent CSRF attacks.How to generate:Example:Required for: GitHub OAuth flows
Firebase Configuration
The API uses Firebase for authentication and real-time database.Primary Method (Recommended)
Complete Firebase Admin SDK credentials as a JSON string.How to get:
- Go to Firebase Console → Project Settings → Service Accounts
- Click “Generate New Private Key”
- Download the JSON file
- Copy the entire JSON content as a string
Ensure the JSON is properly escaped if it contains special characters.
Firebase Web API key for authentication requests.How to get: Firebase Console → Project Settings → General → Web API KeyExample:Used for: Password authentication via Firebase Identity Toolkit
Firebase Realtime Database URL.Format: How to get: Firebase Console → Realtime Database → Data tab (URL is shown at top)
https://your-project-id.firebaseio.comExample:Alternative Method (Individual Fields)
Instead ofFIREBASE_ADMIN_CREDENTIALS, you can provide individual fields:
Always
service_account for server-side applications.Default: service_accountYour Firebase project ID.Example:
water-quality-prodPrivate key ID from Firebase service account.
RSA private key (including
-----BEGIN PRIVATE KEY----- headers).Note: Newlines should be preserved or escaped as \nService account email address.Format:
[email protected]Service account client ID (numeric).
OAuth2 authorization URI.Default:
https://accounts.google.com/o/oauth2/authOAuth2 token URI.Default:
https://oauth2.googleapis.com/tokenX.509 certificate URL for auth provider.Default:
https://www.googleapis.com/oauth2/v1/certsX.509 certificate URL for service account.Format:
https://www.googleapis.com/robot/v1/metadata/x509/[email protected]Universe domain for Firebase.Default:
googleapis.comGitHub OAuth Configuration
GitHub OAuth App client ID.How to get:
- Go to GitHub → Settings → Developer settings → OAuth Apps
- Create new OAuth App or use existing
- Copy the Client ID
GitHub OAuth App client secret.How to get: Same location as Client ID → Generate new client secretExample:
OAuth callback URL that GitHub redirects to after authorization.Format: Important: This must match the callback URL configured in your GitHub OAuth App settings.
https://your-api-domain.com/auth/github/callbackExample:Frontend application URL where users are redirected after OAuth success (web).Format: Used for: Web OAuth flow redirects
https://your-frontend-domain.com/oauth/callbackExample:Mobile app deep link scheme for OAuth success redirects.Default: Used for: Mobile OAuth flow redirects
aquaminds://login-successExample:External Services
Weather API
API key for weather data integration.Provider: Various (OpenWeatherMap, WeatherAPI, etc.)Example:Used for: Correlating water quality with weather conditions
Notifications
OneSignal application ID for push notifications.How to get: OneSignal Dashboard → Settings → Keys & IDsExample:
OneSignal REST API key.How to get: OneSignal Dashboard → Settings → Keys & IDs → REST API KeyExample:Used for: Sending alert notifications to mobile/web clients
Email Service
Resend API key for transactional emails.How to get: Resend Dashboard → API KeysExample:Used for: Password reset emails, alerts, notifications
AI/ML Configuration
OpenRouter API key for AI model access.How to get: OpenRouter Dashboard → API KeysExample:Used for: AI-powered water quality analysis and predictions
OpenRouter model identifier to use.Example:Popular models:
openai/gpt-4-turboanthropic/claude-3-opusmeta-llama/llama-3-70b-instruct
Optional Configuration
Skip Firebase initialization (useful for testing).Default: Use case: Unit tests that don’t need Firebase connectivity
falseExample:Configuration by Environment
Development
Staging
Production
Docker Configuration
When deploying with Docker, pass environment variables via:Docker Run
Docker Compose
Docker Secrets (Production)
For production, use Docker secrets:Validation
Verify your configuration is correct:Security Best Practices
-
Never commit
.envfiles to version control -
Use different secrets per environment
- Development secrets should differ from production
- Rotate secrets regularly (every 90 days recommended)
-
Restrict access to
.envfiles -
Use environment variable managers for production:
- AWS Secrets Manager
- HashiCorp Vault
- Azure Key Vault
- Google Cloud Secret Manager
- Validate required variables on startup: The application should fail fast if critical variables are missing
- Audit secret access: Log when secrets are accessed or modified
Troubleshooting
Firebase Initialization Failed
Firebase Initialization Failed
Error:
Failed to initialize FirebasePossible causes:- Invalid
FIREBASE_ADMIN_CREDENTIALSJSON - Incorrect
FIREBASE_REALTIME_URL - Network connectivity issues
- Firebase project permissions
- Validate JSON syntax:
python -c "import json; json.loads(open('.env').read().split('FIREBASE_ADMIN_CREDENTIALS=')[1].split('\n')[0])" - Verify Firebase project exists and is active
- Check service account has proper permissions
JWT Token Invalid
JWT Token Invalid
Error:
Token validation failedPossible causes:SECRET_KEYmismatch between token creation and validationSECRET_KEYchanged after tokens were issued
- Ensure
SECRET_KEYis consistent across all API instances - Re-authenticate to get new tokens
OAuth State Invalid
OAuth State Invalid
Error:
invalid state signaturePossible causes:STATE_SECRETnot configuredSTATE_SECRETchanged during OAuth flow- State parameter tampered with
- Set
STATE_SECRETin environment - Ensure
STATE_SECRETis consistent across requests
Next Steps
Authentication
Learn how authentication uses these variables
Deployment
Deploy the API with proper configuration