Nov 10: FastAPI and SQLModel
Learning Objectives
After today's class, you should be able to:
- Explain how a REST endpoint maps an HTTP request to a database query.
- Return query results as JSON from a path operation, using a session dependency.
Lesson Outline
Announcements [5 min]
- GP4 Peer Reviews are due before Thursday's class
- Responses to your reviewers are due before Monday
Activity: Web Requests [25 min]
- Web Requests: How Does a URL Become a Query?
- Model 1: seven requests to a course catalog
- Model 2: the Python code behind those requests
Web APIs with FastAPI [15 min]
- FastAPI and SQLModel
- Client → FastAPI → your function → the database
- Methods, status codes, and how REST names things
- Path operations,
SessionDep, and the/docspage
Your First Endpoints [25 min]
- Download practice_api.py (save it outside your
plannerclone) - Set
PLANNER_DBat the top of the file to your clone'splanner.db - Run
fastapi dev practice_api.py, then open http://127.0.0.1:8000/docs - Read the two examples, then write Problems 1–3 (Problem 4 is optional)