> ## Documentation Index
> Fetch the complete documentation index at: https://docs.westyx.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Python SDK - Framework adapters

> Django, FastAPI and Flask integrations: settings, lifecycle and logging.

The Python SDK ships with three optional adapters for the most common Python web frameworks. Each is installable as a pip extra and provides a thin layer that handles the singleton lifecycle for you.

| Framework | Extra | Module | Client type |
| - | - | - | - |
| Django | `[django]` | `westyx_nexus.django` | `NexusClient` (sync) |
| FastAPI | `[fastapi]` | `westyx_nexus.fastapi` | `AsyncNexusClient` |
| Flask | `[flask]` | `westyx_nexus.flask` | `NexusClient` (sync) |

If you don't use any of these frameworks, ignore this page - the core `NexusClient` / `AsyncNexusClient` work everywhere.

## Django

Install with the extra:

```sh theme={null}
pip install "westyx-nexus-sdk[django]"
```

`settings.py`:

```python theme={null}
INSTALLED_APPS = [
    # ...
    "westyx_nexus.django",
]

NEXUS_BASE_URL = "https://blue-ocean-a5rx7.westyx.dev"
NEXUS_API_KEY  = "wxs_..."

# Optional:
# NEXUS_WIF         = {"enabled": True, "provider": "kubernetes"}
# NEXUS_TTL_SECONDS = 60.0
# NEXUS_STREAM      = True
# NEXUS_LOGGER      = my_logger_instance
```

Every setting is optional. `NEXUS_BASE_URL` falls back to `WESTYX_SERVICE_ENDPOINT`; `NEXUS_WIF` takes a `WIFConfig` or a mapping of its fields (`{"enabled": True, "provider": "kubernetes"}`). With neither `NEXUS_API_KEY` nor `NEXUS_WIF` the adapter uses Workload Identity Federation with the default provider, which finds the session `westyx dev setup` wrote - so under `westyx dev run` it needs no Nexus settings at all.

The `AppConfig.ready()` hook performs the blocking initial sync at Django startup, so by the time any view is called the cache is populated.

`views.py`:

```python theme={null}
from westyx_nexus.django import get_client

def homepage(request):
    nexus = get_client()
    if nexus.get_flag("new.homepage"):
        return render(request, "homepage_v2.html")
    return render(request, "homepage.html")
```

The client is closed automatically when the Django process exits (via `atexit`).

## FastAPI

Install with the extra:

```sh theme={null}
pip install "westyx-nexus-sdk[fastapi]"
```

Use the `attach_nexus` lifespan helper:

```python theme={null}
from contextlib import asynccontextmanager
from fastapi import Depends, FastAPI
from westyx_nexus import AsyncNexusClient, NexusConfig
from westyx_nexus.fastapi import attach_nexus, get_nexus


@asynccontextmanager
async def lifespan(app: FastAPI):
    async with attach_nexus(app, NexusConfig(
        base_url="https://blue-ocean-a5rx7.westyx.dev",
        api_key="wxs_...",
    )):
        yield


app = FastAPI(lifespan=lifespan)


@app.get("/checkout")
async def checkout(nexus: AsyncNexusClient = Depends(get_nexus)):
    if nexus.get_flag("checkout.v2"):
        return {"version": "v2"}
    return {"version": "v1"}
```

The lifespan starts the client (blocking initial sync) before the app accepts requests, and closes it on shutdown. `get_nexus` is a standard FastAPI dependency - inject it into any handler.

## Flask

Install with the extra:

```sh theme={null}
pip install "westyx-nexus-sdk[flask]"
```

```python theme={null}
from flask import Flask, render_template
from westyx_nexus.flask import NexusExtension, get_nexus

app = Flask(__name__)
app.config["NEXUS_BASE_URL"] = "https://blue-ocean-a5rx7.westyx.dev"
app.config["NEXUS_API_KEY"]  = "wxs_..."

# Optional:
# app.config["NEXUS_WIF"]         = {"enabled": True, "provider": "kubernetes"}
# app.config["NEXUS_TTL_SECONDS"] = 60.0
# app.config["NEXUS_STREAM"]      = True
# app.config["NEXUS_LOGGER"]      = my_logger_instance

NexusExtension(app)


@app.route("/")
def home():
    nexus = get_nexus()
    if nexus.get_flag("new.homepage"):
        return render_template("homepage_v2.html")
    return render_template("homepage.html")
```

Two patterns are supported (Flask convention):

* **One-shot:** `NexusExtension(app)` - create at module level alongside the app
* **Factory:** `ext = NexusExtension(); ext.init_app(app)` - for the application-factory pattern

Every setting is optional, as for Django.

The client is closed automatically when the Flask process exits (via `atexit`).

## Logging

The Django and Flask adapters log through `StdlibNexusLogger()` - the `westyx_nexus` logger - unless
`NEXUS_LOGGER` names another, and FastAPI's `attach_nexus` takes whatever `logger` the `NexusConfig`
carries, the same logger by default. So the SDK's records, the
[startup summary](https://docs.westyx.dev/sdks/python/logging.md#the-startup-summary) among them, appear wherever your
application's logging configuration sends `westyx_nexus` at `INFO` and above.

## Framework settings come from the environment, not from Nexus

`DJANGO_SETTINGS_MODULE` and the settings Flask reads from its environment are read when the
framework starts, before the Nexus integration loads, so a Nexus config of that name changes
nothing. Set them in the deployment.

## Manual (no adapter)

If you don't use any of the supported frameworks (or you're using one in a way the adapter doesn't fit), you can always instantiate the client directly:

```python theme={null}
from westyx_nexus import NexusClient, NexusConfig

# Module-level singleton - created once, used everywhere.
nexus = NexusClient.create(NexusConfig(
    base_url="...",
    api_key="...",
))
```

Just remember to call `nexus.close()` on shutdown (signal handler, atexit, or `with` block).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.