Skip to content

FastAPI

A modern, high-performance Python web framework built on type hints.

Backend Framework

FastAPI builds request validation, serialization, and interactive API documentation directly from Python type hints and Pydantic models. It's async-native (built on Starlette) and consistently benchmarks among the fastest Python web frameworks, which — combined with being Python-native — makes it a common choice for APIs that also need to touch the Python ML/AI ecosystem.

When to use it

  • You're building an API that needs to call into Python ML/AI libraries directly
  • You want automatic, always-up-to-date API documentation (OpenAPI/Swagger) generated from your code
  • You want request/response validation with minimal boilerplate — types and Pydantic models do most of the work
  • You need async I/O performance closer to Node.js than traditional synchronous Python frameworks provide

Considerations

  • Newer and smaller ecosystem than Django — fewer batteries-included features like an admin panel or built-in ORM
  • No official ORM — you'll typically pair it with SQLAlchemy, Tortoise ORM, or similar
  • Async patterns throughout the codebase require the whole call chain to be async-aware (mixing sync and async carelessly can hurt performance)

Key concepts

Pydantic models

Python classes that define request/response shapes; FastAPI validates incoming data against them automatically.

Dependency injection

The Depends() system for sharing logic (auth checks, DB sessions) across route handlers declaratively.

Automatic docs

FastAPI generates interactive Swagger UI and ReDoc documentation from your route definitions with no extra work.

Path/query/body params

Function parameters and type hints determine whether a value comes from the URL path, query string, or request body.

Getting started

pip install fastapi uvicorn[standard]

Install FastAPI and an ASGI server to run it.

uvicorn main:app --reload

Run the dev server with auto-reload, assuming a FastAPI app instance named app in main.py.

Ecosystem

Pydantic (validation)SQLAlchemy (ORM)Uvicorn / Gunicorn (server)Alembic (migrations)

This guide states trade-offs, not a single "best" answer — the right choice depends on your team, constraints, and existing stack.