Up Next: Models and Validation
Look back at what Module 1 taught you, see a project that puts all of it to work, a book club's API, and see the gaps Module 2 closes, with Pydantic models and field rules that check every request, one clear shape for every error, and tests that prove the API works.
Apprentice · FastAPI · REST API · Updated
After this lesson you can
- Describe what Module 2's field rules, error shape and tests will add to an API built in Module 1
Key takeaways
- After Module 1 you can build a FastAPI app with path and query parameters, a 404, a request body, a 201 and a router per resource, and try every route on its docs page.
- A book club's API, a project you can build on your own now, puts all of it to work, with a list with filters, one book by its id, adding a book and the book the club is reading now.
- A type hint can check that a value is a whole number, but not that it makes sense. Module 2's field rules refuse a game for zero players or a blank title.
- Module 2 gives every error one clear shape, and its tests check every rule each time the code changes.
At the start of this module, an API was something other people's apps called. Now you can write one: routes that read their address, filters that narrow a list, honest status codes, new items from a request's body, and a project split into one file per resource. Those are the same pieces the API behind a store or a sign-up form is made of.
This lesson looks back at what you can do, shows you a project that puts it all together, and gives you a first look at Module 2, where the API learns to refuse bad input politely and proves it with tests.
What you can do now
After Module 1, you can:
- set up a project with a virtual environment, install FastAPI from a pinned
requirements.txt, and get exercise code and hand it in with Git; - use the Python an API leans on: functions, lists of dictionaries, JSON, exceptions, classes and type hints;
- speak HTTP: methods, paths, query strings, status codes and headers, and design a resource's endpoints the REST way;
- build routes with
@app.getand@app.post, read path parameters such as/games/{game_id}and query parameters such as?players=4, with type hints that turn their text into numbers; - answer honestly: raise
HTTPExceptionfor a 404, and setstatus_code=201on a route that creates; - take JSON in with a small Pydantic model, and organize routes with
APIRouter, a prefix and tags; - try every route on the docs page at
/docs.
Put it all together: a book club's API
Here's a project that uses all of it, one you can build on your own now. The Page Turners Book Club wants an API for its books, kept in memory:
- list the club's books, with filters:
?author=for one author's books and?read=truefor the ones the club has finished; - one book by its id, with a 404 when there's no such book;
- add a book, answering 201 with the new book and its id;
- mark a book as read, and the book the club is reading now;
- a router per resource, and the docs page as the API's manual.
When it's done, its docs page is the API's manual, and it's an API you can show a friend.
Where it falls short
Try this on the café's API from Taking JSON In: Your First Request Body:
{"title": "", "min_players": 0, "max_players": -3, "minutes": 9999}
FastAPI answers 201 and puts it on the shelf: a game with no name, for zero to minus three players, that takes almost a week. Every value has the right type, and that's all the model checks. A real API needs rules: a title that isn't blank, at least one player, a largest group no smaller than the smallest.
The errors aren't consistent either. A missing game answers {"detail":"There's no game 9."}, while a bad id answers a list of objects with type, loc and msg. An app that shows errors to people has to handle both shapes.
And so far, you've checked each route by clicking through the docs page. Change one line, and you'd have to click through every route again to be sure nothing broke.
What Module 2 unlocks
Module 2 makes the API trust nothing:
- Request models and field rules check every request: lengths, ranges and choices, such as "at least 1 player" or "a title of 1 to 80 characters", so the bad game above gets a 422 that says what's wrong with each field.
- Response models choose exactly what goes back out, so a sign-up list never shows other people's emails.
- One error shape for every error, a 404, a 409 or a 422, so an app handles them all the same way.
- Tests with pytest and
TestClient, the same tool this chapter's exercise uses, written by you this time: one test for every rule, run in seconds after every change.
By its end, you can build an API for a community center's event sign-ups that refuses bad input politely, with a test for every rule.
Check yourself
2 questions to try on the lesson page.
So what
Module 1 taught your API to answer; Module 2 teaches it to say no to bad input, the same way every time, and to prove it with tests.