Dependencies -Depends() andSecurity()¶
Depends()¶
Dependencies are handled mainly with the special functionDepends() that takes a callable.
Here is the reference for it and its parameters.
You can import it directly fromfastapi:
fromfastapiimportDependsfastapi.Depends¶
Depends(dependency=None,*,use_cache=True,scope=None)Declare a FastAPI dependency.
It takes a single "dependable" callable (like a function).
Don't call it directly, FastAPI will call it for you.
Read more about it in theFastAPI docs for Dependencies.
Example
fromtypingimportAnnotatedfromfastapiimportDepends,FastAPIapp=FastAPI()asyncdefcommon_parameters(q:str|None=None,skip:int=0,limit:int=100):return{"q":q,"skip":skip,"limit":limit}@app.get("/items/")asyncdefread_items(commons:Annotated[dict,Depends(common_parameters)]):returncommons| PARAMETER | DESCRIPTION |
|---|---|
dependency | A "dependable" callable (like a function). Don't call it directly, FastAPI will call it for you, just pass the objectdirectly. Read more about it in theFastAPI docs for Dependencies TYPE: |
use_cache | By default, after a dependency is called the first time in a request, ifthe dependency is declared again for the rest of the request (for exampleif the dependency is needed by several dependencies), the value will bere-used for the rest of the request. Set Read more about it in theFastAPI docs about sub-dependencies TYPE: |
scope | Mainly for dependencies with
Read more about it in theFastAPI docs for FastAPI Dependencies with yield TYPE: |
Source code infastapi/param_functions.py
228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370 | |
Security()¶
For many scenarios, you can handle security (authorization, authentication, etc.) with dependencies, usingDepends().
But when you want to also declare OAuth2 scopes, you can useSecurity() instead ofDepends().
You can importSecurity() directly fromfastapi:
fromfastapiimportSecurityfastapi.Security¶
Security(dependency=None,*,scopes=None,use_cache=True)Declare a FastAPI Security dependency.
The only difference with a regular dependency is that it can declare OAuth2scopes that will be integrated with OpenAPI and the automatic UI docs (by defaultat/docs).
It takes a single "dependable" callable (like a function).
Don't call it directly, FastAPI will call it for you.
Read more about it in theFastAPI docs for Security andin theFastAPI docs for OAuth2 scopes.
Example
fromtypingimportAnnotatedfromfastapiimportSecurity,FastAPIfrom.dbimportUserfrom.securityimportget_current_active_userapp=FastAPI()@app.get("/users/me/items/")asyncdefread_own_items(current_user:Annotated[User,Security(get_current_active_user,scopes=["items"])]):return[{"item_id":"Foo","owner":current_user.username}]| PARAMETER | DESCRIPTION |
|---|---|
dependency | A "dependable" callable (like a function). Don't call it directly, FastAPI will call it for you, just pass the objectdirectly. Read more about it in theFastAPI docs for Dependencies TYPE: |
scopes | OAuth2 scopes required for thepath operation that uses this Securitydependency. The term "scope" comes from the OAuth2 specification, it seems to beintentionally vague and interpretable. It normally refers to permissions,in cases to roles. These scopes are integrated with OpenAPI (and the API docs at Read more about it in theFastAPI docs about OAuth2 scopes TYPE: |
use_cache | By default, after a dependency is called the first time in a request, ifthe dependency is declared again for the rest of the request (for exampleif the dependency is needed by several dependencies), the value will bere-used for the rest of the request. Set Read more about it in theFastAPI docs about sub-dependencies TYPE: |
Source code infastapi/param_functions.py
23732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461 | |







