https://github.com/shearichard/fastapi_session
How do session work with FastAPI ?
https://github.com/shearichard/fastapi_session
fastapi python sessions starlette starlette-middleware
Last synced: about 2 months ago
JSON representation
How do session work with FastAPI ?
- Host: GitHub
- URL: https://github.com/shearichard/fastapi_session
- Owner: shearichard
- Created: 2025-03-29T08:49:43.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-29T09:14:47.000Z (over 1 year ago)
- Last Synced: 2025-03-29T10:23:30.247Z (over 1 year ago)
- Topics: fastapi, python, sessions, starlette, starlette-middleware
- Language: Python
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Using Sessions in FastAPI
A project to experiment with the use of sessions from within FastAPI by using the Starlette middleware - https://www.starlette.io/middleware/#sessionmiddleware .
The aim here is to replicate parts of the functionality of the Django Sessions facility, as it says in the [Django Sessions doco](https://docs.djangoproject.com/en/5.1/topics/http/sessions/) - _"The session framework lets you store and retrieve arbitrary data on a per-site-visitor basis. It stores data on the server side and abstracts the sending and receiving of cookies."_.
## Starting the server
### VirtualEnv Initialization
From the root of the project execute ...
```
$ pipenv shell
```
### Start Server
From the root of the project execute ...
```
uvicorn src.main:app --reload
```
## Environment Variables
Environment variables specific to this project are defined in the .envrc file, this is not committed to the repos however the .envrc_TEMPLATE provides guidance on the contents of .envrc so that it can be recreated when necessary.
## Secret Key
One way to generate the secret key needed to use the Session functionality (and which is stored in the uncommitted .direnv) is this
```
python3 -c 'import os; print(os.urandom(16).hex())'
```