FastAPI State
Simple state management utilities for FastAPI applications.
This package provides a decorator-based approach to define and inject custom application state objects. It supports both synchronous and asynchronous state initializers and ensures that state keys are unique within a FastAPI application's lifecycle.
Features
- Define application-wide state using decorators
- Support for both sync and async state initializers
- Works with both HTTP and WebSocket routes.
Limitations
- When using a custom name with
@state('...'), mypy may require a# type: ignore[arg-type]comment due to type narrowing limitations.
Installation
# Install with pip
pip install fastapi-state
# Or add to your project
uv add fastapi-state
Example
See more usage examples.
from contextlib import asynccontextmanager
from typing import Annotated, Any
import uvicorn
from fastapi import Body, Depends, FastAPI, HTTPException, status
from fastapi_state import state
def main() -> None:
uvicorn.run(f'{__name__}:app')
type Database = dict[str, Any]
@state
def database() -> Database:
return {}
@asynccontextmanager
async def lifespan(_app: FastAPI): # noqa: ANN201
state: dict = {}
await database.inject(state)
yield state
DatabaseDep = Annotated[Database, Depends(database.extract)]
app = FastAPI(lifespan=lifespan)
@app.put('/{key}')
async def put_item(key: str, value: Annotated[Any, Body()], db: DatabaseDep) -> Any: # noqa: ANN401
db[key] = value
return value
@app.get('/{key}')
async def get_item(key: str, db: DatabaseDep) -> Any: # noqa: ANN401
if key in db:
return db[key]
raise HTTPException(status.HTTP_404_NOT_FOUND)
License
This project is licensed under the MIT License.
Metadata
Release files for fastapi-state 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi_state-0.2.0.tar.gz | 4.2 kB | With provenance | Details for fastapi_state-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_state-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for fastapi_state-0.2.0-py3-none-any.whl |
Total release size: 9.4 kB
Release files / fastapi_state-0.2.0.tar.gz
| Download URL | fastapi_state-0.2.0.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c6231b00a002b3e400b9e73beea73ebf2c11a461537b8ad01235fc0d758a71ae
|
|
BLAKE2b-256 checksum How to use checksums |
49d133b8be8a659a4148c4559dc09067f0450e599ca040db780fbf57c007073a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 27, 2025.
Transparency logRelease files / fastapi_state-0.2.0-py3-none-any.whl
| Download URL | fastapi_state-0.2.0-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e96e17cdfc4d2ee6d8455ed0eb1815d1e47b27118dc6f361709dbbc770f13111
|
|
BLAKE2b-256 checksum How to use checksums |
3e8776ca0d601e355df19e1196510872b973e20a2212140852b912f56c07dd14
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 27, 2025.
Transparency logSecurity for fastapi-state 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for fastapi_state-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 27, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.