>_ DevTrendspt

Idioma

Início

Linguagens

Seções

Frontend Backend Mobile DevOps AI / ML GameDev Blockchain Embarcados Segurança
Python

How to Build a Working FastAPI Backend Skeleton in a Couple of Minutes

Every time I start a new FastAPI project, I'm stuck in a Groundhog Day. I need to set up the directory structure again, connect the database, write migrations, hook up environment variables, build the Dockerfile, and configure linters. It's easy to waste an entire evening on this routine before the first real endpoint even appears.

FastAPI Template Logo

Of course, you can copy the structure from your previous repository. But it usually has a bunch of old workarounds, unnecessary dependencies, and outdated libraries. Ready-made boilerplates from search results also rarely hit the mark, because the author chose TortoiseORM but you need SQLAlchemy 2.0, or they stuffed in heavy Celery instead of lightweight queues.

The FastAPI-template repository by developer s3rius solves this problem differently. It's not just a static template, but a flexible interactive project generator.

Constructor instead of a rigid template

The project works like a terminal questionnaire. You launch the utility via pip, uv, or Docker, answer a couple dozen questions, and get a ready-made codebase tailored to your needs.

The main advantage of the generator is variability. Most templates impose a specific stack, but here you assemble the needed set of components yourself.

Databases and ORM of your choice

Instead of being tied to a single library, the utility supports several popular data handling options:

  • SQLAlchemy 2.0 with async engine
  • TortoiseORM with migration generation via Aerich
  • Piccolo ORM
  • Ormar
  • Beanie for MongoDB
  • Raw psycopg driver without ORM

If you don't need a database at all, you can choose the no-data-storage option. In that case, the code won't have unnecessary dependencies and garbage configuration files.

REST or GraphQL

By default, the generator builds a classic REST API with automatic Swagger documentation. But if your frontend prefers working through GraphQL, the generator will set up the wrapper based on the Strawberry library. No need to manually configure the schema and types to get started.

Background tasks and queues

Instead of bulky Celery, the template author added integration with Taskiq — a modern async task manager for Python. It fits perfectly into the overall async architecture of FastAPI. As a message broker, you can choose Redis, RabbitMQ, or Kafka during generation.

Monitoring and logging out of the box

The template already has built-in ready integrations that you usually have to configure manually from separate articles:

  • Metrics collection for Prometheus
  • Tracing via OpenTelemetry
  • Error sending to Sentry
  • Structured logging via Loguru

All these options are toggled via simple flags or selection in the interactive menu.

Quick start

To run the utility, you need Git, Python, and the fast package manager uv installed on your machine.

The generator is installed via pip:

python3 -m pip install fastapi_template
fastapi_template

After launching, a step-by-step questionnaire will appear in the console. You select the project name, desired DBMS, authentication type (JWT or cookie via fastapi-users), CI/CD format (GitHub Actions or GitLab CI), and additional services.

When the generator finishes, go to the created folder and spin up the environment:

cd my_awesome_project
docker-compose up --build

If you don't want to install the package locally, you can run the generator via Docker in one line:

docker run --rm -it -v "$(pwd):/projects" ghcr.io/s3rius/fastapi_template

Generation via CLI flags

If you're setting up automated microservice creation in your company, you can disable interactive mode. The utility accepts all parameters via command-line arguments:

fastapi_template \
  --name billing_service \
  --api-type rest \
  --db postgresql \
  --orm sqlalchemy \
  --migrations \
  --redis \
  --taskiq \
  --prometheus \
  --ci github \
  --quiet

This call will create a ready service with configured SQLAlchemy, Alembic migrations, Taskiq background tasks, metrics, and a GitHub pipeline without a single question in the terminal.

What's inside the generated project

The generated code pleasantly surprises with its structure. There's no mess of everything crammed into one file main.py.

The architecture is divided into logical layers:

  • web/api: routers broken down by version prefixes and modules
  • db: database connection, models, and migration configuration
  • services: business logic and external service clients
  • settings.py: typed settings based on pydantic-settings

The project immediately includes pytest tests with about 90% coverage for the base structure, configured pre-commit with linters, and a ready docker-compose.yml with all selected services. You won't need to separately spin up Redis or PostgreSQL for local development.

Who will find this project useful

The generator is perfect for two scenarios.

The first case is quick launch of pet projects and MVPs. When you want to test a hypothesis over the weekend, there's no desire to spend half a day on boilerplate.

The second case is microservice standardization in a team. You can take this template as a base or use its CLI interface so that new services in the company are created according to a single standard with already configured telemetry and tests.

If you write in Python and often spin up new FastAPI backends, definitely bookmark this repository. It will save a lot of time and nerves at the start.

Projetos relacionados