MCPcopy
hub / github.com/fastapi/fastapi / include_router

Method include_router

fastapi/routing.py:3082–3269  ·  view source on GitHub ↗

Include another `APIRouter` in the same current `APIRouter`. Read more about it in the [FastAPI docs for Bigger Applications](https://fastapi.tiangolo.com/tutorial/bigger-applications/). ## Example ```python from fastapi import APIRouter, FastAPI

(
        self,
        router: Annotated["APIRouter", Doc("The `APIRouter` to include.")],
        *,
        prefix: Annotated[str, Doc("An optional path prefix for the router.")] = "",
        tags: Annotated[
            list[str | Enum] | None,
            Doc(
                """
                A list of tags to be applied to all the *path operations* in this
                router.

                It will be added to the generated OpenAPI (e.g. visible at `/docs`).

                Read more about it in the
                [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
                """
            ),
        ] = None,
        dependencies: Annotated[
            Sequence[params.Depends] | None,
            Doc(
                """
                A list of dependencies (using `Depends()`) to be applied to all the
                *path operations* in this router.

                Read more about it in the
                [FastAPI docs for Bigger Applications - Multiple Files](https://fastapi.tiangolo.com/tutorial/bigger-applications/#include-an-apirouter-with-a-custom-prefix-tags-responses-and-dependencies).
                """
            ),
        ] = None,
        default_response_class: Annotated[
            type[Response],
            Doc(
                """
                The default response class to be used.

                Read more in the
                [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/#default-response-class).
                """
            ),
        ] = Default(JSONResponse),
        responses: Annotated[
            dict[int | str, dict[str, Any]] | None,
            Doc(
                """
                Additional responses to be shown in OpenAPI.

                It will be added to the generated OpenAPI (e.g. visible at `/docs`).

                Read more about it in the
                [FastAPI docs for Additional Responses in OpenAPI](https://fastapi.tiangolo.com/advanced/additional-responses/).

                And in the
                [FastAPI docs for Bigger Applications](https://fastapi.tiangolo.com/tutorial/bigger-applications/#include-an-apirouter-with-a-custom-prefix-tags-responses-and-dependencies).
                """
            ),
        ] = None,
        callbacks: Annotated[
            list[BaseRoute] | None,
            Doc(
                """
                OpenAPI callbacks that should apply to all *path operations* in this
                router.

                It will be added to the generated OpenAPI (e.g. visible at `/docs`).

                Read more about it in the
                [FastAPI docs for OpenAPI Callbacks](https://fastapi.tiangolo.com/advanced/openapi-callbacks/).
                """
            ),
        ] = None,
        deprecated: Annotated[
            bool | None,
            Doc(
                """
                Mark all *path operations* in this router as deprecated.

                It will be added to the generated OpenAPI (e.g. visible at `/docs`).

                Read more about it in the
                [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
                """
            ),
        ] = None,
        include_in_schema: Annotated[
            bool,
            Doc(
                """
                Include (or not) all the *path operations* in this router in the
                generated OpenAPI schema.

                This affects the generated OpenAPI (e.g. visible at `/docs`).
                """
            ),
        ] = True,
        generate_unique_id_function: Annotated[
            Callable[[APIRoute], str],
            Doc(
                """
                Customize the function used to generate unique IDs for the *path
                operations* shown in the generated OpenAPI.

                This is particularly useful when automatically generating clients or
                SDKs for your API.

                Read more about it in the
                [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
                """
            ),
        ] = Default(generate_unique_id),
    )

Source from the content-addressed store, hash-verified

3080 return decorator
3081
3082 def include_router(
3083 self,
3084 router: Annotated["APIRouter", Doc("The `APIRouter` to include.")],
3085 *,
3086 prefix: Annotated[str, Doc("An optional path prefix for the router.")] = "",
3087 tags: Annotated[
3088 list[str | Enum] | None,
3089 Doc(
3090 """
3091 A list of tags to be applied to all the *path operations* in this
3092 router.
3093
3094 It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3095
3096 Read more about it in the
3097 [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
3098 """
3099 ),
3100 ] = None,
3101 dependencies: Annotated[
3102 Sequence[params.Depends] | None,
3103 Doc(
3104 """
3105 A list of dependencies (using `Depends()`) to be applied to all the
3106 *path operations* in this router.
3107
3108 Read more about it in the
3109 [FastAPI docs for Bigger Applications - Multiple Files](https://fastapi.tiangolo.com/tutorial/bigger-applications/#include-an-apirouter-with-a-custom-prefix-tags-responses-and-dependencies).
3110 """
3111 ),
3112 ] = None,
3113 default_response_class: Annotated[
3114 type[Response],
3115 Doc(
3116 """
3117 The default response class to be used.
3118
3119 Read more in the
3120 [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/#default-response-class).
3121 """
3122 ),
3123 ] = Default(JSONResponse),
3124 responses: Annotated[
3125 dict[int | str, dict[str, Any]] | None,
3126 Doc(
3127 """
3128 Additional responses to be shown in OpenAPI.
3129
3130 It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3131
3132 Read more about it in the
3133 [FastAPI docs for Additional Responses in OpenAPI](https://fastapi.tiangolo.com/advanced/additional-responses/).
3134
3135 And in the
3136 [FastAPI docs for Bigger Applications](https://fastapi.tiangolo.com/tutorial/bigger-applications/#include-an-apirouter-with-a-custom-prefix-tags-responses-and-dependencies).
3137 """
3138 ),
3139 ] = None,

Calls 9

_mark_routes_changedMethod · 0.95
add_event_handlerMethod · 0.95
DefaultFunction · 0.90
FastAPIErrorClass · 0.90
_IncludedRouterClass · 0.85
_merge_lifespan_contextFunction · 0.85
_contains_routerMethod · 0.80
for_includeMethod · 0.80