"""icon" sizes="16x16" href="_static/icon/favicon-16x16.png" type="image/png"> """icon" sizes="32x32" href="_static/icon/favicon-32x32.png" type="image/png"> """icon" sizes="48x48" href="_static/icon/favicon-48x48.png" type="image/png"> """icon" sizes="192x192" href="_static/icon/favicon-192x192.png" type="image/png"> """icon" sizes="512x512" href="_static/icon/favicon-512x512.png" type="image/png"> """apple-touch-icon" sizes="180x180" href="_static/icon/apple-touch-icon-180x180.png" type="image/png"> """mask-icon" href="_static/icon/safari-pinned-tab.svg" type="image/svg+xml">"""search" title="Search" href="search.html">"""next" title="Configuration" href="config.html">"""prev" title="Installation" href="installation.html"> """prefetch" href="_static/logo/logo-light.png" as="image"> """prefetch" href="_static/logo/logo-dark.png" as="image"> """stylesheet" type="text/css" href="_static/pygments.css?v=d111a655" /> """stylesheet" type="text/css" href="_static/styles/furo.css?v=4db3a74f" /> """stylesheet" type="text/css" href="_static/styles/furo-extensions.css?v=99069b78" /> """stylesheet" type="text/css" href="_static/styles.css?v=6deb019a" /> """stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/fontawesome.min.css" /> """stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/solid.min.css" /> """stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/brands.min.css" />
Skip to content
Flask-Session is switching serializer to msgspec in 1.0.0. Version 0.7.0 will migrate existing sessions upon read or write.
Back to top
View this page
Edit this page

# Usage

## Quickstart

Create your `Flask` application, load the configuration of choice, and then create the `Session` object by passing it the application.

    from flask import Flask, session
    from flask_session import Session

    app = Flask(__name__)

    SESSION_TYPE = 'redis'
    SESSION_REDIS = Redis(host='localhost', port=6379)
    app.config.from_object(__name__)
    Session(app)

    @app.route('/set/')
    def set():
        session['key'] = 'value'
        return 'ok'

    @app.route('/get/')
    def get():
        return session.get('key', 'not set')

See the configuration section for more details.

Note

You can not use `Session` instance directly, what `Session` does is just change the `session_interface` attribute on your Flask applications. You should always use `flask.session` when accessing or modifying the current session.

## Alternative initialization

Rather than calling `Session`, you may initialize later using `init_app()`.

    ...
    sess = Session()
    sess.init_app(app)

Or, if you prefer to directly set parameters rather than using the configuration constants, you can initialize by setting an instance of `flask_session.redis.RedisSessionInterface` directly to the `flask.Flask.session_interface`.

    from flask import Flask, session
    from flask_session.redis import RedisSessionInterface
    from redis import Redis

    app = Flask(__name__)

    redis = Redis(host='localhost', port=6379)
    app.session_interface = RedisSessionInterface(client=redis)

## Using CacheLib as a session backend

Note

FileSystemSession was recently deprecated in favor of CacheLib, which is what is was using under the hood.

The following example demonstrates how to use CacheLib as a session backend with the file system cache. This might be useful for rapid development or testing.

    from flask import Flask, session
    from flask_session import Session
    from cachelib.file import FileSystemCache

    app = Flask(__name__)

    SESSION_TYPE = 'cachelib'
    SESSION_SERIALIZATION_FORMAT = 'json'
    SESSION_CACHELIB = FileSystemCache(threshold=500, cache_dir="/sessions"),
    app.config.from_object(__name__)
    Session(app)