Concepts 🚀 Project 🧮 Labs
🧮 Labs / FastAPI / Path parameters — {id} becomes a function arg

Path parameters — {id} becomes a function arg

Wrap a segment of the path in {braces} and FastAPI extracts it. Type hints validate.

main.py v3.1
API simulator click Simulate
Click "Simulate" to see what this FastAPI app does.
🧠 How it works

{param} in path → param becomes a function arg. Type hints validate: int — /items/abc returns 422. str — anything works. float — /items/1.5 works. bool — /items/true, /items/1, /items/True work. UUID — /items/123e4567-e89b-12d3-a456-426614174000. Enum types constrain choices — invalid values return 422. Order matters: /users/me should be defined BEFORE /users/{user_id} (else 'me' is caught by the param route). HTTPException(status_code, detail) raises an HTTP error with a custom message.

✏️ Try these changes
  • Change item_id: int to item_id: str
    → /items/abc now works
  • Add a new route /products/{product_id} that returns a fake product
    → new endpoint
  • Change the Color enum to add 'yellow'
    → /colors/yellow works
  • Change HTTPException status_code to 400
    → Bad Request instead of Not Found