FastAPI
- Pronunciation
- FAST ay-pee-EYE
In short
FastAPI is a modern Python API framework that uses standard type hints to validate requests, convert data and generate OpenAPI documentation automatically.
What is FastAPI?
FastAPI was created by Sebastián Ramírez and released in 2018. Its key idea is that the type hints you already write in Python describe your API. A path function declared with item_id: int and a body typed as a Pydantic model tells FastAPI what to expect, and it validates every incoming request against that, returning clear errors when the data doesn't fit.
The same types produce documentation. Every FastAPI app serves an OpenAPI schema and interactive docs at /docs, where you can read every endpoint and try it from the browser. Editors also understand the types, so autocompletion works throughout the code. Dependency injection with Depends handles shared pieces such as database sessions and authentication.
FastAPI is built on Starlette and the ASGI standard, so it supports async endpoints, WebSockets and background tasks, and it runs on servers such as Uvicorn. It has become one of the most popular Python web frameworks, especially for machine learning model APIs and backends that serve JSON to a separate front end.
A common misconception is that FastAPI is automatically faster than everything else. It is among the fastest Python frameworks thanks to async I/O, but blocking code inside an async function, such as a synchronous database driver, stalls the server; ordinary def endpoints run in a thread pool for exactly that reason.
Key takeaways
- FastAPI builds APIs in Python using standard type hints.
- Types drive request validation through Pydantic models.
- Interactive OpenAPI docs are generated automatically at /docs.
- It is async-first, built on Starlette and ASGI, and run with Uvicorn.
- Blocking calls inside async endpoints slow the whole server down.
Example
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
app = FastAPI()
class BookIn(BaseModel):
title: str = Field(min_length=1)
year: int = Field(ge=1450)
books: dict[int, BookIn] = {}
@app.post("/books", status_code=201)
def create_book(book: BookIn): # body validated against BookIn
book_id = len(books) + 1
books[book_id] = book
return {"id": book_id, **book.model_dump()}
@app.get("/books/{book_id}")
def get_book(book_id: int): # "abc" is rejected with a 422 error
if book_id not in books:
raise HTTPException(status_code=404, detail="Not found")
return books[book_id]
# Run with: uvicorn main:app --reload → docs at http://localhost:8000/docsReaders ask
FastAPI or Django?
FastAPI is focused on APIs, with type-driven validation and async support. Django is a full framework with an ORM, admin panel, templates and authentication built in. FastAPI suits JSON APIs and services; Django suits full web applications.
FastAPI or Flask?
Both are lightweight. FastAPI adds automatic validation, async support and generated docs from type hints. Flask is older and simpler, with a huge ecosystem of extensions.
What is Pydantic?
A Python library that validates data using type hints. FastAPI uses Pydantic models to check request bodies and to shape responses.
See also
- PythonProgramming Languages, p. 26Python is a general-purpose, dynamically typed programming language known for readable syntax and wide use in data science, automation, and web backends.
- REST APIBackend & APIs, p. 38A REST API is a web API that exposes data as resources identified by URLs and lets clients read or change them using standard HTTP methods.
- OpenAPIBackend & APIs, p. 33OpenAPI is an open standard for describing HTTP APIs in a YAML or JSON file, so people and tools can understand every endpoint, parameter, and response.
- APIBackend & APIs, p. 2An API is a set of rules that lets one piece of software request data or actions from another in a predictable, documented way.
- DjangoBackend & APIs, p. 11Django is an open-source Python web framework that comes with an ORM, an admin panel, authentication and security protections, so teams can build quickly.
- FlaskBackend & APIs, p. 18Flask is a lightweight Python web framework offering routing, request handling and templates, leaving the database and project structure to the developer.
Spotted a mistake or something missing on this page?Suggest an edit