Path Parameters in Python

From the FastAPI cheat sheet ยท Path & Query Parameters ยท verified Jul 2026

Path Parameters

Extract values from the URL path with type validation.

python
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: 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

More Python tasks

Back to the full FastAPI cheat sheet