Backend
FastAPI
A modern Python API framework built on type hints, with automatic validation and OpenAPI docs.
What it is
FastAPI is built on Starlette (the web part) and Pydantic (data validation). You describe parameters and models with type hints, and the framework validates input and serializes output.
Interactive Swagger UI docs and an OpenAPI schema appear from the code automatically. That saves time and keeps docs current.
It supports async def and runs on ASGI servers (Uvicorn). It is widely used for ML services and microservices.
Use it when
- REST APIs and microservices in Python.
- Services around ML models.
- You need auto-docs and strict data validation.
Avoid it when
- A classic web app with pages, forms and an admin: Django is handier.
- The team is not familiar with type hints and async.
Key concepts
- Pydantic
- A model library with validation based on type hints.
- Dependency injection
Dependsinjects dependencies (DB session, current user).- ASGI
- The async server interface Uvicorn runs on.
- OpenAPI
- The API schema docs and clients are generated from.
Example
Cheat sheet
| Command or code | What it does |
|---|---|
pip install 'fastapi[standard]' | Installs FastAPI with the standard extras |
fastapi dev main.py | Dev server with reload |
uvicorn main:app --reload | Runs through uvicorn |
app = FastAPI() | Creates the app |
@app.get('/items/{id}') | Route with a path parameter |
async def read(id: int, q: str | None = None): | Typed parameters |
class Item(BaseModel): name: str | Pydantic model |
def f(db = Depends(get_db)): | Dependency injection |
raise HTTPException(status_code=404) | Return an error to the client |
/docs | Automatic Swagger docs |
TestClient(app).get('/') | Tests an endpoint |
How to learn it
- Learn Python: functions, type hints and async.
- Take the FastAPI course on ShortLesson and start a first endpoint.
- Understand routes, path and query parameters.
- Describe data models with Pydantic and get validation for free.
- Study Depends and dependency injection.
- Connect a database with SQLAlchemy or SQLModel.
- Add authentication and tests with TestClient.
- Deploy with uvicorn in Docker.
Interview questions
1. Where do the auto docs come from?
FastAPI reads type hints and Pydantic models and builds an OpenAPI schema. Swagger UI is available at
/docswith no extra code.2. When to use
async defand whendef?async deffor code that awaits I/O through async libraries. Plaindefruns in a thread pool, so blocking code does not stall the event loop.3. What does
Dependsdo?It registers a dependency the framework creates and passes to the handler. Handy for DB sessions, auth and shared logic.
Comparisons with this technology
Related technologies
- PythonA readable general-purpose language: web, data, automation and machine learning.
- DjangoA batteries-included Python framework: ORM, admin, auth and templates.
- FlaskA Python microframework: a minimal core with routing and templates, the rest through extensions.
- PostgreSQLA powerful open source relational database with extensions, JSONB, full-text search and strong SQL support.
- GraphQLA query language and API specification where the client states exactly which fields it needs.
Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).