fastapi-globals
Allows you to use request global values. Can be used to store request speficic data, like state, database connection, etc.
Installation
Just use pip install fastapi-globals to install the library.
Note: fastapi-globals is compatible with fastapi versions 0.100.0 and later on Python 3.10,
3.11, 3.12, 3.13 and 3.14. This is also ensured running all tests on all those versions
using tox.
Setup
Add the GlobalsMiddleware to your app:
app = fastapi.FastAPI(
title="Your app API",
)
app.add_middleware(GlobalsMiddleware) # <-- This line is necessary
Usage
Import g and then access (set/get) attributes of it:
from fastapi_globals import g
g.foo = "foo"
# In some other code
assert g.foo == "foo"
Best way to utilize the global g in your code is to set the desired
value in a FastAPI dependency, like so:
async def set_global_foo() -> None:
g.foo = "foo"
@app.get("/test/", dependencies=[Depends(set_global_foo)])
async def test():
assert g.foo == "foo"
Default values
You may use g.set_default("name", some_value) to set a default value
for a global variable. This default value will then be used instead of None
when the variable is accessed before it was set.
Note that default values should only be set at startup time, never
inside dependencies or similar. Otherwise you may run into the issue that
the value was already used any thus have a value of None set already, which
would result in the default value not being used.
Metadata
Release files for fastapi-globals 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 | Provenance | |
|---|---|---|---|---|
| fastapi_globals-0.3.1.tar.gz | 3.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_globals-0.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.5 kB
Release files / fastapi_globals-0.3.1.tar.gz
| Download URL | fastapi_globals-0.3.1.tar.gz |
|---|---|
| Size | 3.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a84487cd92d368e236a417509d554bbc00fb2c4557e36731d8caa72417aa5918
|
|
BLAKE2b-256 checksum How to use checksums |
138a31122aeace68f482c1c5d0b902b011e1ff1c41924b53a390a21017bdc5b7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","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_globals-0.3.1-py3-none-any.whl
| Download URL | fastapi_globals-0.3.1-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f0ce002b5efa5f922e9d601e9a00b6200a468583d875072ca979736a0657c606
|
|
BLAKE2b-256 checksum How to use checksums |
b93381c70340b0da7b48ab168fa9f60f6fae48ebf3eb8637f3c4497b94dc287a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","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}
|
Security for fastapi-globals 0.3.1
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A 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.