Stage 5 track
Track: Web Backend
Capstone: REST API with authentication · Tools: FastAPI, Pydantic, SQLAlchemy, Docker · Time: ~2 weeks (one step every 2–3 days, ~3 hours a day)
Who this track is for
You enjoyed Part 12 (HTTP, REST, SQL) and the layered design of the library manager project, and you want to build the server side of apps and websites rather than scripts that run once and exit.
Job roles this maps to: Backend Developer, Software Engineer (Python/backend), API Developer, and later Backend/Platform engineering roles that expect you to ship and operate a service, not just write a script.
You already have, from earlier stages:
- OOP, classes and dataclasses (Part 9)
- Context managers and decorators (Part 10)
- pytest, logging, type hints (Part 11)
- HTTP basics, REST, status codes, SQL and
sqlite3(Part 12) - A layered architecture (CLI → service → repository → database) from the library manager project
This track replaces pieces of that stack with production-grade tools: sqlite3 + hand-written SQL becomes SQLAlchemy, dataclasses used for validation become Pydantic models, and the CLI becomes a web API.
What you'll learn, step by step
Step 1 — FastAPI and Pydantic basics
Builds on: Part 12 (HTTP, REST, status codes), Part 9 (classes, type hints).
- What FastAPI is: a web framework for building APIs, built on type hints.
- Pydantic: data-validation models. Where Part 9 used
@dataclassforBook/Member/Loan, Pydantic models do the same job for data coming over the network, and validate it automatically. - Path parameters, query parameters, request bodies, response models.
- The automatic interactive docs FastAPI generates for every endpoint — use them constantly while developing.
🔍 Sketch, not a lesson — just to show the shape of a FastAPI route:
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Book(BaseModel): title: str author: str @app.get("/books/{book_id}") def get_book(book_id: int): ... # look up and return a BookRead the official FastAPI tutorial for the real, runnable version — exact behaviour depends on the FastAPI and Pydantic versions you install.
Step 2 — SQLAlchemy
Builds on: Part 12 (SQL, tables, primary/foreign keys), the Repository class pattern from the library manager project.
- SQLAlchemy is an ORM (Object-Relational Mapper): it lets you describe tables as Python classes and query them with Python instead of hand-written SQL strings.
- Map the same kind of tables you built by hand in Part 12 (books, members, loans — or your API's own entities) to SQLAlchemy models.
- Sessions, adding/querying/updating rows, relationships between tables (the ORM equivalent of the
REFERENCESforeign keys you used in Part 12). - Keep the same layering discipline as the library manager: routes call a service layer, the service layer calls the database layer — the database layer is the only place that touches SQLAlchemy.
Step 3 — Authentication
Builds on: Part 6 (custom exceptions), Part 9 (OOP).
- Why you never store plain-text passwords: password hashing.
- Issuing and verifying tokens so a user stays "logged in" across requests (the standard approach used in FastAPI's own security tutorial).
- Protecting routes so only a logged-in user (or the right user) can reach them.
- Turning auth failures into clean HTTP error responses (401/403) — the same "one error, one clear message" discipline from Part 6 and the library manager project.
Step 4 — Structure and testing
Builds on: Part 11 (pytest, type hints), the layered architecture from the library manager project.
- Organise the project into routers, services and a database layer — the same separation of concerns ("the CLI never writes SQL") you already practiced, just with HTTP routes instead of a CLI.
- Dependency injection in FastAPI (e.g. injecting a database session into a route).
- Testing API endpoints with FastAPI's test client and pytest, the same
pytest.raises/fixture style from Part 11.
Step 5 — Docker
Builds on: Part 7 (environments, pip).
- What a container is and why it solves "works on my machine": it packages your app with its exact dependencies.
- Writing a
Dockerfilefor your API, and running your app plus its database as separate containers together. - Keeping secrets (passwords, tokens) out of your code using environment variables, not hard-coded strings.
Step 6 — Polish (optional buffer)
Finish the authentication flow end to end, write the README, and attempt a stretch goal.
Setup pointers (official docs only)
| Tool | Official docs |
|---|---|
| FastAPI | https://fastapi.tiangolo.com/ |
| Pydantic | https://docs.pydantic.dev/ |
| SQLAlchemy | https://www.sqlalchemy.org/ |
| Docker | https://docs.docker.com/ |
Install everything inside a virtual environment, exactly as you learned in Part 7 (python3 -m venv .venv, then pip install inside it). Follow each project's own "Getting Started" page for the current install command and version — don't hard-code a version number from memory, library releases move fast.
Capstone brief: REST API with authentication
User stories
- As a new user, I can register an account with an email and password.
- As a registered user, I can log in and receive a token that proves who I am.
- As a logged-in user, I can create, read, update and delete my own records of some resource (pick a domain you like — notes, expenses, books, tasks).
- As a logged-in user, I cannot see or change another user's records.
- As any client, I get a clear JSON error with the right HTTP status code when something goes wrong (not found, not allowed, bad input).
- As a developer, I can run the whole thing with one command using Docker.
Architecture sketch
HTTP request (JSON)
│
▼
┌───────────────────────────┐
│ routers/ (API) │ FastAPI routes, Pydantic request/response models
└────────────┬──────────────┘
│ calls service functions
▼
┌───────────────────────────┐
│ services/ │ business rules: "only the owner can edit this",
│ │ password hashing, token checks
└────────────┬──────────────┘
│ calls repository functions
▼
┌───────────────────────────┐
│ db/ │ SQLAlchemy models and session, all queries
└────────────┬──────────────┘
▼
database (container)
Dockerfile + docker-compose: runs the API container
and the database container together.
This is the same shape as the library manager's architecture: a layer that talks to the outside world, a layer that holds the rules, and a layer that touches storage — only the storage layer is now SQLAlchemy instead of raw sqlite3, and the outside world is HTTP instead of a CLI.
Milestones
- Skeleton — FastAPI app runs, one
GET /healthroute returns{"status": "ok"}. - Data layer — SQLAlchemy models and tables for your chosen resource; create and read it through the API with no auth yet.
- Auth — register and login endpoints; passwords hashed, never stored in plain text; a working token flow.
- Ownership rules — update/delete only works for the record's owner; everything else returns a 403 or 404 with no leaked details.
- Tests — pytest tests covering at least: register, login, a successful create, an unauthorized attempt, a not-found case.
- Containerized —
Dockerfileand (if you used a separate database container) a compose file bring the whole thing up with one command.
Acceptance checklist
- Registration rejects a duplicate email with a clear error, not a crash
- Passwords are hashed in the database, never stored as plain text
- A protected route returns 401 with no token and 403 for the wrong user
- CRUD operations work end to end through the interactive docs
- pytest tests pass and cover at least one success and one failure path per endpoint
- The app starts with one documented Docker command
- Secrets (passwords, token secret key) are read from environment variables, not committed to the repo
Stretch goals
- 🟡 Pagination and filtering on list endpoints.
- 🟡 Role-based access (e.g. an "admin" who can see everyone's records).
- 🔴 Rate limiting on the login endpoint to slow down password-guessing.
- 🔴 A second container that runs background work (e.g. sending a welcome message) separate from the API container.
How you'll know you're done
- Every item in the acceptance checklist above is ticked
- You can explain, out loud, why passwords are hashed and why a token is not the same as a password
- You can explain your three-layer split (routes / services / db) and why the database layer is the only one that imports SQLAlchemy
- The repo is on GitHub with a README that states the goal, lists the endpoints, and shows how to run it with Docker in one command
- You can demo it live: start the container, open the interactive docs, register, log in, and perform one authenticated action