Copy as MarkdownCtrl+⇧+C
Chapter 67 — FastAPI: Your First Web API
The SmartNotes CLI works for James. He types commands in a terminal and gets results. But what about a mobile app? A browser? A chatbot? CLI tools require a terminal. Web APIs work everywhere. This chapter teaches you to expose SmartNotes over HTTP so any program, on any device, can add notes, search them, and export data.
FastAPI is the natural fit: it uses the typed Pydantic models you already know (Ch 55), it is async-native (Ch 66), and it auto-generates interactive documentation. You will build 3-4 routes, test them with TestClient, and see your notes in a browser for the first time.
| # | Lesson | Focus | SmartNotes Deliverable |
|---|---|---|---|
| 1 | What Is a Web API? | HTTP methods, URLs, status codes (conceptual) | None (vocabulary building) |
| 2 | Hello FastAPI | FastAPI app, route decorator, uvicorn, /docs UI | GET / health endpoint |
| 3 | Request and Response Models | Pydantic request/response schemas, POST route, 422 validation | POST /notes with NoteCreate/NoteResponse models |
| 4 | Path Parameters and Search | Path params, query params, route ordering | GET /notes/{id}, GET /notes/search |
| 5 | Testing with TestClient | TestClient, TDG for API routes | Test suite for all routes |
| 6 | SmartNotes API Capstone | Full TDG: complete API with search and export | GET/POST, search, export endpoints |
Prerequisites: Async Python (Ch 66 for async def routes), Pydantic BaseModel (Ch 55), pytest (Ch 52), SmartNotes package (Ch 63).