An elegant, modular, and production-ready boilerplate for building Python backend services with FastAPI.
This Backend Template provides a solid foundation for building scalable, maintainable, and production-ready Python backend services. It is designed to help developers start new projects quickly without worrying about boilerplate setup. The template emphasizes modularity, clear project structure, and best practices, including async database operations, caching, middleware support, and structured API responses. With this template, you can focus on building your application logic while the foundation handles configuration, logging, database initialization, and API versioning. It is suitable for building RESTful APIs, microservices, or any backend service that requires reliability, scalability, and maintainability.
| π Language | π· Code | π Link |
|---|---|---|
| Russian | ru |
README.ru.md |
| Ukrainian | uk |
README.uk.md |
| Tatar | tt |
README.tt.md |
| Uzbek | uz |
README.uz.md |
| Kazakh | kk |
README.kk.md |
| English | en |
README.md |
- β‘ fastapi β modern, fast (high-performance) web framework for building APIs with Python.
- π₯ uvicorn β lightning-fast ASGI server for running FastAPI apps.
- π§© pydantic-settings β settings management using Pydantic.
- πΎ sqlalchemy + aiosqlite β async ORM and DB driver.
- π redis β a client for Redis, used for caching.
- π§± alembic β a database migration tool.
- β¨ ruff β an extremely fast Python linter and formatter.
- π uv β an extremely fast package manager and bundler for Python.
// Directory tree (3 levels, limited to 200 entries)
βββ .gitignore
βββ .python-version
βββ Dockerfile
βββ LICENSE
βββ README.md
βββ example.alembic.ini
βββ pyproject.toml
βββ ruff.toml
βββ tests\
βββ src\
β βββ .env.example
β βββ config.py
β βββ app.py
β βββ run.py
β βββ api\
β β βββ v1\
β β β βββ __init__.py
β β β βββ admin.py
β β β βββ user.py
β β βββ __init.py__
β β βββ common.py
β βββ database\
β β βββ __init__.py
β β βββ base.py
β β βββ models\
β β βββ repositories\
β βββ middlewares\
β β βββ rate_limit.py
β βββ schemas\
β β βββ __init__.py
β βββ services\
β β βββ redis_service.py
β βββ utils\
β βββ api_structure.py
β βββ command_runner.py
β βββ endpoints.py
β βββ exceptions.py
β βββ logger.py
β βββ migration_database.py
β βββ responses.py
βββ uv.lock
| π Folder | π§ Description |
|---|---|
src/api/ |
Contains API routes and versioned modules. |
src/api/v1/ |
API version 1 endpoints (admin, user, etc.). |
src/database/ |
Database initialization, ORM models, and repositories. |
src/database/models/ |
SQLAlchemy ORM models. |
src/database/repositories/ |
CRUD repositories for working with the database. |
src/middlewares/ |
Middleware for rate limiting and request processing. |
src/schemas/ |
Pydantic schemas for request/response validation. |
src/services/ |
Application services (e.g., Redis). |
src/utils/ |
Utilities: logging, exceptions, responses, etc. |
tests/ |
Unit and integration tests. |
Redis is used for caching and rate limiting.
For Windows:
- Download the
.msiinstaller from the Microsoft's repository: https://github.com/microsoftarchive/redis/releases - Run the
.msifile and follow the installer instructions.
For Linux:
- Arch Linux:
sudo pacman -S redis sudo systemctl start redis sudo systemctl enable redis - Debian/Ubuntu:
sudo apt update sudo apt install redis-server sudo systemctl enable redis-server sudo systemctl start redis-server - Fedora:
sudo dnf install redis sudo systemctl start redis sudo systemctl enable redis
For macOS:
brew install redis
brew services start redis-
Install
uv:curl -LsSf https://astral.sh/uv/install.sh | shOr for Windows:
irm https://astral.sh/uv/install.ps1 | iex
-
Clone the repository and navigate to the folder:
git clone https://github.com/Kitty-Ilnazik/backend-template.git cd backend-template -
Configure environment variables: Copy the example
.envfile and fill it with your data:cp src/.env.example src/.env
Open
src/.envand enter values forREDIS_URLandDB_URL. -
Install dependencies and run:
uv run start
-
Clone the repository and navigate to the folder:
git clone https://github.com/Kitty-Ilnazik/backend-template.git cd backend-template -
Configure environment variables: Copy the example
.envfile and fill it with your data:cp src/.env.example src/.env
Open
src/.envand enter values forREDIS_URLandDB_URL. -
Create and activate a virtual environment:
- Windows:
python -m venv .venv .venv/Scripts/activate
- macOS/Linux:
python3 -m venv .venv . .venv/bin/activate
- Windows:
-
Install dependencies:
pip install -r requirements.txt
-
Run the bot:
python src/run.py
-
Install Docker:
- Arch Linux:
sudo pacman -S docker sudo systemctl start docker sudo systemctl enable docker sudo usermod -aG docker $USER # Add user to docker group newgrp docker # Apply group changes
- Debian/Ubuntu:
sudo apt update sudo apt install docker.io sudo systemctl start docker sudo systemctl enable docker sudo usermod -aG docker $USER newgrp docker
- Fedora:
sudo dnf install docker sudo systemctl start docker sudo systemctl enable docker sudo usermod -aG docker $USER newgrp docker
- Windows/macOS: Install Docker Desktop from the official Docker website.
- Arch Linux:
-
Build and run the Docker image:
docker build -t backend-template .docker build: Command to build a Docker image.-t backend-template: Assigns the tag (name)backend-templateto the created image..: Indicates that the Dockerfile is in the current directory.
docker run -d --name my-backend --env-file src/.env backend-template
docker run: Command to run a Docker container.-d: Runs the container in detached mode.--name my-backend: Assigns the namemy-backendto the running container.--env-file src/.env: Instructs Docker to use environment variables fromsrc/.envinside the container.backend-template: The name of the Docker image to run.
-
Alembic Configuration: Copy the example Alembic configuration file:
cp example.alembic.ini alembic.ini
Open
alembic.iniand change the path to the.envfile in the[alembic]section tosrc/.env. -
Create and apply a migration:
uv run migrate commit "Initial migration"
Ruff is used for code formatting and error checking.
uv run ruff check .
uv run ruff format .