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

391
Views
session object in Fastapi similar to flask

I am trying to use session to pass variables across view functions in fastapi. However, I do not find any doc which specifically says of about session object. Everywhere I see, cookies are used. Is there any way to convert the below flask code in fastapi? I want to keep session implementation as simple as possible.

from flask import Flask, session, render_template, request, redirect, url_for


app=Flask(__name__)
app.secret_key='asdsdfsdfs13sdf_df%&'   

@app.route('/a')
def a():
    session['my_var'] = '1234'              
    return redirect(url_for('b'))          


@app.route('/b')
def b():
    my_var = session.get('my_var', None)
    return my_var    


if __name__=='__main__':
    app.run(host='0.0.0.0', port=5000, debug = True)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Take a look at Starlette's SessionMiddleware. FastAPI uses Starlette under the hood so it is compatible.

After you register SessionMiddleware, you can access Request.session, which is a dictionary.

Documentation: SessionMiddleware

An implementation in FastAPI may look like:

@app.route("/a")
async def a(request: Request) -> RedirectResponse:

    request.session["my_var"] = "1234"

    return RedirectResponse("/b")

@app.route("/b")
async def b(request: Request) -> PlainTextResponse:

    my_var = request.session.get("my_var", None)

    return PlainTextResponse(my_var)
over 4 years ago · Santiago Trujillo Report
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!