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

By

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, in DEMO_TRAILS, and seed_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, and GET /health isn't there.
  • routers/trails.py, trails_data.py, database.py and migrations/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.ini and .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.

  1. Add a .python-version file to the exercise folder, holding one line: the first two numbers that python --version prints, such as 3.15. It must be 3.12 or later.
  2. Finish Settings in settings.py: a pydantic-settings class that reads DATABASE_PATH and LOG_LEVEL from the environment or .env, with the defaults "trails.db" and "INFO". With DATABASE_PATH=/var/data/trails.db and LOG_LEVEL=WARNING set, it gives exactly those two values.
  3. Finish seed_demo_trails(conn) in seed.py, as its comment says.
  4. Seed at startup: in main.py's lifespan, call seed_demo_trails(conn) right after migrate(conn), and log both results: Trails API started: ran migrations [1], added 6 demo trails.
  5. Add GET /health to main.py: 200 with {"status": "ok"} when SELECT COUNT(*) FROM trails works, and 503 with the detail The database isn't answering. in the one error shape when it raises a sqlite3.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 BaseSettings with SettingsConfigDict(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 same conn to seed_demo_trails before it's closed.
  • The health route asks a real question, as in Startup, Shutdown and a Health Check. problem() is already in main.py.

Put it online

When every test is green:

  1. Try the start command on your computer, as Getting an API Ready for a Host shows, with PORT set to 3100. Delete trails.db first, so it starts on an empty database, the way it will on Render: the terminal shows Trails API started: ran migrations [1], added 6 demo trails, and http://127.0.0.1:3100/trails lists six trails. Stop it with Ctrl+C.
  2. Push your work to a public repository of your own: steps 1 and 2 of Submit your exercise, below.
  3. 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 command uvicorn main:app --host 0.0.0.0 --port $PORT, the Free plan, and /health as the health check path. If Render asks for a credit card, stop there: nothing in this course needs a paid plan.
  4. Try the live API. Open your service's address with /docs at the end, and try GET /trails. If the service has been asleep, the first answer takes about a minute.
  5. Add your live link to README.md, on its Live link: line, such as https://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 /health answers 200 OK with {"status":"ok"}.
  • GET /trails?status=muddy answers 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.

  1. 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.

  2. 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 main
    
  3. Paste 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.

Key terms

Related