FastAPI

MIT
Platform
Linux Windows macOS
Licence
MIT Open source
FastAPI

Overview

FastAPI is a free, open-source Python framework for building HTTP APIs from ordinary type hints. Sebastián Ramírez released it in 2018 on top of two existing libraries — Starlette for the ASGI web layer and Pydantic for data validation — and the central idea is that the function signature is the contract: annotate a parameter and FastAPI validates the incoming request, serialises the response, documents the endpoint and reports the error, all from the same declaration. It speaks OpenAPI and JSON Schema natively, so every application is born with interactive documentation at /docs and a machine-readable specification at /openapi.json. FastAPI 0.162 is the current release series, it needs Python 3.10 or newer, and the code is MIT licensed.

Key Features

  • Generated documentation — an OpenAPI 3 schema is built from the same type hints the code already uses, rendering a Swagger UI console at /docs and a ReDoc reference at /redoc with no extra annotations.
  • Pydantic validation — request bodies, query parameters, headers and path segments are validated against typed models, with a structured 422 response naming every field that failed.
  • Dependency injection — Depends() composes shared logic (database sessions, current user, pagination, permission checks) declaratively, and the same dependencies are reused by the docs and by the tests.
  • Async and sync together — async def handlers run on the ASGI event loop while ordinary def handlers are offloaded to a thread pool, so blocking libraries do not stall the server.
  • Security utilities — OAuth2 password and bearer flows, API keys and HTTP basic auth ship as reusable dependencies, with the token flow wired into the docs console.
  • Response models — response_model filters and validates what leaves the API, so a field added to an internal object does not leak into a public payload.
  • Starlette underneath — WebSockets, background tasks, streaming responses, CORS, static files and the ASGI middleware ecosystem are all available directly.
  • CLI — fastapi dev runs the app with reload, and fastapi run serves it for deployment, without a separate server command.

Why Use FastAPI?

The framework’s argument is that correctness and documentation should come from the code rather than from a parallel spec that drifts. Because the schema is derived from the annotations, a validation error, the generated docs and the editor’s autocompletion all describe the same contract; changing a field updates all three at once. That makes FastAPI particularly effective where an API is consumed by people outside the team — a public endpoint, an internal platform service or a model-serving endpoint — and where the request and response shapes are complex enough that hand-written validation would be the bulk of the code. The trade is the dependency graph: FastAPI is a thin layer over Starlette and Pydantic, so debugging a validation or routing problem usually means reading those libraries’ behaviour, and the async model rewards a working knowledge of the event loop. The documentation and tutorial are among the most complete in the Python ecosystem, and the project ships type stubs so editors complete endpoint code as it is written.

Use Cases

  • REST and JSON APIs that need a live, always-current specification for their consumers.
  • Machine-learning model serving, where an inference function becomes a documented endpoint in a few lines.
  • Microservices and internal platform APIs that exchange typed payloads between teams.
  • Back ends for single-page and mobile applications, generated alongside the client’s expectations.
  • Data-validation front ends for pipelines and ETL jobs, reusing Pydantic models on both sides.
  • Replacing a hand-written Flask or Django view set with typed validation and generated docs.

Getting Started

FastAPI installs from PyPI with pip install fastapi plus an ASGI server such as uvicorn, ideally inside a virtual environment. A minimal application creates the app, decorates a function with @app.get, and returns a dict or a Pydantic model; fastapi dev main.py then serves it at 127.0.0.1:8000 with reload, and the documentation appears at /docs as soon as the first route exists. From there the official tutorial covers path and query parameters, request bodies, dependencies, security, database integration and testing with TestClient.

Related reading in this directory: Python, the language FastAPI is written in, and Flask, the lightweight WSGI alternative when async and generated specs are not needed.

Explore More Categories

Sign In

Register

Reset Password

Please enter your username or email address, you will receive a link to create a new password via email.