tezvyn:

How do you include a router in your main FastAPI app?

AI-drafted, machine-checkedSource: fastapi.tiangolo.combeginner
How do you include a router in your main FastAPI app?
WHAT IT TESTS

knowledge of the APIRouter wiring pattern.

ANSWER OUTLINE

create APIRouter in another file, import it into main.py, then call app.include_router(router).

RED FLAG

rewriting endpoints manually in main.py rather than using include_router.

WHAT THIS TESTS: This question checks if you know the standard FastAPI pattern for scaling an application across multiple files. Interviewers want to see that you understand APIRouter is the building block for modularity and that you can wire it into a FastAPI instance with a single method call. At the senior level this is table stakes, but even for beginner-targeted roles they want to hear the exact method name and see that you do not hardcode routes in the main file. Knowing where the router is created, where it is imported, and where it is mounted demonstrates that you have actually built a multi-file project rather than just a single-script demo.

A GOOD ANSWER COVERS: A strong response hits four things in order. First, create an APIRouter instance in a separate module and use it as a decorator for path operations just like you would with a FastAPI app. Second, import that router object into your main application file. Third, instantiate the main FastAPI application as app. Fourth, call app.include_router(router) to mount the imported router. Mentioning that you can pass optional arguments like prefix, tags, dependencies, or responses to include_router shows deeper familiarity with the docs. You should also note that the router is included after the FastAPI instance exists, not before.

COMMON WRONG ANSWERS: The biggest red flag is describing a manual approach where you redefine or copy route functions into main.py instead of importing the router object. Another mistake is forgetting the include_router step and assuming that merely importing the module registers the routes. Some candidates also confuse Flask blueprints or Django URLconfs and describe patterns that do not exist in FastAPI, such as manually iterating over routes. A subtler error is trying to call include_router on the APIRouter itself rather than on the FastAPI app instance.

LIKELY FOLLOW-UPS: An interviewer might ask how you would add a shared prefix to every route in a router, which you do by passing prefix="/items" to include_router. They could also ask how to apply dependencies or tags to an entire router, which are also arguments to include_router. A more advanced follow-up is whether you can include the same router multiple times with different prefixes, which FastAPI supports. You might also be asked how relative imports work when the router file is in a sub-package, so be ready to explain dot notation.

ONE CONCRETE EXAMPLE: Imagine a file named items.py where you write from fastapi import APIRouter and router = APIRouter(), then define a get endpoint with @router.get("/"). In main.py you write from fastapi import FastAPI, from .items import router as items_router, app = FastAPI(), and finally app.include_router(items_router, prefix="/items", tags=["items"]). That is five lines in main.py and two lines in the module, and it is the minimal canonical pattern. You can repeat this pattern for users, auth, or any other domain and keep main.py clean.

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.