Docs/Integrations/Python / FastAPI

Python / FastAPI Integration

Observe FastAPI services and any standard Python ASGI/WSGI app.

The NxtOne Python SDK runs inside your Python process and sends behavioral events straight to the NxtOne platform over HTTPS — there is no separate process, container, or daemon to run. It works with web frameworks (FastAPI, Flask, Django) and background workers (Celery) to trace requests and code execution.

1. Install the package

Install the SDK with pip. It requires Python 3.10+.

bash
pip install nxtone

2. Configuration

Set your API key and a project ID for this service as environment variables:

bash
export NXTONE_API_KEY="<your-api-key>" export NXTONE_PROJECT_ID="user-service"
VariableDefaultDescription
NXTONE_API_KEYrequiredWithout it the SDK does nothing (it prints a hint to stderr) and never fails your app.
NXTONE_PROJECT_IDProject folder nameGroups execution flows in the UI. Set it explicitly — the folder name is often the same across services (for example app in containers).
NXTONE_EXCLUDE—Packages or paths to leave uninstrumented.
NXTONE_LOG_LEVELwarnSDK log verbosity on stderr.
NXTONE_SHUTDOWN_TIMEOUT60 sHow long the SDK waits to flush buffered events at shutdown. See Shutdown below.

You can also keep settings in a nxtone.toml file in your project root, or in ~/.nxtone/config.toml, both under an [nxtone] table. Environment variables override the project file, which overrides the home-directory file:

toml
[nxtone] project_id = "user-service" # Keep the API key out of version control: set NXTONE_API_KEY in the environment.

3. Decorate your entry point

Activate the SDK by decorating the function that starts your application with @nxtone.NxtOneAgent:

python
import nxtone import uvicorn from fastapi import FastAPI app = FastAPI() @app.get("/users") def get_users(): return [{"id": 1, "name": "Alice"}] @nxtone.NxtOneAgent def main(): uvicorn.run(app, host="0.0.0.0", port=8000) if __name__ == "__main__": main()

nxtone.run(main) and @nxtone.instrument are also accepted, if you prefer them to the decorator.

Automatic middleware detection: when running inside a FastAPI application, the NxtOne SDK automatically registers an ASGI middleware wrapper to intercept incoming HTTP headers and map active execution spans.

4. Running your service

Start the service through the decorated function:

bash
python main.py

There is no python -m nxtone wrapper and no separate nxtone command — the decorator is the whole integration. If you usually launch with the uvicorn command, move the uvicorn.run(...) call into the decorated main() as shown above so the SDK is active.

5. Shutdown and buffering

The SDK batches events in memory and flushes them when the process exits, or on ASGI lifespan shutdown. At startup it also creates a nxtone/ directory under the system temp directory ($TMPDIR, else /tmp), and writes batches there only when memory is full or sends fail — so if your root filesystem is read-only, mount a tmpfs on /tmp.

  • The SDK installs no SIGTERM handler of its own. Servers such as uvicorn handle SIGTERM and run the lifespan shutdown, where the SDK flushes.
  • The flush can take up to NXTONE_SHUTDOWN_TIMEOUT (60 seconds by default) — longer than Docker's 10-second stop timeout and Kubernetes' 30-second grace period. Raise the grace period to match, or lower the timeout.

6. Verifying the connection

The SDK logs at warn level by default, so a healthy start produces little or no output. The message to watch for is the stderr hint that the API key is missing: it means the SDK is running as a no-op. Otherwise, send a request to your service and check that its execution flow appears in NxtOne under your project ID. If it does not, raise NXTONE_LOG_LEVEL and work through Troubleshooting.

The SDK only makes outbound HTTPS (443) connections — to www.api.nxtone.ai and to Amazon S3 for large values — and opens no ports.