Difficulty: Beginner
What is FastAPI? What are its key features and advantages over Flask or Django?
FastAPI is a modern, high-performance Python web framework for building APIs, based on standard Python type hints. It is built on top of Starlette (ASGI) and Pydantic.
Key features: - Speed: One of the fastest Python frameworks, on par with Node.js and Go, thanks to async support via ASGI - Automatic docs: Generates interactive OpenAPI (Swagger UI) and ReDoc documentation automatically - Type safety: Uses Python type hints for request/response validation via Pydantic - Dependency injection: Built-in, easy-to-use DI system - Async support: Native async/await support for non-blocking I/O - Standards-based: Built on OpenAPI and JSON Schema
Vs Flask: Flask is synchronous by default, no built-in validation, no auto docs. FastAPI is async-first with validation and docs built in. Vs Django: Django is a full-stack framework with ORM, admin, templates. FastAPI is API-focused and lightweight.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello, FastAPI!"}
# Run: uvicorn main:app --reload
# Docs: http://127.0.0.1:8000/docs
FastAPI, ASGI, type hints, OpenAPI