Dependencies - Depends() and Security()¶
Depends()¶
Dependencies are handled mainly with the special function Depends() that takes a callable.
Here is the reference for it and its parameters.
You can import it directly from fastapi:
from fastapi import Depends
fastapi.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 the FastAPI docs for Dependencies.
Example
from typing import Annotated
from fastapi import Depends, FastAPI
app = FastAPI()
async def common_parameters(q: str | None = None, skip: int = 0, limit: int = 100):
return {"q": q, "skip": skip, "limit": limit}
@app.get("/items/")
async def read_items(commons: Annotated[dict, Depends(common_parameters)]):
return commons
| PARAMETER | DESCRIPTION |
|---|---|
dependency
|
A "dependable" callable (like a function). Don't call it directly, FastAPI will call it for you, just pass the object directly. Read more about it in the FastAPI docs for Dependencies
TYPE:
|
use_cache
|
By default, after a dependency is called the first time in a request, if the dependency is declared again for the rest of the request (for example if the dependency is needed by several dependencies), the value will be re-used for the rest of the request. Set Read more about it in the FastAPI docs about sub-dependencies
TYPE:
|
scope
|
Mainly for dependencies with
Read more about it in the FastAPI docs for FastAPI Dependencies with yield
TYPE:
|
Source code in fastapi/param_functions.py
2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 | |
Security()¶
For many scenarios, you can handle security (authorization, authentication, etc.) with dependencies, using Depends().
But when you want to also declare OAuth2 scopes, you can use Security() instead of Depends().
You can import Security() directly from fastapi:
from fastapi import Security
fastapi.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 OAuth2
scopes that will be integrated with OpenAPI and the automatic UI docs (by default
at /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 the FastAPI docs for Security and in the FastAPI docs for OAuth2 scopes.
Example
from typing import Annotated
from fastapi import Security, FastAPI
from .db import User
from .security import get_current_active_user
app = FastAPI()
@app.get("/users/me/items/")
async def read_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 object directly. Read more about it in the FastAPI docs for Dependencies
TYPE:
|
scopes
|
OAuth2 scopes required for the path operation that uses this Security dependency. The term "scope" comes from the OAuth2 specification, it seems to be intentionally 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 the FastAPI docs about OAuth2 scopes
TYPE:
|
use_cache
|
By default, after a dependency is called the first time in a request, if the dependency is declared again for the rest of the request (for example if the dependency is needed by several dependencies), the value will be re-used for the rest of the request. Set Read more about it in the FastAPI docs about sub-dependencies
TYPE:
|
Source code in fastapi/param_functions.py
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 | |