Path Parameters: Turning URL Parts into Variables

Path parameters turn parts of a URL, like `/users/123`, into typed function arguments. FastAPI uses this to create endpoints for specific resources, like fetching a user by their ID. The footgun is forgetting type hints; without `int`, `123` is just a string.
Path parameters treat parts of a URL as dynamic function arguments. Instead of a static `/items/123`, you define `/items/{item_id}`, and FastAPI passes `123` to your function. This is standard for fetching or updating a specific resource by its identifier. The main footgun is forgetting type hints; path values are strings by default, so you must declare `item_id: int` for automatic validation and conversion, preventing unexpected type errors in your logic.
Read the original → fastapi.tiangolo.com
- #fastapi
- #python
- #api
- #rest
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.