A redis-based session backend for Fastapi apps
Project description
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),
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_redis_session-0.3.1.tar.gz.
File metadata
- Download URL: fastapi_redis_session-0.3.1.tar.gz
- Upload date:
- Size: 6.2 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80b2448b2cb93023a494752495afe42b60f5cec9bbca2846023f2365225cce6b
|
|
| MD5 |
528e0302bfe98f7bb7ff28dfa955ec2d
|
|
| BLAKE2b-256 |
c0f16ccf1a2bc37e611279ee1978cdeb1f974a9c2242658911b579651c109429
|
File details
Details for the file fastapi_redis_session-0.3.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_redis_session-0.3.1-py3-none-any.whl
- Upload date:
- Size: 4.7 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54e90d4bb03b826e67c2b798154da166811f1c03e327d3e891ad6b062f034b42
|
|
| MD5 |
b2a68495652559d3110b0fa8622531f2
|
|
| BLAKE2b-256 |
a776819740367e9f20c1c3f7b50a9f0632849eb7f0dc4f51ab7c392dc67875fa
|