tezvyn:

How to apply a dependency to only one FastAPI router?

Source: fastapi.tiangolo.comintermediate

TESTS: router-level dependency injection in FastAPI. OUTLINE: pass dependencies=[Depends(auth)] to APIRouter for /users, omit it for /items, include both. RED FLAG: repeating Depends() on every route or using middleware.

TESTS: whether you know FastAPI dependencies can be scoped to an APIRouter. OUTLINE: first, create the /users router with dependencies=[Depends(get_current_user)] in the APIRouter constructor; second, create the /items router without the dependencies argument; third, register both with app.include_router(). Router dependencies run before every route in that router and appear in OpenAPI docs.

Read the original → fastapi.tiangolo.com

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.

How to apply a dependency to only one FastAPI router? · Tezvyn