Skip to main content

Use request global variables in FastAPI

Project description

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 and 3.13. 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.

Project details


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.2.1.tar.gz (3.4 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.2.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_globals-0.2.1.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.24

File hashes

Hashes for fastapi_globals-0.2.1.tar.gz
Algorithm Hash digest
SHA256 43dfef9e3154c54916df2cb7108f2e9f725f102769c9905f96c1b468d66f9dee
MD5 f4caedca95de9057b28d6f7b9085f9b9
BLAKE2b-256 06cbe760a1c0ef8d942aa58d37f96d471e09bc992a77449fd8dbfc8cdf9f5cb2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_globals-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a1435ec21c477d3dd9a5e22429c6b1d8d8a0864bea2d821f3bb0dffbed42bc41
MD5 e5a300d6f4ee9d1c19583bd7d526c49e
BLAKE2b-256 0a87ed82ac6bc70e22d6df36568215e4c4c1a3d7f201ab9f05e0764991c4fde7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page