Exercise: Bakery Specials API
Finish Maple Street Bakery's FastAPI app with two GET routes, a welcome message at / and today's specials at /specials, try them on the docs page, let pytest's tests check both, and hand your work in with Git.
Apprentice · FastAPI · REST API · Updated
Maple Street Bakery's owner wants today's specials in the bakery's phone app, and the app needs an API to ask. You'll finish that API: a welcome message at / and today's specials at /specials, built with FastAPI the way Your First FastAPI App built its routes. Tests check both answers, so you'll know when the API is right. This is the course's first exercise, so this lesson walks through every step, from getting the code to handing it in.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-fastapi-bakery-specials-api, also linked from the Exercise box beside the lesson. Clone it into your python-course folder. In VS Code's terminal:
cd ~/python-course
git clone https://github.com/ask-codey/exercise-fastapi-bakery-specials-api.git
Git makes a folder, exercise-fastapi-bakery-specials-api, with the exercise code inside. Get the Exercise Code with Git walks through cloning step by step. If you can't clone it, click Code on the repository's page, then Download ZIP, and unzip it into your python-course folder instead.
Open that folder in VS Code (File > Open Folder), open its terminal with Terminal > New Terminal, and make the exercise's virtual environment. On Windows (PowerShell):
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt
On a Mac (zsh):
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
The first makes the virtual environment, the second turns it on, so the prompt starts with (.venv), and the third installs FastAPI and pytest into it, at the exact versions requirements.txt lists. Virtual Environments and pip explains all three; every exercise in this course starts with them. If PowerShell says running scripts is disabled, the exercise's README has the one-line fix.
The folder holds:
main.py: the bakery's app, for you to finish. It makesappand brings in the specials; it has no routes yet.data.py: today's three specials, in the listSPECIALS. Ready to use.tests/test_bakery.py: the tests, which send requests to your app withTestClient. You don't need to change it.requirements.txtandpytest.ini: the packages the app and the tests need, at exact versions, and where pytest finds the tests and your code.
Run the tests first
Before you write anything, run the tests, with the virtual environment on:
pytest
All 5 tests fail. That's expected: the app has no routes, so every request answers 404, and each test says so. Read one of the messages, such as GET /specials should answer 200, but it answered 404 with {"detail":"Not Found"}: it names the request, what the test expected and what came back. Every test in this course talks to you this way. No server needs to be running: TestClient hands each request straight to the app.
What to do
The comment at the top of main.py lists every request the API should answer:
GET / 200, the welcome message, exactly:
{"bakery": "Maple Street Bakery", "message": "Welcome! Today's specials are at /specials."}
GET /specials 200, the list SPECIALS from data.py, exactly as it is there:
[{"name": "Sourdough loaf", "price": 6.5}, {"name": "A dozen bagels", "price": 12.0},
{"name": "Cranberry scone", "price": 3.5}]
- Welcome message. Under the comment that says to add the routes, write a route for
GET /that returns the welcome message above: a dictionary with the keysbakeryandmessage, its text copied word for word. - Today's specials. Below it, write a route for
GET /specialsthat returnsSPECIALS, the listmain.pyalready brings in fromdata.py. The address is exactly/specials, with no slash at the end.
Run pytest after each route: its tests turn green.
Check your work
Start the app and look at it the way the phone app will:
fastapi dev main.py
Open http://127.0.0.1:8000/specials in your browser, then http://127.0.0.1:8000/docs. The docs page, titled Maple Street Bakery, lists GET / and GET /specials; open each, click Try it out, then Execute, and compare the answer with the comment in main.py. Stop the server with Ctrl+C, then run the tests one last time:
pytest
Hints
- Copy the lesson's shape. Each route is a decorator,
@app.get("..."), right above a function that returns the answer, as in Your First FastAPI App. - Give each function its own name, such as
welcomeandlist_specials. The name doesn't change the address; the decorator sets that. - Return, don't print. A route's answer is what its function returns. A function that prints and returns nothing answers 200 with
null. - Copy the message exactly. The test compares every letter, the apostrophe in Today's and the final period included.
- One slash too many.
@app.get("/specials/")puts the list at/specials/, and the last test says so.
When you're done
Every test is green: pytest's last line says 5 passed. And http://127.0.0.1:8000/specials answers:
[{"name":"Sourdough loaf","price":6.5},{"name":"A dozen bagels","price":12.0},{"name":"Cranberry scone","price":3.5}]
Tomorrow the scone sells out and a cinnamon roll at $3.75 takes its place. Which file would you change, and would main.py change at all?
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. Hand In Your Work with Git walks through each step.
On GitHub, click +, then New repository. Name it (for example
exercise-fastapi-bakery-specials-api), choose Public, and click Create repository. Leave out the README: your folder already has one.In VS Code's terminal, in your exercise folder, send your work to your new repo. If you cloned the starter, point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-fastapi-bakery-specials-api.git git add . git commit -m "My solution" git push -u origin mainIf you downloaded the ZIP instead:
git init -b main git add . git commit -m "My solution" git remote add origin https://github.com/your-name/exercise-fastapi-bakery-specials-api.git git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-fastapi-bakery-specials-api, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
You cloned a project, made its virtual environment, read failing tests, wrote two routes that answer an app, and handed it in with Git. Every exercise in this course follows that same loop, and so does a back-end developer's working day.