This template provides a basic setup for FastAPI backend development. This template is influenced by https://github.com/fastapi/full-stack-fastapi-template.
- basic: Contains only the Python development environment setup (Poetry, linter, formatter, pre-commit) without any source code. If FastAPI, SQLModel, pyJWT, etc., are not needed, remove the unnecessary dependencies before starting development.
- main: A template for developing an API server using FastAPI. It is a simple version for API server.
- webdev: A template for developing an web API server using FastAPI with more functionalities.
This template follows a standard project structure for FastAPI backend development, as outlined below.
- FastAPI
- Pydantic
- SQLModel
- pyJWT
- black = "^24.4.2" (in
pyproject.toml) - pytest = "^8.2.2"
- pre-commit = "^3.7.1"
- ruff = "0.5.3" (in
pyproject.toml) - mypy = "^1.10.1" (in
pyproject.toml) - importlib-metadata = "4.13.0"
.
├── app
│ ├── api
│ │ ├── __init__.py
│ │ ├── dependencies.py
│ │ └── v1
│ │ ├── endpoints
│ │ │ ├── __init__.py
│ │ │ ├── login.py
│ │ │ └── users.py
│ │ └── router.py
│ ├── core
│ │ ├── __init__.py
│ │ ├── config.py
│ │ ├── db.py
│ │ └── security.py
│ ├── main.py
│ ├── models
│ │ ├── auth.py
│ │ └── user.py
│ ├── tests
│ │ └── api
│ └── utils
│ ├── auth_utils.py
│ └── email_utils.py
├── .env
└── pyproject.toml
10 directories, 16 files- Clone the repository.
- Update
pyproject.tomlwith your project details. - Run
poetry install. - Set up pre-commit hooks with
pre-commit install. - Activate the virtual environment with
poetry shell.
- Create the root project directory.
- Initialize the project with
poetry init:- Set the project name, Python version, and install basic modules.
- If the project won't be a package, set
package-mode = false.
- Add dependencies as needed:
For dev dependencies (like linters, formatters, and testing tools), use the
poetry add {module} poetry install--devflag:poetry add --dev flake8
To install dev dependencies, use the --dev flag. This allows separating dev dependencies during installation. For production, it's recommended to freeze versions using the --prod flag. Write specific configurations in the .pyproject.toml.
To install without dev dependencies:
poetry install --without dev,docs- Create a
.pre-commit-config.yamlfile and define hooks. - Pre-commit hooks help check files before committing (e.g., checking for large files, formatting issues, etc.).
Example .pre-commit-config.yaml:
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-added-large-files
- id: check-toml
- id: check-yaml
args:
- --unsafe
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.215
hooks:
- id: ruff
args: ["--config=pyproject.toml"]
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.7.0
hooks:
- id: python-check-blanket-noqa
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.10.1
hooks:
- id: mypy
- repo: https://github.com/pre-commit/mirrors-pytest
rev: v6.2.5
hooks:
- id: pytestInstall the dependencies and pre-commit hooks:
poetry install
pre-commit install- Create the
appdirectory. - In
app/core, createconfig.pyand define the settings class.- Include configurations such as CORS, API version, secret keys, domain, environment variables, database details, and SMTP.
- Set up
secrets.pyfor handling sensitive data. - Configure
db.pyfor database connection information (exclude if no database is required yet).
Create main.py and run the server to ensure everything works:
uvicorn app.main:app --host 0.0.0.0 --port 8888Write a Dockerfile to containerize the application.
Add .dockerignore, .gitignore, and other necessary configuration files.
This structure and setup will help you get started quickly with FastAPI backend development.