new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

FastAPI · Tutorial · all subjects

bigger-applications/include-router

6 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

include_router() to add APIRouter to FastAPI app

In the main `FastAPI` application file, import the submodules containing `APIRouter` instances and add them to the app using `app.include_router(router_instance)`. For example: `app.include_router(items.router)`. This includes all path operations from the router into the main application. FastAPI keeps the original router and its routes active, maintaining custom subclasses and ensuring they participate in routing and OpenAPI generation.

include_router() with custom prefix, tags, responses, and dependencies

When calling `app.include_router()`, you can override or add configuration without modifying the original router: `app.include_router(router, prefix="/path", tags=["tag"], responses={418: {"description": "..."}}, dependencies=[...])`. This allows sharing an unmodified router across projects while customizing it per application.

Include the same router multiple times with different prefixes

You can call `app.include_router()` multiple times with the same router instance using different `prefix` values. This allows exposing the same API under different paths, such as `/api/v1` and `/api/latest`. This is an advanced usage pattern.

Include an APIRouter in another APIRouter

Use `router.include_router(other_router)` to include one `APIRouter` inside another. This works before or after the parent router is included in the `FastAPI` app. FastAPI will include all path operations from the nested router in routing and OpenAPI generation. Use documented APIs like path operation decorators and `.include_router()` to add routes; avoid directly mutating `router.routes` after inclusion.

Dependencies for group of path operations

When structuring bigger applications with multiple files, you can declare a single dependencies parameter for a group of path operations using APIRouter.

Using app.frontend() with APIRouter

You can add frontend files to an APIRouter and include it with a prefix using app.include_router(). Frontend paths are then served under that prefix. Regular path operations in the app still take precedence, including in other routers.

Give your agent this brain