Skip to content

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 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 /docs page

Your First Endpoints [25 min]

  • Download practice_api.py (save it outside your planner clone)
  • Set PLANNER_DB at the top of the file to your clone's planner.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)

Your To-Do List

  • Submit practice_api.py to Gradescope
  • Week 12 reading ← due Wednesday
  • GP4 reviews ← due Thursday (noon)
  • HW6 ← due Monday