This is a simple template for FastAPI projects. It contains the bare minimum needed to get started developing a modern async Python web API.
- Simple, extensible API structure with FastAPI
- Fully asynchronous web server deployed with Uvicorn
- Multi-stage Dockerfile for efficient image creation.
- Dependency management and tool config with Poetry.
- Pre-commit hooks for enforcing standards.
- Ruff for code style and formatting.
- MyPy for static type checking.
- Example sentry integration with FastAPI hooks.
- Example database integration
- Example auth with JWT
- Example integration testing with testcontainers
Make sure you have installed the following:
- Docker
- Poetry
- Pre-commit
- Clone this repository:
- Build the Docker image:
docker build -t my-fastapi-app .
- Run the Docker container:
docker run -p 8080:8080 my-fastapi-app
- Clone this repository.
- Install pre-commit hooks:
pre-commit install
- Install dependencies:
poetry install
- Run the tests:
poetry run pytest tests/
- Add new dependencies with Poetry:
poetry add <package>
- Run the server locally:
poetry run uvicorn src.main:app --reload
The project is structured as follows:
├── Dockerfile
├── README.md
├── docs
├── mypy.ini
├── poetry.lock
├── pyproject.toml
├── src
│ ├── __init__.py
│ ├── api
│ │ ├── __init__.py
│ │ └── v1
│ │ ├── __init__.py
│ │ ├── api.py
│ │ ├── models.py
│ │ └── routes
│ │ ├── __init__.py
│ │ └── health.py
│ ├── core
│ │ ├── __init__.py
│ │ ├── auth.py
│ │ ├── config.py
│ │ ├── logging.py
│ │ └── middleware.py
│ └── main.py
└── tests
└── test_v1_route_health.py
To add a new v1 route/endpoint, create a new module in src/api/v1/routes
and simply import it in src/api/v1/api.py
.
To add the v2 version of the route, create a new module in src/api/v2/routes
, import it in src/api/v2/api.py
, and add a new versioned route in src/api/api.py
.