Path Parameters in Python
From the FastAPI cheat sheet ยท Path & Query Parameters ยท verified Nov 2026
Path Parameters
Extract values from the URL path with type validation.
python
from typing import Annotated
from fastapi import FastAPI, Path
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(item_id: int):
return {"item_id": item_id}
# With validation
@app.get("/users/{user_id}")
async def read_user(
user_id: Annotated[int, Path(gt=0, le=1000, description="The user ID")],
):
return {"user_id": user_id}๐ก Path parameters are required โ they're part of the URL itself
โก Use Path() for validation: gt, ge, lt, le for numbers; min_length, max_length for strings
๐ Enum parameters auto-generate a dropdown in the /docs UI
๐ข Use {param:path} to capture a full file path including slashes
pathparametersvalidation
Continue with FastAPI
Save the full cheat sheet or work through every related task.