tezvyn:

How do you apply a common path prefix across FastAPI routers?

AI-drafted, machine-checkedSource: fastapi.tiangolo.comintermediate
How do you apply a common path prefix across FastAPI routers?

Tests whether you know APIRouter decouples routes from path prefixes. Use relative paths in APIRouter, then mount with app.include_router(router, prefix="/api/v1"). This keeps modules reusable. Red flag: hardcoding the full absolute path in every decorator.

WHAT THIS TESTS: This question checks if you understand the separation of concerns between defining routes and composing URL structure in FastAPI. Interviewers want to see that you know APIRouter is meant to encapsulate related endpoints, while the main FastAPI app controls how those endpoints are exposed to the outside world. It also tests whether you value DRY principles and modular design when building larger applications.

A GOOD ANSWER COVERS: First, define relative paths inside each APIRouter, such as /users or /items, without any global prefix. Second, in the main application file, import each router and mount it using app.include_router with the prefix parameter set to the shared path like /api/v1. Third, explain that this keeps router modules decoupled so they can be reused across different apps or mounted with different prefixes. Fourth, mention that include_router also accepts tags, dependencies, and responses, so the prefix is just one part of how you configure a router at mount time.

COMMON WRONG ANSWERS: Hardcoding the full path such as /api/v1/users directly in every APIRouter decorator is the biggest red flag because it scatters configuration and makes refactoring painful. Another weak pattern is creating a custom base class or middleware to rewrite paths, which adds unnecessary complexity when FastAPI already provides a built-in mechanism. Some candidates also confuse APIRouter with the main FastAPI class and try to attach prefixes at the router level using incorrect constructor arguments.

LIKELY FOLLOW-UPS: An interviewer might ask how you would apply different authentication dependencies to the same router under different prefixes, which you can do by passing dependencies into include_router. They might also ask about nested routers, where one APIRouter includes another, and whether prefixes stack, which they do. You could also be asked how this design affects OpenAPI schema generation, and the answer is that FastAPI handles prefix concatenation automatically so the docs remain accurate.

ONE CONCRETE EXAMPLE: Suppose you have a users router with @router.get("/") and an items router with @router.get("/"). In main.py, you write app.include_router(users_router, prefix="/api/v1") and app.include_router(items_router, prefix="/api/v1"). The resulting endpoints are /api/v1/ and /api/v1/items. If you later need to expose the same users logic under /api/v2, you simply include the router again with prefix="/api/v2" without touching the router file.

Source: fastapi.tiangolo.com

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.