Advanced Dependencies

    The current page still doesn’t have a translation for this language.

    But you can help translating it: Contributing.

    All the dependencies we have seen are a fixed function or class.

    But there could be cases where you want to be able to set parameters on the dependency, without having to declare many different functions or classes.

    Let’s imagine that we want to have a dependency that checks if the query parameter contains some fixed content.

    But we want to be able to parameterize that fixed content.

    Not the class itself (which is already a callable), but an instance of that class.

    To do that, we declare a method __call__:

    In this case, this __call__ is what FastAPI will use to check for additional parameters and sub-dependencies, and this is what will be called to pass a value to the parameter in your path operation function later.

    And now, we can use __init__ to declare the parameters of the instance that we can use to “parameterize” the dependency:

    In this case, FastAPI won’t ever touch or care about , we will use it directly in our code.

    We could create an instance of this class with:

    Then, we could use this checker in a , instead of Depends(FixedContentQueryChecker), because the dependency is the instance, checker, not the class itself.

    And when solving the dependency, FastAPI will call this checker like:

    …and pass whatever that returns as the value of the dependency in our path operation function as the parameter :

    Tip

    All this might seem contrived. And it might not be very clear how is it useful yet.

    These examples are intentionally simple, but show how it all works.

    If you understood all this, you already know how those utility tools for security work underneath.