API Lens Python SDK with OpenTelemetry-based ingest forwarding
Project description
API Lens Python SDK
Production-ready Python ingest client for API Lens with OpenTelemetry integration.
Easy setup: your API key is project-level. You only need two values — the project's
api_keyand theapp_idof the app you're instrumenting. (project_slugis no longer required — the server derives the project from the key.)from fastapi import FastAPI from apilens.fastapi import ApiLensMiddleware app = FastAPI() app.add_middleware(ApiLensMiddleware, api_key="apilens_xxx", app_id="orders-api")
base_urldefaults tohttps://ingest.apilens.ai/v1(override withAPILENS_BASE_URLfor local dev).
Framework support matrix
| Framework | Integration Module | Integration Type | Client Type |
|---|---|---|---|
| FastAPI | apilens.fastapi |
ASGI Middleware | AsyncIO |
| Starlette | apilens.starlette |
ASGI Middleware | AsyncIO |
| Django REST Framework | apilens.django |
Django Middleware | Threading |
| Django Ninja | apilens.django |
Django Middleware | Threading |
| Flask | apilens.flask |
WSGI Wrapper | Threading |
| Litestar | apilens.litestar |
Plugin Protocol | AsyncIO |
| BlackSheep | apilens.blacksheep |
ASGI Middleware | AsyncIO |
What this SDK includes
- batched + retrying ingest client (
ApiLensClient) - OpenTelemetry span exporter (
apilens.otel) for teams already on OTel - first-class framework integrations listed above
- automatic request/response payload sampling (size-limited)
Install
pip install apilenss
With framework support:
pip install 'apilenss[all]'
# or only one
pip install 'apilenss[fastapi]'
pip install 'apilenss[flask]'
Local development install (from repo):
pip install ./packages/sdk-python
pip install './packages/sdk-python[all]'
The two values you need
api_key— created on a project in the dashboard. It is project-level: one key works for every app in that project.app_id— the slug of the specific app you're instrumenting, so the SDK knows which app the traffic belongs to.
Find both in the API Lens Dashboard: the API key on the project's API-keys page, the app slug on the app's page.
Quick start (manual capture)
from apilens import ApiLensClient, ApiLensConfig
client = ApiLensClient(
ApiLensConfig(api_key="apilens_xxx") # project-level key
)
client.capture(
app_id="orders-api", # which app in the project
method="GET",
path="/health",
status_code=200,
response_time_ms=12.4,
)
client.shutdown(flush=True)
FastAPI
No OpenTelemetry instrumentation is required for endpoint + payload monitoring.
from fastapi import FastAPI
from apilens.fastapi import ApiLensMiddleware, set_consumer
app = FastAPI()
app.add_middleware(
ApiLensMiddleware,
api_key="apilens_xxx", # project-level key
app_id="orders-api", # which app
)
Environment variables (recommended):
import os
from fastapi import FastAPI
from apilens.fastapi import ApiLensMiddleware
app = FastAPI()
app.add_middleware(
ApiLensMiddleware,
api_key=os.getenv("APILENS_API_KEY"),
app_id=os.getenv("APILENS_APP_ID"),
)
Capture the calling consumer per request with set_consumer(request, identifier=..., name=..., group=...).
Starlette
from starlette.applications import Starlette
from apilens import ApiLensClient, ApiLensConfig
from apilens.starlette import instrument_app
app = Starlette()
client = ApiLensClient(ApiLensConfig(api_key="apilens_xxx"))
instrument_app(app, client, app_id="orders-api")
Flask
from flask import Flask
from apilens import ApiLensClient, ApiLensConfig
from apilens.flask import instrument_app
app = Flask(__name__)
client = ApiLensClient(ApiLensConfig(api_key="apilens_xxx"))
instrument_app(app, client, app_id="orders-api")
Django (DRF + Django Ninja)
Add the middleware and two settings:
MIDDLEWARE = [
# ...
"apilens.django.ApiLensDjangoMiddleware",
]
APILENS_API_KEY = "apilens_xxx" # project-level key
APILENS_APP_ID = "orders-api" # which app
Litestar
from litestar import Litestar
from apilens import ApiLensClient, ApiLensConfig
from apilens.litestar import ApiLensPlugin
client = ApiLensClient(ApiLensConfig(api_key="apilens_xxx"))
app = Litestar(route_handlers=[], plugins=[ApiLensPlugin(client=client, app_id="orders-api")])
BlackSheep
from blacksheep import Application
from apilens import ApiLensClient, ApiLensConfig
from apilens.blacksheep import instrument_app
app = Application()
client = ApiLensClient(ApiLensConfig(api_key="apilens_xxx"))
instrument_app(app, client, app_id="orders-api")
Configuration Options
| Parameter | Required | Default | Description |
|---|---|---|---|
api_key |
Yes | - | Project-level API key from the dashboard |
app_id |
Yes | - | Slug of the app being instrumented |
project_slug |
No | derived from key | Only needed for clarity/validation; the key already identifies the project |
base_url |
No | https://ingest.apilens.ai/v1 |
API Lens ingest endpoint |
environment |
No | production |
Environment name (e.g., production, staging, dev) |
enable_request_logging |
No | True |
Enable request/response logging |
log_request_body |
No | False |
Log request body (up to max size) |
log_response_body |
No | False |
Log response body (up to max size) |
Notes
- Default flush interval:
3s - Default batch size:
200 - Max ingest batch payload sent per request: follows backend limit (
<= 1000) - Call
client.shutdown(flush=True)on graceful shutdown
Troubleshooting
Data not appearing in the dashboard
- Verify
api_keyis valid (and belongs to the right project). - Verify
app_idmatches an app in that project. - Ensure
base_urlpoints to the correct endpoint. - Check application logs for SDK errors.
- Wait up to ~30s for data to appear (batching delay).
422 Unprocessable Entity
A 422 from the ingest endpoint usually means app_id is missing or doesn't match
an app in your key's project. Set app_id to the app's slug from the dashboard.
Support
- 📧 Email: hello@apilens.ai
- 📖 Documentation: https://apilens.ai/docs
- 🐛 Issues: https://github.com/apilens/apilens/issues
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file apilenss-0.1.9.tar.gz.
File metadata
- Download URL: apilenss-0.1.9.tar.gz
- Upload date:
- Size: 76.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8bb853f0b78cb4663d13891357214ad888853c1f39c23de41cb580f3ecc7c846
|
|
| MD5 |
1f60558bd50a6b81f29be9ffcb36b6e0
|
|
| BLAKE2b-256 |
43f569100e68465d5bb25f8f3b09fbc68750db69dc7896a2e2d49380c2be3f1a
|
Provenance
The following attestation bundles were made for apilenss-0.1.9.tar.gz:
Publisher:
workflow.yml on apilens/apilens
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
apilenss-0.1.9.tar.gz -
Subject digest:
8bb853f0b78cb4663d13891357214ad888853c1f39c23de41cb580f3ecc7c846 - Sigstore transparency entry: 1765525495
- Sigstore integration time:
-
Permalink:
apilens/apilens@f191687a671559f07e5616c2fab889119b7952f1 -
Branch / Tag:
refs/tags/apilens-sdk-v0.1.9 - Owner: https://github.com/apilens
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@f191687a671559f07e5616c2fab889119b7952f1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file apilenss-0.1.9-py3-none-any.whl.
File metadata
- Download URL: apilenss-0.1.9-py3-none-any.whl
- Upload date:
- Size: 19.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46f966ae9db2df7cfdea81e3b9b6435f86abb92d1ebe886ae33d4d8d9d55f74f
|
|
| MD5 |
67c3f7ffead8891c849227b5420b8668
|
|
| BLAKE2b-256 |
21cb8a8ee52dd9b0fb3d7d6567b715a624b1792d49182815b1443c190db057c9
|
Provenance
The following attestation bundles were made for apilenss-0.1.9-py3-none-any.whl:
Publisher:
workflow.yml on apilens/apilens
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
apilenss-0.1.9-py3-none-any.whl -
Subject digest:
46f966ae9db2df7cfdea81e3b9b6435f86abb92d1ebe886ae33d4d8d9d55f74f - Sigstore transparency entry: 1765525876
- Sigstore integration time:
-
Permalink:
apilens/apilens@f191687a671559f07e5616c2fab889119b7952f1 -
Branch / Tag:
refs/tags/apilens-sdk-v0.1.9 - Owner: https://github.com/apilens
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@f191687a671559f07e5616c2fab889119b7952f1 -
Trigger Event:
push
-
Statement type: