Skip to main content

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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_globals-0.3.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_globals-0.3.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_globals-0.3.1.tar.gz.

File metadata

  • Download URL: fastapi_globals-0.3.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for fastapi_globals-0.3.1.tar.gz
Algorithm Hash digest
SHA256 a84487cd92d368e236a417509d554bbc00fb2c4557e36731d8caa72417aa5918
MD5 3e607ebd4e1c75e47cc28347b03049e6
BLAKE2b-256 138a31122aeace68f482c1c5d0b902b011e1ff1c41924b53a390a21017bdc5b7

See more details on using hashes here.

File details

Details for the file fastapi_globals-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: fastapi_globals-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for fastapi_globals-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f0ce002b5efa5f922e9d601e9a00b6200a468583d875072ca979736a0657c606
MD5 57e40e4883a435adb6c7a3c7a4d613f9
BLAKE2b-256 b93381c70340b0da7b48ab168fa9f60f6fae48ebf3eb8637f3c4497b94dc287a

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 files

0.2.1

2 files

0.2.0

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page