What a framework does for you, in a runnable toy
A toy 'framework' written as a plain function (**body.model_dump() spreads a dictionary into keyword arguments, Topic 5.3) and called through a mock transport. It shows the real mechanism behind the 422 answer: Pydantic validation turned into an HTTP response.
FastAPI is a Python framework for web APIs: you write ordinary functions with type hints, and it turns them into HTTP endpoints, validates path, query and body data with Pydantic, shapes the response, and generates interactive documentation. You run it with the uvicorn server.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write (not run here; save it in a file main.py) an endpoint GET /greet/{name} that returns {"message": "Hello, <name>!"} and accepts an optional query parameter shout (bool).
Write a model Item with name and price (above 0) and a POST /items endpoint that returns the item with status 201.
Using the runnable toy approach, write a mock-transport app function that answers GET /ping with {"pong": true} and anything else with 404.
Explain it without notes
How does FastAPI decide whether a function parameter is a path, query or body parameter?
What happens when a request has invalid data, and how do you return your own error?
What are response_model and the automatic /docs page, and where does the documentation come from?
When should an endpoint be def and when async def?
What does Depends do, and how does it help testing?
Expected output
201 {'title': 'write phase 15', 'done': False, 'id': 1}
422 {'detail': [{'loc': ['body', 'title'], 'msg': 'String should have at least 1 character'}]}
422 {'detail': [{'loc': ['body', 'title'], 'msg': 'Field required'}, {'loc': ['body', 'done'], 'msg': 'Input should be a valid boolean, unable to interpret input'}]}
200 {'title': 'write phase 15', 'done': False, 'id': 1}
404 {'detail': 'todo not found'}