Prerequisites
Before you start, ensure you have the following installed:- Python 3.8 or higher
- MySQL database server
- pip (Python package installer)
- Git
Initial Setup
Create Virtual Environment
Create and activate a Python virtual environment to isolate project dependencies:
Always activate the virtual environment before installing packages or running the server. You’ll know it’s active when you see
(venv) in your terminal prompt.Install Dependencies
Install all required Python packages from This installs FastAPI, uvicorn, SQLAlchemy, and other dependencies including:
requirements.txt:- FastAPI for the web framework
- Uvicorn for the ASGI server
- SQLAlchemy for database ORM
- PyJWT for authentication
- OpenCV and YOLO for image detection
- APScheduler for scheduled tasks
Configure Environment Variables
Create a Update the
.env file in the project root by copying the example file:.env file with your configuration:Set Up the Database
Create a MySQL database for the project:Run Alembic migrations to create tables:
The database connection is configured in
app/db/session.py:17 using PyMySQL as the database driver. The connection includes pool management for better performance.Running the Development Server
Using Uvicorn with Auto-Reload
The recommended way to run the API during development is with uvicorn’s--reload flag:
app.main:app- Points to the FastAPI app instance inapp/main.py:31--reload- Automatically restarts the server when code changes are detected--host 0.0.0.0- Makes the server accessible from other devices on your network--port 8000- Runs the server on port 8000
The
--reload flag watches for file changes and restarts the server automatically. This is perfect for development but should NOT be used in production.Alternative: Running Without Reload
For testing production-like behavior:Expected Output
When the server starts successfully, you should see:app/main.py:175).
Interactive API Documentation
FastAPI automatically generates interactive API documentation that you can use to test endpoints during development.Swagger UI (/docs)
Access the Swagger UI at: http://localhost:8000/api/docs
The API has a root path of
/api configured in app/main.py:31, so all endpoints are prefixed with /api.- Interactive interface to test all API endpoints
- Automatic request/response schema validation
- Built-in authentication support (click “Authorize” to add Bearer token)
- “Try it out” buttons to execute requests directly from the browser
- First, call the
POST /api/auth/loginorPOST /api/auth/signupendpoint - Copy the
tokenfrom the response - Click the “Authorize” button at the top right
- Enter
Bearer <your-token>in the “Value” field - Click “Authorize” - now all requests will include the auth header
ReDoc (/redoc)
Access the ReDoc documentation at: http://localhost:8000/api/redoc
Features:
- Clean, three-panel design
- Better for browsing and reading API structure
- Searchable endpoint list
- Detailed schema documentation
Both
/docs and /redoc are public paths (see app/main.py:58-65) and don’t require authentication. All other endpoints require a valid Bearer token in the Authorization header.Development Workflow
Hot Reloading
When running with--reload, the server automatically restarts when you:
- Modify Python files (
.py) - Add or remove files in the project
- Change imported modules
app/ directory and subdirectories
Common Development Tasks
Adding a New Endpoint:- Create or modify a router file in
app/routers/ - Define your endpoint function with appropriate decorators
- Import and include the router in
app/main.py(see lines 162-168) - Save the file - the server will auto-reload
- Check the
/docspage to see your new endpoint
- Update the model in
app/models/ - Generate a migration:
alembic revision --autogenerate -m "Description" - Review the generated migration in
alembic/versions/ - Apply the migration:
alembic upgrade head
app/main.py:173):
CORS Configuration
The API is configured with CORS middleware to allow requests from specific origins (app/main.py:34-48):
origins list in app/main.py:34.
Troubleshooting Development Issues
Port Already in Use
If port 8000 is already taken:Module Import Errors
If you see import errors after adding dependencies:Database Connection Issues
See the Troubleshooting guide for database-specific issues.Next Steps
Testing
Learn how to test your API using the interactive documentation
Troubleshooting
Common issues and their solutions