π§π· PortuguΓͺs | π«π· FranΓ§ais | πͺπΈ EspaΓ±ol
A modern, production-ready FastAPI backend template with best practices, Docker support, and comprehensive tooling for rapid development.
This project was created using the excellent cookiecutter-fastapi-backend template by @nickatnight, which provides a solid foundation for FastAPI applications with best practices and modern tooling.
- Architecture - System architecture overview
- Development Guide - Local development guidelines
- Contributing Guide - How to contribute to this project
- Troubleshooting - Common issues and solutions
- FastAPI - Modern, fast web framework for building APIs
- Docker - Containerized development and deployment
- PostgreSQL - Robust relational database with async support
- Redis - Caching and session management
- Celery - Background task processing
- Alembic - Database migration management
- Testing - Comprehensive test suite with pytest
- Code Quality - Pre-commit hooks, linting, and formatting
- Type Safety - Full type hints with mypy validation
- Documentation - Auto-generated API docs with Swagger UI
- Docker and Docker Compose
- Python 3.13+ (for local development)
- uv package manager
-
Clone the repository:
git clone https://github.com/GabrielVGS/fastapi-base.git cd fastapi-base -
Copy environment variables:
cp .env.example .env
-
Start the services:
make up
-
Access the application:
- API:
http://localhost:8666/v1/ - Health check:
http://localhost:8666/v1/ping - Interactive docs:
http://localhost:8666/docs - Alternative docs:
http://localhost:8666/redoc
- API:
-
Install uv (if not already installed):
curl -LsSf https://astral.sh/uv/install.sh | sh -
Navigate to the project directory:
cd fastapi-base/ -
Install dependencies:
uv sync
-
Install pre-commit hooks:
make hooks
Initialize the first migration (project must be running with docker compose up and contain no 'version' files):
make alembic-initCreate new migration file:
make alembic-make-migrations "describe your changes"Apply migrations:
make alembic-migrateAfter each migration, you can create new migrations and apply them with:
make alembic-make-migrations "describe your changes"
make alembic-migrateCreate a .env file based on .env.example:
PROJECT_NAME- Name of the project (default: fastapi-base)VERSION- API version (default: v1)DEBUG- Enable debug mode (default: True)SECRET_KEY- Secret key for JWT and encryptionENV- Environment (dev/staging/production)
POSTGRES_USER- PostgreSQL usernamePOSTGRES_PASSWORD- PostgreSQL passwordPOSTGRES_DB- Database namePOSTGRES_HOST- Database host (default: localhost)POSTGRES_PORT- Database port (default: 5432)POSTGRES_URL- Complete database URL (optional, auto-generated if not provided)
REDIS_HOST- Redis host (default: redis)REDIS_PORT- Redis port (default: 6379)REDIS_URL- Complete Redis URL (optional, auto-generated if not provided)
SENTRY_DSN- Sentry error tracking DSNLOG_LEVEL- Logging level (default: INFO)CACHE_TTL- Cache time-to-live in seconds (default: 60)
# Build and start all services
make build
# Start services (without building)
make up
# Stop services
make down
# Access container bash
make bash# Ensure PostgreSQL and Redis are running locally
# Update .env with local database/redis connections
# Run the FastAPI application
uv run uvicorn src.main:app --reload --host 0.0.0.0 --port 8000Check the Makefile to view all available commands.
# Development
make up # Start all services with Docker Compose
make down # Stop all services
make build # Build and start services
make bash # Access the main container shell
# Database
make alembic-init # Initialize first migration
make alembic-make-migrations # Create new migration
make alembic-migrate # Apply migrations
make alembic-reset # Reset database
make init-db # Initialize database with sample data
# Code Quality
make test # Run test suite with coverage
make lint # Run ruff linter
make black # Format code with black
make isort # Sort imports
make mypy # Type checking
make precommit-run # Run all pre-commit hooks
# Maintenance
make hooks # Install pre-commit hooksBy default, dependencies are managed with uv. Please visit the link and install it.
From ./fastapi-base/ you can install all dependencies with:
uv syncThe project uses pre-commit hooks to ensure code quality. Install them with:
make hooksThis will install hooks that run automatically before each commit to:
- Format code with
black - Sort imports with
isort - Lint code with
ruff - Check types with
mypy - Run tests
Run the complete test suite:
make testRun specific tests:
# Inside the container
docker compose exec fastapi-base pytest tests/test_specific.py
# Locally (if dependencies installed)
uv run pytest tests/test_specific.pyWe welcome contributions! Please see our Contributing Guidelines for detailed information about:
- Setting up your development environment
- Code style and standards
- Testing requirements
- Pull request process
- Code of conduct
This project is licensed under the MIT License - see the LICENSE file for details.
- FastAPI - Modern, fast web framework
- SQLModel - SQL databases in Python, designed for simplicity
- Alembic - Database migration tool
- Celery - Distributed task queue
- Redis - In-memory data structure store
- PostgreSQL - Advanced open source database
- Docker - Containerization platform
- uv - Fast Python package manager
If you found this project useful, please consider:
- Giving it a star βοΈ on GitHub
- Sharing it with your colleagues
- Contributing to its development
- Reporting issues or suggesting improvements
Happy coding! π