Skip to content
tezvyn:

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

Source: fastapi.tiangolo.comEasyHow cards are made

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

knowledge of the APIRouter wiring pattern.

Key points

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

Watch out for

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

What's really being asked

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.

The full answer

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.

The mistakes people make

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.

What usually comes next

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.

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

Interview question

You have defined routes using APIRouter in a separate file. What is the correct final step in main.py to make those routes available?

  • a.Import the router object and call app.include_router(router)Correct
  • b.Copy the route functions into main.py and register them directly with @app.get
  • c.Call router.include_router(app) after creating the FastAPI instance
  • d.Import the router file and rely on FastAPI to auto-register the routes
Why?

Calling app.include_router(router) is the required step to mount the imported router on the FastAPI instance. Simply importing the module is not enough, as FastAPI does not auto-register routes from imported files.

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