FastAPI
Async web framework that derives validation, serialisation and OpenAPI docs from your type hints.
What it is
FastAPI is a modern, high-performance Python web framework for building APIs with automatic interactive documentation, leveraging Python type hints for data validation and serialization.
FastAPI allows you to define API endpoints using Python function definitions with type annotations. It supports async operations, request validation, automatic docs generation, dependency injection, security, and more.
- Built on
- Starlette for the web layer and Pydantic for validation
- Licence
- MIT
- Best known for
- Automatic OpenAPI docs at /docs with zero configuration
When to use it
The question documentation cannot answer for you — because it cannot recommend something else.
Reach for it when
- Building a JSON API where request and response shapes matter
- You want interactive API documentation generated automatically and kept in sync
- The workload is I/O-bound — calling databases, queues or other services concurrently
Look elsewhere when
- You need a full-stack framework with an admin interface, ORM and templating included — that is Django
- The team is unfamiliar with async and the workload is simple and synchronous
Installation
pip install fastapi[all]Getting started
The smallest useful thing you can do with it, and what each part means.
from fastapi import FastAPI
app = FastAPI()
@app.get('/items/{id}')
async def read_item(id: int):
return {'id': id}from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
@app.post('/items/')
async def create_item(item: Item):
return itemAdvanced usage
Where the library earns its place over a simpler alternative.
from fastapi import FastAPI, Query
app = FastAPI()
@app.get('/search')
async def search(q: str = Query(..., min_length=3, max_length=50)):
return {'query': q}@app.get('/users/{user_id}')
async def get_user(user_id: int):
return {'user_id': user_id}# Run your FastAPI app using uvicorn:
# uvicorn main:app --reloadfrom fastapi import Depends
async def common_parameters(q: str = None, limit: int = 10):
return {'q': q, 'limit': limit}
@app.get('/items/')
async def read_items(commons: dict = Depends(common_parameters)):
return commonsErrors and fixes
The failures you are most likely to hit, and what actually resolves them.
- 422 Unprocessable Entity
- Occurs when request body validation fails. Ensure JSON fields match Pydantic model types.
- 404 Not Found
- Use proper path parameters and raise HTTPException with status_code=404 when resources are missing.
Best practices
- Use Pydantic models for request validation and response models.
- Leverage async endpoints for IO-bound operations.
- Use dependency injection for reusable logic like authentication or DB connections.
- Keep path and query parameters explicit for clarity.
- Include meaningful tags and summaries for better auto-generated documentation.
Alternatives
Comparable options, and the reason you would pick one over the other.
Background
Why it exists, and what it was reacting to.
FastAPI was created by Sebastián Ramírez in 2018. Its goal was to provide a framework that is fast (high-performance), easy to use, and fully compatible with modern Python features like type hints and async programming. It automatically generates OpenAPI and Swagger documentation for your APIs, making it a popular choice for building RESTful services and microservices.
