Skip to main content

A redis-based session backend for Fastapi apps

Project description

fastapi-redis-session

CI

A redis-based session backend for Fastapi apps

Install

pip install -U fastapi-redis-session

Requires Python 3.12 or newer.

The public API is async-only and uses redis.asyncio.

Development

uv sync --group dev
uv run pytest

Use

from typing import Any

from fastapi import Depends, FastAPI, Request, Response

from fastapi_redis_session import deleteSession, getSession, getSessionId, getSessionStorage, setSession, SessionStorage

app = FastAPI(title=__name__)


@app.post("/setSession")
async def _setSession(
    request: Request, response: Response, sessionStorage: SessionStorage = Depends(getSessionStorage)
):
    sessionData = await request.json()
    await setSession(response, sessionData, sessionStorage)


@app.get("/getSession")
async def _setSession(session: Any = Depends(getSession)):
    return session


@app.post("/deleteSession")
async def _deleteSession(
    sessionId: str = Depends(getSessionId), sessionStorage: SessionStorage = Depends(getSessionStorage)
):
    await deleteSession(sessionId, sessionStorage)
    return None

getSession is an async dependency, and setSession / deleteSession must be awaited inside async route handlers.

Migration notes

  • SessionStorage now uses redis.asyncio.Redis.
  • Session access is no longer exposed through sync magic methods; use the async helper functions or awaitable storage methods instead.
  • Existing code that called setSession(...) or deleteSession(...) without await must be updated.

Config

Deafult Config

  • url of Redis: redis://localhost:6379/0
  • name of sessionId: ssid
  • generator function of sessionId: lambda :uuid.uuid4().hex
  • expire time of session in redis: 6 hours

Custom Config

from fastapi_redis_session.config import basicConfig
basicConfig(
    redisURL="redis://localhost:6379/1",
    sessionIdName="sessionId",
    sessionIdGenerator=lambda: str(random.randint(1000, 9999)),
    expireTime=timedelta(days=1),
    )

Project details


Download files

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

Source Distribution

fastapi_redis_session-0.3.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_redis_session-0.3.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_redis_session-0.3.0.tar.gz.

File metadata

  • Download URL: fastapi_redis_session-0.3.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastapi_redis_session-0.3.0.tar.gz
Algorithm Hash digest
SHA256 d5645b922e9279b8fdcd8792834643a77435becd6159224796b49fe8f8eb64ac
MD5 6f7c7ad82b71fcff1b804a689c5a7e92
BLAKE2b-256 ad59f3cc671d1179ccc568b62ee9f852fee21a43d6fc89c49c434e1d66007864

See more details on using hashes here.

File details

Details for the file fastapi_redis_session-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: fastapi_redis_session-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastapi_redis_session-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9c854b86c5e087b5b7120f74eaef7159f3a2275c6545e3e90ef2753f71a53bd8
MD5 bab6368a6c570f67644d5ecf2babc751
BLAKE2b-256 b13a687b8abc013052db449e407266fe519981993bca8ed17c59a30b60dc4ba5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page