python
41 lines · 7 steps
Building a users router in FastAPI
A FastAPI router pairs Pydantic response models with SQLAlchemy queries to serve validated, shape-controlled JSON.
Explained by
highlit
1from datetime import datetime
2from fastapi import APIRouter, Depends, HTTPException, status
3from pydantic import BaseModel, ConfigDict, EmailStr
4from sqlalchemy.orm import Session
5
6from .database import get_session
7from .models import User
8
9router = APIRouter(prefix="/users", tags=["users"])
10
11
12class ProfileOut(BaseModel):
13 model_config = ConfigDict(from_attributes=True)
14
15 id: int
16 username: str
17 email: EmailStr
18 full_name: str | None = None
19 is_verified: bool
20 created_at: datetime
21
22
23class UserListItem(BaseModel):
24 model_config = ConfigDict(from_attributes=True)
25
26 id: int
27 username: str
28 is_verified: bool
29
30
31@router.get("", response_model=list[UserListItem])
32def list_users(session: Session = Depends(get_session)):
33 return session.query(User).order_by(User.username).all()
34
35
36@router.get("/{user_id}", response_model=ProfileOut)
37def get_user(user_id: int, session: Session = Depends(get_session)):
38 user = session.get(User, user_id)
39 if user is None:
40 raise HTTPException(status.HTTP_404_NOT_FOUND, detail="User not found")
41 return user
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Separate response models let each endpoint expose exactly the fields it should, hiding sensitive columns like email from list views.
- 2from_attributes lets Pydantic read ORM objects directly, so you can return SQLAlchemy models and FastAPI serializes them for you.
- 3Depends injects a database session per request, keeping route functions free of setup and teardown boilerplate.
Related explainers
javascript
const express = require('express'); const router = express.Router(); router.get('/articles/:slug', async (req, res, next) => {
Conditional GET caching in Express
http-caching
conditional-get
routing
Intermediate
8 steps
go
package handlers import ( "net/http"
Custom validators and binding in Gin
validation
struct-tags
error-handling
Intermediate
8 steps
python
import secrets from fastapi import Depends, FastAPI, HTTPException, Security, status from fastapi.security import APIKeyHeader
API key authentication as a FastAPI dependency
authentication
dependency-injection
api-keys
Intermediate
8 steps
php
class OrderReceiptController extends Controller { public function store(Request $request, Order $order) {
Handling receipt uploads in a Laravel controller
file-upload
validation
authorization
Intermediate
5 steps
javascript
function parseHexColor(hex) { const cleaned = hex.trim().replace(/^#/, ''); const expand = (short) =>
Parsing hex colors into RGBA channels
parsing
bitwise
regex
Intermediate
7 steps
python
import hashlib from collections import defaultdict from pathlib import Path
Finding duplicate files by size then hash
hashing
file-io
deduplication
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-a-users-router-in-fastapi-explained-python-9752/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.