What FastAPI is and how it works
FastAPI is a Python web framework for HTTP APIs. Type annotations on function parameters and Pydantic models define input validation, conversion of the response into JSON and the OpenAPI schema. From this schema, FastAPI generates the interactive documentation automatically.
FastAPI is a good fit for JSON APIs: the backend of web and mobile applications, microservices and internal services. It does not impose a database, an ORM or an authentication method — these are added as separate libraries through the dependency system. If you need a website with a built-in ORM, templates and an admin panel, see the comparison in the Django reference, section “What Django is and how it works”.
A request travels through the application along a pipeline. The ASGI server Uvicorn accepts the connection and passes the request to the application, and middleware process each request before the operation and each response after it. The path and method select the path operation. Its parameters and body are converted to the declared types and validated by Pydantic; then the dependencies and the function itself are called. The result passes through the response model: extra fields are cut off and the rest goes to the client as JSON. If the input fails validation, the function is not called and the client receives a 422 response.
How FastAPI differs from Starlette and Pydantic
FastAPI is assembled from ready-made parts, and the documentation and error messages mention all of them by name. The FastAPI class inherits from the Starlette class, so everything Starlette can do also works in FastAPI.
| Component | What it handles |
|---|---|
| Starlette | ASGI framework: routing, middleware, WebSockets, background tasks, startup and shutdown events, test client |
| Pydantic | validation and serialization of data from type annotations, JSON Schema of the models |
| Uvicorn | ASGI server: accepts connections and runs the application; it has no routing |
| FastAPI | on top of Starlette and Pydantic: parameters and body from declared types, dependency injection, security utilities, the OpenAPI schema and documentation |
Code written for Starlette — middleware, responses, other tools — works in a FastAPI application without changes, and Pydantic models and libraries connect as they are.
What you need to know first
- Python: type annotations, functions,
asyncandawait— see the Python reference, sections “Functions, arguments and scopes” and “asyncio, threads and processes”. - Virtual environments and packages — see the Python reference, section “Installation and first run”.
- HTTP methods, response codes and JSON — see the HTTP and REST reference.
Section sources
the section is written from these pages- Featuresfastapi.tiangolo.com/features/(opens in a new tab)
- Alternatives, Inspiration and Comparisonsfastapi.tiangolo.com/alternatives/(opens in a new tab)
- Middlewarefastapi.tiangolo.com/tutorial/middleware/(opens in a new tab)
- Response Model - Return Typefastapi.tiangolo.com/tutorial/response-model/(opens in a new tab)
- Query Parametersfastapi.tiangolo.com/tutorial/query-params/(opens in a new tab)