Prerequisites
Before you begin, ensure you have:- Python 3.9 or higher installed on your system
- pip package manager
- Git for cloning the repository
- A Firebase project (we’ll set this up in the steps below)
New to Python? Download it from python.org
Setup Steps
Clone the Repository
Start by cloning the Water Quality Backend repository to your local machine:Verify you’re in the correct directory:You should see files like
main.py, requirements.txt, and the app/ directory.Create a Virtual Environment
Create an isolated Python environment to avoid dependency conflicts:Activate the virtual environment:
You’ll know the virtual environment is active when you see
(.venv) at the beginning of your terminal prompt.Install Dependencies
Install all required Python packages using pip:This will install core dependencies including:
- FastAPI - Web framework
- Firebase Admin SDK - Firebase integration
- Pandas, NumPy, Scikit-learn - Data science and ML
- Socket.IO - Real-time communication
- PyJWT - JWT token handling
Installation may take 2-3 minutes depending on your internet connection.
Configure Environment Variables
Copy the example environment file to create your own configuration:Open Copy the output and paste it as your
.env in your favorite text editor and configure the following required variables:.env
Generate a Secret Key
Create a secure secret key for JWT token signing:SECRET_KEY in the .env file.Optional Services
For full functionality, you can also configure:.env
The API will work with just Firebase and SECRET_KEY configured. Optional services can be added later as needed.
Start the Development Server
Launch the FastAPI development server:You should see output similar to:
The
fastapi dev command enables auto-reload, so the server will automatically restart when you make code changes.Verify the Server is Running
Open your browser and navigate to:- API Root: http://localhost:8000
- Interactive Docs: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Make Your First API Call
Now let’s register a new user using the authentication endpoint.Expected Response:Expected Response:
Register a New User
Open a new terminal (keep the server running) and execute:The password must meet security requirements: at least 8 characters, including uppercase, lowercase, numbers, and special characters.
Login with Your User
Now authenticate and receive a JWT token:Use the Token for Authenticated Requests
Copy thetoken from the response and use it in subsequent requests:The JWT token is valid for 30 days and includes user information like
uid, email, username, and rol (role).Alternative: Using the Interactive Docs
FastAPI provides built-in interactive API documentation at http://localhost:8000/docs.Try the API in Your Browser
- Navigate to http://localhost:8000/docs
- Find the POST /auth/register/ endpoint
- Click “Try it out”
- Enter the request body:
- Click “Execute”
- View the response below
The interactive docs are perfect for exploring the API without needing curl or Postman!
What’s Next?
Congratulations! You’ve successfully:- ✅ Set up the Water Quality Backend API
- ✅ Started the development server
- ✅ Registered and authenticated a user
- ✅ Received your first JWT token
Continue Learning
Installation Guide
Learn about production deployment with Docker and advanced configuration
Authentication
Explore all authentication methods including OAuth and password reset
Meter Management
Create and manage water quality meters with real-time data
API Reference
Complete reference for all API endpoints and data models
Troubleshooting
Server Won’t Start
Problem:ModuleNotFoundError or import errors
Solution: Make sure your virtual environment is activated and dependencies are installed:
Firebase Connection Issues
Problem: Firebase authentication or database errors Solution: Verify your Firebase credentials in.env:
- Check that
FIREBASE_PRIVATE_KEYincludes the full key with\nfor newlines - Ensure
FIREBASE_PROJECT_IDmatches your Firebase console - Verify the Realtime Database URL is correct
Registration Fails
Problem: User registration returns validation errors Solution: Check password requirements:- Minimum 8 characters
- At least one uppercase letter
- At least one lowercase letter
- At least one number
- At least one special character
Need Help?
- Interactive Docs: http://localhost:8000/docs
- Postman Collection: Check the
.postman/directory for pre-configured requests - Example Tests: See
/testsdirectory for working code examples