Exercise: Trail Conditions Online
Get Willow Creek Parks' finished trails API ready for a host (a .python-version file, settings read from the environment, demo trails seeded at startup on an empty database, and a /health route), then put it online with Render's free web service and hand in a repository whose README links the live API.
Journeyman · FastAPI · REST API · Updated
Hikers keep phoning Willow Creek Parks' ranger station to ask which trails are muddy. The parks department's trails API is finished and tested, but it only runs on a laptop. You'll get it ready for a host, the way this chapter showed, and then put it online with Render, so the link you hand in works on any hiker's phone.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-fastapi-trail-conditions-online, 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-trail-conditions-online.git
Open the new exercise-fastapi-trail-conditions-online 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
Get the Exercise Code with Git walks through cloning step by step, if you'd like a reminder, and the exercise's README says what each command does.
The folder holds:
settings.py: the API's settings, for you to finish. Today they're written into the code.seed.py: the six demo trails, inDEMO_TRAILS, andseed_demo_trails(conn), for you to finish.main.py: the app, its lifespan and its one error shape. For you to finish: the lifespan doesn't add the demo trails yet, andGET /healthisn't there.routers/trails.py,trails_data.py,database.pyandmigrations/0001_create_trails.sql: the finished trail routes, their queries, the connection for each request, and the trails table. Ready to use.tests/test_trails_online.py: the tests. You don't need to change it.requirements.txt(every package pinned),.env.example,pytest.iniand.gitignore. Ready to use.
What to do
settings.py, seed.py and main.py each start with a comment that says exactly what they must do. Here is seed.py's:
seed_demo_trails(conn):
- When the trails table has no rows: adds every trail in DEMO_TRAILS, commits, and returns how many it added, 6.
- When the trails table already has rows: changes nothing and returns 0, so a database you've been using on your
own computer keeps its trails as they are.
For example, on a new, migrated database, the first seed_demo_trails(conn) returns 6 and the table holds six trails; a second call returns 0 and the table still holds six.
- Add a
.python-versionfile to the exercise folder, holding one line: the first two numbers thatpython --versionprints, such as3.15. It must be 3.12 or later. - Finish
Settingsinsettings.py: a pydantic-settings class that readsDATABASE_PATHandLOG_LEVELfrom the environment or.env, with the defaults"trails.db"and"INFO". WithDATABASE_PATH=/var/data/trails.dbandLOG_LEVEL=WARNINGset, it gives exactly those two values. - Finish
seed_demo_trails(conn)inseed.py, as its comment says. - Seed at startup: in
main.py's lifespan, callseed_demo_trails(conn)right aftermigrate(conn), and log both results:Trails API started: ran migrations [1], added 6 demo trails. - Add
GET /healthtomain.py: 200 with{"status": "ok"}whenSELECT COUNT(*) FROM trailsworks, and 503 with the detailThe database isn't answering.in the one error shape when it raises asqlite3.Error.
Check your work
Run the tests from the exercise folder, with the virtual environment on:
pytest
No server needs to be running, and every test uses a throwaway database file of its own. At first tests 1 to 9 fail, and tests 10 to 14, which check the finished trail routes, pass. Each step above turns its tests green. A failing test's message says what it expected and what came back, for example seed_demo_trails on an empty database should answer 6, but it answered 0.
Hints
- The settings have defaults, so neither is required. Settings Checked at Startup shows
BaseSettingswithSettingsConfigDict(env_file=".env"). - Count first.
conn.execute("SELECT COUNT(*) FROM trails").fetchone()[0]is the number of trails. Where the Data Lives When Your API Is Online shows the whole function. - Use the lifespan's own connection. The lifespan opens
conn, so pass that sameconntoseed_demo_trailsbefore it's closed. - The health route asks a real question, as in Startup, Shutdown and a Health Check.
problem()is already inmain.py.
Put it online
When every test is green:
- Try the start command on your computer, as Getting an API Ready for a Host shows, with
PORTset to3100. Deletetrails.dbfirst, so it starts on an empty database, the way it will on Render: the terminal showsTrails API started: ran migrations [1], added 6 demo trails, andhttp://127.0.0.1:3100/trailslists six trails. Stop it with Ctrl+C. - Push your work to a public repository of your own: steps 1 and 2 of Submit your exercise, below.
- Make the Render web service from that repository, as Put Your API Online with Render shows: Language Python 3, build command
pip install -r requirements.txt, start commanduvicorn main:app --host 0.0.0.0 --port $PORT, the Free plan, and/healthas the health check path. If Render asks for a credit card, stop there: nothing in this course needs a paid plan. - Try the live API. Open your service's address with
/docsat the end, and tryGET /trails. If the service has been asleep, the first answer takes about a minute. - Add your live link to
README.md, on itsLive link:line, such ashttps://your-service-name.onrender.com/trails. Commit and push: Render deploys again by itself.
When you're done
Every test is green: pytest's last line says 14 passed. And your API gives these answers on your computer and at your live link:
GET /healthanswers 200 OK with{"status":"ok"}.GET /trails?status=muddyanswers 200 OK with the two muddy trails:
[{"slug":"lookout-ridge","name":"Lookout Ridge Trail","miles":5.8,"status":"muddy","report_date":"2026-11-15","report_note":"Muddy past the second bridge. Wear boots."},{"slug":"meadow-view-loop","name":"Meadow View Loop","miles":2.4,"status":"muddy","report_date":"2026-11-16","report_note":"Soft ground in the low meadow after rain."}]
Then open your service's Logs page on Render after your last push: the lifespan's line says added 6 demo trails again, because the new deploy started on an empty disk. That's the demo data at work.
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-trail-conditions-online), 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. Point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-fastapi-trail-conditions-online.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-fastapi-trail-conditions-online, 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. Its README's live link is how anyone reviewing it opens your API.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
The same few changes (a pinned Python, settings from the environment, seed data for a disk that forgets, and a health route) make any FastAPI app ready to go online, and now you have a live link to prove it.