Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

315
Views
FastAPI : Retrieving variable from group of path operation dependency

I would like to manage a custom authorization system in my FastAPI app.

To achieve this, I would like to use group path operations dependencies on concerned routes to ensure the auth token is present and valid. I followed this guide in documentation that works well.

But since my business logic is more complicated, I also would like to be able to use the API token in my controlers to perform additional rules.

The tip in this section states that Note that, much like dependencies in path operation decorators, no value will be passed to your path operation function.

Which means, if the auth token is defined as a dependency for a group of path operations, there's no way to retrieve its value in the controlers.

Does it mean that I necessarily have to use single dependencies in each path operation function by adding them as a parameter, or is there another way of doing this in a more DRY way ? I know I can retrieve the header within request.headers, but is it how it's supposed to be done ?

One way would look like this :

router = APIRouter()

@router.get('/test')
async def test(request: Request, token: str = Depends(check_token)):
    token  # directly available

While the other is :

router = APIRouter(dependencies=[Depends(check_token)])

@router.get('/test')
async def test(request: Request):
    token = request.headers['auth']  # need some work around to retrieve token
    ...
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!