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

231
Views
How to set order of Middlewares in FastAPI?

So I am using this open-source available MDW: https://github.com/snok/asgi-correlation-id which essentially just generates and adds a unique Correlation ID for incoming requests in FastAPI app.

However, I have created another custom middleware for my app, this way:

@app.middleware("http")
async def some_custom_mdw_function(request: Request, call_next):
    # some operation  I do here
    # which also requires me fetching the 
    # correlation_id created via the asgi-correlation-id
    # I have code line over here like:

    asgi_corr_id = correlation_id.get() # a function imported from asgi-correlation-id library

This however gives me an error sometimes, since apparently, those times, this some_custom_mdw_function MDW is called before the asgi-correlation-id MDW, and hence the correlation ID is not found.

How do I ensure that asgi-correlation-id MDW is called always before some_custom_mdw_function MDW? Is there a way to set the order of executions for MDW in FastAPI?

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!