How to apply a dependency to only one FastAPI router?
router-level dependency injection in FastAPI.
pass dependencies=[Depends(auth)] to APIRouter for /users, omit it for /items, include both.
repeating Depends() on every route or using middleware.
WHAT THIS TESTS: whether you understand FastAPI dependency injection scoping and the APIRouter constructor. Senior engineers should know that dependencies can be attached to routers, not just individual routes or the global app.
A GOOD ANSWER COVERS four things in order. First, instantiate the users router with the dependencies parameter: users_router = APIRouter(prefix="/users", dependencies=[Depends(get_current_user)]). Second, instantiate the items router normally without that parameter: items_router = APIRouter(prefix="/items"). Third, include both in the main application with app.include_router(users_router) and app.include_router(items_router). Fourth, note that router-level dependencies execute before every path operation in that router, are additive to any route-level dependencies, and automatically show up in OpenAPI schema documentation.
COMMON WRONG ANSWERS include three patterns. One is manually adding Depends(get_current_user) to every single path operation in the users router, which violates DRY and makes refactoring painful. Two is suggesting an app-level dependency and then trying to exclude /items with conditional logic inside the dependency, which is backwards and hard to maintain. Three is immediately jumping to middleware, which bypasses FastAPI dependency injection, breaks automatic OpenAPI documentation for security schemes, and makes testing harder because you cannot override it with dependency_overrides.
LIKELY FOLLOW-UPS the interviewer may ask next. How would you exempt a single route inside the users router from authentication? The answer is that you cannot easily remove a router dependency from one route; instead you would either split that route into a separate router or make the dependency itself conditional based on a parameter. Another follow-up is how router dependencies interact with route dependencies: they run first, and if any dependency uses yield, the router dependency exit code runs after the route dependency exit code. A third follow-up is about testing: because router dependencies are standard FastAPI dependencies, you can override them in the test client with app.dependency_overrides.
ONE CONCRETE EXAMPLE you can sketch in the interview. Write from fastapi import APIRouter, Depends and define async def get_current_user(): pass. Then write users_router = APIRouter(prefix="/users", dependencies=[Depends(get_current_user)]) and items_router = APIRouter(prefix="/items"). Add a route like @users_router.get("/me") and @items_router.get("/public"). Finally register both with app.include_router. This takes thirty seconds to type and proves you know the API.
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.