A modern, production-ready FastAPI boilerplate with built-in authentication, user management, and notification features. This project follows best practices for API development and includes essential tools and configurations out of the box.
- FastAPI - Modern, fast (high-performance), web framework for building APIs
- SQLAlchemy 2.0 - Full async SQL toolkit and ORM
- Alembic - Database migrations
- JWT Authentication - Secure token-based authentication
- Pydantic v2 - Data validation and settings management
- Dependency Injection - Clean architecture with dependency injection
- WebSockets - Real-time bidirectional communication for notifications and live updates
- RabbitMQ Integration - Message queuing for background tasks and email notifications
- Async MySQL - Async database support with aiomysql
- Environment Configuration - Easy environment variable management
- CORS - Built-in CORS middleware
- Structured Logging - Ready for production logging with Loki support
- Type Hints - Full Python type support
- Email Service - Built-in email service for user notifications with queue support
- Gunicorn with uvloop - High-performance server with ultra-fast event loop implementation
- Python 3.13
- MySQL 8.0
- RabbitMQ 3.x - Message broker for background tasks
- uv - Fast Python package installer and resolver
-
Clone the repository:
git clone [email protected]:{your_username}/python-fast-api.git cd python-fast-api
-
Create and activate a virtual environment using uv:
uv venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
-
Install dependencies with uv:
uv pip install -e . -
Set up environment variables:
cp .env.example .env # Edit .env with your configuration -
Initialize the database:
alembic upgrade head
Start the development server:
# Development mode with auto-reload
python server.py
# Production mode with Gunicorn and uvloop
gunicorn -c gunicorn.conf.py server:appThe API will be available at http://localhost:8000
API documentation:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
The application uses RabbitMQ for background task processing. To process email notifications in the background:
# Start the email worker
python src/cmd/email_worker.pyThis worker connects to RabbitMQ and processes email tasks from the queue, allowing the main application to continue processing requests without waiting for emails to be sent.
src/
βββ app/ # Application components
β βββ auth/ # Authentication module
β βββ user/ # User management
β βββ user_notification/ # Notification system
β βββ ws/ # WebSockets implementation
βββ cmd/ # Command-line tools
β βββ worker/ # Background workers
β βββ email/ # Email processing workers
βββ core/ # Core functionality
β βββ di/ # Dependency injection
β βββ exception/ # Custom exceptions
β βββ http/ # HTTP-related code
β βββ log/ # Logging configuration
β βββ rabbit_mq/ # RabbitMQ integration
β βββ service/ # Core services
β βββ settings/ # Application settings
β βββ web_socket/ # WebSockets core functionality
βββ database/ # Database configuration
POST /auth/signup- Register a new userPOST /auth/login- User loginPOST /auth/refresh- Refresh access tokenGET /auth/confirm-email- Confirm email addressPOST /auth/re-send-confirm-email- Resend confirmation email
GET /users- List usersPOST /users- Create a userGET /users/{user_id}- Get user by ID
GET /user-notifications- List user notificationsPOST /user-notifications- Create a notification
WebSocket /ws/{user_id}- Real-time connection for user-specific notifications and updates
Required environment variables are defined in .env.example. Copy this to .env and update the values:
# Application
ENVIRONMENT=dev
LOG_REQUEST=True
LOKI_URL="http://127.0.0.1:3100"
LOKI_ENABLED=False
APP_NAME="App"
SERVICE_NAME="api"
X_API_KEY=
# Database
SQLALCHEMY_DATABASE_URI=mysqlaiomysql://user:password@host/db_name
# JWT
JWT_PUBLIC_KEY="" # base64
JWT_PRIVATE_KEY="" # base64
JWT_ALGORITHM="ES512"
JWT_ACCESS_EXPIRATION_HOURS=8
JWT_REFRESH_EXPIRATION_HOURS=24
JWT_CONFIRM_TOKEN_EXP_HOURS=24
# Email
SMTP_SERVER="smtp.gmail.com"
SMTP_PORT=587
APP_PASSWORD="YOUR_PASSWORD"
FROM_EMAIL="YOUR_EMAIL"
APP_URL="http://localhost:8000"
# RabbitMQ
RABBITMQ_URL="amqp://guest:guest@localhost/"
Run tests with pytest:
pytestThis project enforces strict code quality standards using:
- Ruff - An extremely fast Python linter and code formatter
- Mypy - Static type checking
- Black - Code formatting (via Ruff)
- isort - Import sorting (via Ruff)
Run Ruff linter:
ruff check .Run Ruff formatter:
ruff format .Run Mypy type checking:
mypy .Create a new migration:
alembic revision --autogenerate -m "description of changes"Apply migrations:
alembic upgrade headThis project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.