fastapi-redis-session
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
SessionStoragenow usesredis.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(...)ordeleteSession(...)withoutawaitmust 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),
)
Metadata
Release files for fastapi-redis-session 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fastapi_redis_session-0.3.1.tar.gz | 6.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_redis_session-0.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.9 kB
Release files / fastapi_redis_session-0.3.1.tar.gz
| Download URL | fastapi_redis_session-0.3.1.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
80b2448b2cb93023a494752495afe42b60f5cec9bbca2846023f2365225cce6b
|
|
BLAKE2b-256 checksum How to use checksums |
c0f16ccf1a2bc37e611279ee1978cdeb1f974a9c2242658911b579651c109429
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Release files / fastapi_redis_session-0.3.1-py3-none-any.whl
| Download URL | fastapi_redis_session-0.3.1-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
54e90d4bb03b826e67c2b798154da166811f1c03e327d3e891ad6b062f034b42
|
|
BLAKE2b-256 checksum How to use checksums |
a776819740367e9f20c1c3f7b50a9f0632849eb7f0dc4f51ab7c392dc67875fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|