Guide Home Part 1 - FastAPI Foundations Part 2 - Building Applications Part 3 - Pages and HTML Part 4 - Security and Deployment Part 5 - Going Further Capstone Build Plan Markdown Source All Guides

Chapter 6: Routing

ASP.NET Core endpoint routing maps URLs and HTTP verbs to handlers. FastAPI routes are path operation decorators.

from fastapi import APIRouter

router = APIRouter(prefix="/api/recipes", tags=["recipes"])


@router.get("")
def list_recipes():
    ...


@router.get("/{recipe_id}")
def get_recipe(recipe_id: int):
    ...


@router.put("/{recipe_id}")
def replace_recipe(recipe_id: int):
    ...

Register routers in the app:

from recipevault.api import recipes

app.include_router(recipes.router)

Routing best practices: