Skip to main content

Provides server-backed sessions for Sanic using Redis, Memcache and more.

Project description

Sanic session management for humans

Build Status ReadTheDocs License: MIT PyPI version Black

sanic_session is session management extension for Sanic that integrates server-backed sessions with most convenient API.

sanic_session provides a number of session interfaces for you to store a client's session data. The interfaces available right now are:

  • Redis (supports both drivers aioredis and asyncio_redis)
  • Memcache (via aiomcache)
  • Mongodb (via sanic_motor and pymongo)
  • In-Memory (suitable for testing and development environments)


Install with pip (there is other options for different drivers, check documentation):

pip install sanic_session

or if you prefer Pipenv:

pipenv install sanic_session


Documentation is available at

Also, make sure you read OWASP's Session Management Cheat Sheet for some really useful info on session management.


A simple example uses the in-memory session interface.

from sanic import Sanic
from sanic.response import text
from sanic_session import Session, InMemorySessionInterface

app = Sanic()
session = Session(app, interface=InMemorySessionInterface())

async def index(request):
    # interact with the session like a normal dict
    if not request['session'].get('foo'):
        request['session']['foo'] = 0

    request['session']['foo'] += 1

    return text(request['session']['foo'])

if __name__ == "__main__":"", port=8000, debug=True)

Examples of using redis and memcache backed sessions can be found in the documentation, under Using the Interfaces.

— ⭐️ —

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for sanic-session, version 0.6.0
Filename, size File type Python version Upload date Hashes
Filename, size sanic_session-0.6.0-py3-none-any.whl (12.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size sanic_session-0.6.0.tar.gz (9.6 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page