What is FastAPI?
FastAPI is a modern, fast (high-performance) web framework for building APIs with Python, based on standard Python type hints. It is one of the fastest Python frameworks available — on par with NodeJS and Go.
Why FastAPI?
- Speed — Based on Starlette and Pydantic. Benchmarks show performance close to NodeJS and Go.
- Fast to code — Type hints mean your IDE can autocomplete everything. Fewer bugs.
- Automatic docs — Go to
/docsfor interactive Swagger UI. Go to/redocfor ReDoc. Both auto-generated from your code. - Validation — Request body, query parameters, path parameters are all validated automatically via Pydantic.
- Async — Native support for
async defendpoints.
Getting Started
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str | None = None):
return {"item_id": item_id, "q": q}
uvicorn main:app --reload
FastAPI for ML Serving
FastAPI is the de facto standard for serving machine learning models as APIs. Load your model once on startup, serve predictions via a POST endpoint. Works seamlessly with PyTorch, TensorFlow, scikit-learn, and HuggingFace.