Skip to content
tezvyn:

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

Source: fastapi.tiangolo.comMediumHow cards are made

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's really being asked

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.

The full answer

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.

The mistakes people make

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.

What usually comes next

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.

A 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.

Interview question

Which approach best keeps FastAPI router modules decoupled and reusable when applying a shared path prefix like /api/v1?

  • a.Use relative paths in the router and apply the shared prefix via app.include_router when mountingCorrect
  • b.Pass the prefix argument directly to the APIRouter constructor instead of to include_router
  • c.Create custom middleware that rewrites incoming URLs to prepend the shared prefix before routing
  • d.Hardcode the full absolute path in every APIRouter decorator and mount the router without any prefix
Why?

Defining relative paths in APIRouter and setting the prefix in include_router keeps route definitions separate from URL composition, enabling reuse. Hardcoding full paths scatters configuration, while middleware and router-level prefix arguments add unnecessary complexity.

Just read this? Test yourself on what you have been reading.

Read the original → fastapi.tiangolo.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.

See open roles