FastAPI Integration
Coming SoonHow to integrate APISense telemetry into your FastAPI application using our dedicated middleware.
Under Development: The FastAPI integration is currently being built alongside the Python SDK. The documentation below represents the planned API.
Usage
The apisense-python package provides a native FastAPI middleware that automatically intercepts, measures, and reports incoming HTTP requests without blocking your application's event loop.
Simply import APISenseMiddleware and add it to your FastAPI app instance.
from fastapi import FastAPI
from apisense.fastapi import APISenseMiddleware
app = FastAPI()
# Add the APISense middleware
# It will automatically pick up the APISENSE_API_KEY env var
app.add_middleware(APISenseMiddleware)
@app.get("/")
def read_root():
return {"message": "Hello World"}
@app.get("/items/{item_id}")
def read_item(item_id: int):
return {"item_id": item_id}
Configuration
If you need to pass specific configuration options (like adjusting the sampling rate for high-traffic services), you can pass them directly when adding the middleware.
app.add_middleware(
APISenseMiddleware,
api_key="your_api_key_here",
sampling_rate=0.5, # Only record 50% of requests
batch_size=200
)
What gets collected?
The FastAPI middleware automatically collects:
- HTTP Method (GET, POST, etc.)
- Route template (e.g.,
/items/{item_id}rather than raw URLs, avoiding PII leakage) - Response Status Code
- Request latency / execution time
- Timestamp
Privacy Note: APISense does not log request bodies, response bodies, or query parameters by default to ensure sensitive user data never leaves your infrastructure.