Home / Function/ __init__() — fastapi Function Reference

__init__() — fastapi Function Reference

Architecture documentation for the __init__() function in oauth2.py from the fastapi codebase.

Entity Profile

Dependency Diagram

graph TD
  9546380c_4ae9_5a79_392c_8b310d60d875["__init__()"]
  530dd5bc_4096_0b36_b4c7_3b84ace8a699["OAuth2PasswordBearer"]
  9546380c_4ae9_5a79_392c_8b310d60d875 -->|defined in| 530dd5bc_4096_0b36_b4c7_3b84ace8a699
  style 9546380c_4ae9_5a79_392c_8b310d60d875 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

fastapi/security/oauth2.py lines 442–534

    def __init__(
        self,
        tokenUrl: Annotated[
            str,
            Doc(
                """
                The URL to obtain the OAuth2 token. This would be the *path operation*
                that has `OAuth2PasswordRequestForm` as a dependency.

                Read more about it in the
                [FastAPI docs for Simple OAuth2 with Password and Bearer](https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/).
                """
            ),
        ],
        scheme_name: Annotated[
            Optional[str],
            Doc(
                """
                Security scheme name.

                It will be included in the generated OpenAPI (e.g. visible at `/docs`).
                """
            ),
        ] = None,
        scopes: Annotated[
            Optional[dict[str, str]],
            Doc(
                """
                The OAuth2 scopes that would be required by the *path operations* that
                use this dependency.

                Read more about it in the
                [FastAPI docs for Simple OAuth2 with Password and Bearer](https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/).
                """
            ),
        ] = None,
        description: Annotated[
            Optional[str],
            Doc(
                """
                Security scheme description.

                It will be included in the generated OpenAPI (e.g. visible at `/docs`).
                """
            ),
        ] = None,
        auto_error: Annotated[
            bool,
            Doc(
                """
                By default, if no HTTP Authorization header is provided, required for
                OAuth2 authentication, it will automatically cancel the request and
                send the client an error.

                If `auto_error` is set to `False`, when the HTTP Authorization header
                is not available, instead of erroring out, the dependency result will
                be `None`.

                This is useful when you want to have optional authentication.

                It is also useful when you want to have authentication that can be
                provided in one of multiple optional ways (for example, with OAuth2
                or in a cookie).
                """
            ),
        ] = True,
        refreshUrl: Annotated[
            Optional[str],
            Doc(
                """
                The URL to refresh the token and obtain a new one.
                """
            ),
        ] = None,
    ):
        if not scopes:
            scopes = {}
        flows = OAuthFlowsModel(
            password=cast(
                Any,
                {

Domain

Subdomains

Frequently Asked Questions

What does __init__() do?
__init__() is a function in the fastapi codebase, defined in fastapi/security/oauth2.py.
Where is __init__() defined?
__init__() is defined in fastapi/security/oauth2.py at line 442.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free