Prerequisites
Before installing the Water Quality Backend API, ensure you have the following:System Requirements
- Python 3.9 or higher (Python 3.12 recommended)
- pip 21.0+ for package management
- Git for version control
- Virtual environment support (built into Python 3.3+)
Optional Tools
- Docker (for containerized deployment)
- Docker Compose (for multi-container orchestration)
- Postman or curl (for API testing)
External Services
You’ll need accounts and credentials for:-
Firebase (Required)
- Firebase Realtime Database
- Firebase Admin SDK credentials
- Get started at firebase.google.com
-
Weather API (Optional)
- For weather data integration
-
OneSignal (Optional)
- For push notifications
- Sign up at onesignal.com
-
Resend (Optional)
- For email notifications
- Sign up at resend.com
-
OpenRouter (Optional)
- For AI-powered analysis
- Sign up at openrouter.ai
-
GitHub OAuth (Optional)
- For social authentication
- Create an OAuth App at github.com/settings/developers
Only Firebase credentials are required to get started. Other services can be configured later based on your needs.
Local Installation
Clone the Repository
Start by cloning the project from your Git repository:Verify the repository structure:You should see:
app/- Application source codemain.py- Application entry pointrequirements.txt- Python dependencies.env.example- Environment variable templateDockerfile- Docker configuration
Create Virtual Environment
Create an isolated Python environment:Or using Python 3 explicitly:Activate the virtual environment:Verify activation:The output should point to the Python binary inside
.venv/.Install Dependencies
Upgrade pip to the latest version:Install all required packages:This installs:Core Framework
fastapi[all]>=0.100.0- Web framework with all extrasuvicorn- ASGI server (included with FastAPI)python-dotenv- Environment variable management
firebase-admin- Firebase Admin SDKPyJWT- JSON Web Token handlingpasslib[bcrypt]- Password hashing
pandas- Data manipulationnumpy- Numerical computingscikit-learn- Machine learningpydantic-ai-slim[openai]- AI model integrationmatplotlib>=3.8.0- Data visualizationfpdf2- PDF report generation
python-socketio[asyncio]- WebSocket supportonesignal-python-api- Push notificationsresend- Email delivery
prometheus-client==0.23.1- Metrics and monitoring
pytest>=7.0.0- Testing frameworkpytest-asyncio>=0.21.0- Async test supportpytest-cov>=4.0.0- Code coveragehttpx>=0.24.0- HTTP client for tests
Installation may take several minutes due to the large number of dependencies, especially data science packages.
Install Development Dependencies (Optional)
For running tests and development tools:Configure Environment Variables
Set Up Firebase
Create a Firebase Project
- Go to Firebase Console
- Click “Add project” and follow the wizard
- Enable Realtime Database:
- Navigate to Build > Realtime Database
- Click “Create Database”
- Choose your location
- Start in test mode for development (update rules for production)
Get Firebase Admin SDK Credentials
- In Firebase Console, go to Project Settings (gear icon)
- Navigate to Service accounts tab
- Click Generate new private key
- Download the JSON file
- Extract the values and add them to your
.envfile:
When copying
private_key to .env, ensure newlines are escaped as \n.Get Firebase Web API Key
- In Project Settings, go to General tab
- Scroll to Your apps section
- If no web app exists, click “Add app” and select Web
- Copy the
apiKeyvalue toFIREBASE_API_KEYin.env
Get Realtime Database URL
- Navigate to Realtime Database in Firebase Console
- Copy the database URL (e.g.,
https://your-project.firebaseio.com) - Add it to
FIREBASE_REALTIME_URLin.env
Run the Development Server
Start the FastAPI development server:The server will start on Or run Uvicorn directly with production settings:
http://localhost:8000 with:- Auto-reload enabled (restarts on code changes)
- Debug mode active
- Interactive docs at
/docs
Production Server
For production, use the production entry point:Verify Installation
Test the API to ensure everything is working:Expected response:Expected response:You should see Prometheus metrics output.
1. Check API Root
2. Access Interactive Docs
Open http://localhost:8000/docs in your browser.You should see the Swagger UI with all available endpoints.3. Test Registration Endpoint
4. Check Metrics Endpoint
If all tests pass, your installation is complete and the API is ready to use!
Docker Installation
Deploy the API in a containerized environment for consistency and portability.Using Docker
Build the Docker Image
Build the container image using the provided Dockerfile:The Dockerfile:
- Uses Python 3.12 slim base image
- Installs system dependencies (gcc, build tools)
- Copies and installs Python dependencies
- Creates a non-root user for security
- Exposes port 8000
The build process may take 5-10 minutes on the first run due to downloading and compiling dependencies.
Using Docker Compose
For more complex deployments with multiple services:Docker deployment is recommended for production environments as it ensures consistent runtime across different systems.
Testing the Installation
Run the comprehensive test suite to verify everything works:The test suite includes unit tests, integration tests, and API endpoint tests across all features.
Troubleshooting
Python Version Issues
Problem:python: command not found or wrong Python version
Solution:
Dependency Installation Failures
Problem: Error installingpandas, numpy, or scikit-learn
Solution: Install system build tools:
Firebase Connection Errors
Problem:Firebase app initialization failed
Solution:
- Verify all Firebase credentials in
.env - Check
FIREBASE_PRIVATE_KEYformatting (must have\nfor newlines) - Ensure Firebase Realtime Database is created and accessible
- Verify service account has proper permissions
Port Already in Use
Problem:Address already in use: port 8000
Solution:
Import Errors
Problem:ModuleNotFoundError when starting the server
Solution:
- Ensure virtual environment is activated
- Reinstall dependencies:
pip install -r requirements.txt - Check you’re in the correct directory (project root)
Next Steps
Now that installation is complete:Quickstart Guide
Make your first API call and authenticate a user
Authentication
Learn about JWT tokens, OAuth, and password management
API Reference
Explore all available endpoints and data models
Deployment Guide
Deploy to production with best practices
Production Considerations
Before deploying to production:-
Environment Variables
- Use production Firebase credentials
- Generate new
SECRET_KEYandSTATE_SECRET - Update
FRONTEND_ORIGINto production URL
-
Security
- Update Firebase Realtime Database rules
- Enable HTTPS/TLS
- Configure CORS properly
- Use environment-specific secrets
-
Performance
- Run with multiple Uvicorn workers
- Set up reverse proxy (Nginx)
- Enable caching where appropriate
- Monitor with Prometheus metrics
-
Monitoring
- Set up log aggregation
- Configure alerting (Prometheus/AlertManager)
- Monitor
/metricsendpoint
For detailed production deployment instructions, see the Deployment Guide.