Build a Local API with FastAPI
A tested to-do API with validation, filtering and clear failure responses.

What you will build
Two automated tests cover creation, reading, replacement, deletion, filters, an empty store and invalid requests. Start Uvicorn after the tests, then open http://127.0.0.1:8000/docs.
This workbook is for readers who can run a Python script. It includes a small local example, explanations, exercises and worked answers. The package was checked on Windows with Python 3.13.17. Pinned package versions and setup instructions are included; they are not a claim to be the latest releases.
Read a sample
The following two sections are taken from the practical download edition.
Keep the first service local
This package teaches request handling with a small in-memory to-do store. It is a local learning application, not a production service. Records disappear when the process restarts. There is no user authentication, persistence or permission model.
Run the server from a normal terminal with the supplied Uvicorn command. It binds to 127.0.0.1 so the demonstration is local to your machine. Stop it with Ctrl+C. Keeping server startup outside app.py makes the same application easy to import in tests.
create_app builds a fresh store and ID counter for each application instance. The tests therefore start independently. A production database would need transactions and persistent identifiers; multiple server processes must not each maintain their own independent store.
Define what a valid request contains
TodoCreate requires a description from 1 to 200 characters, strips surrounding whitespace and rejects unexpected fields. A whitespace-only description fails validation after stripping. These rules are part of the interface rather than ad hoc checks spread across endpoints.
TodoUpdate requires both description and completed because the example uses PUT for replacement. Todo adds the server-assigned ID to the response model. This avoids mixing client-controlled input with fields that the server owns.
If you later add PATCH, define partial-update semantics explicitly. A missing field and a field supplied as null are different inputs. Pydantic validation does not remove the need to decide which transitions your application allows.
Inside the complete guide
- Keep the first service local
- Define what a valid request contains
- Follow the request through the endpoints
- Validate query parameters too
- Test behaviour through the public interface
- Exercises and worked answers
Files in your download
The 6-page PDF includes the walkthrough and complete source listing. The examples folder contains:
app.pytest_api.pyREADME.txtrequirements.txt
Scope and source material
This is a focused learning project with synthetic example data. It does not include hosted services or production deployment. This revised edition is based on previously published articles and uses replacement runnable examples; it is not a verbatim reprint.
Get the guide on Payhip — £4.99