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.
⚠️ Breaking Change in v0.1.4: The
app_idparameter is now required for all integrations. Make sure to include it in your configuration.
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 ./sdks/python
pip install './sdks/python[all]'
Quick start (manual capture)
from apilens import ApiLensClient, ApiLensConfig
client = ApiLensClient(
ApiLensConfig(
api_key="your_app_api_key",
base_url="https://api.apilens.ai/api/v1",
environment="production",
)
)
client.capture(
app_id="your_app_id", # Required: Get this from API Lens dashboard
method="GET",
path="/health",
status_code=200,
response_time_ms=12.4,
)
client.shutdown(flush=True)
Getting your App ID
- Log in to API Lens Dashboard
- Navigate to your project
- Select or create an app
- Copy the App ID from the app settings
FastAPI
No OpenTelemetry instrumentation is required for endpoint + payload monitoring.
from fastapi import FastAPI
from typing import Annotated
from fastapi import Depends, Request
from apilens.fastapi import ApiLensMiddleware, set_consumer
app = FastAPI()
app.add_middleware(
ApiLensMiddleware,
api_key="your_app_api_key", # Required: Your API key
app_id="your_app_id", # Required: Your app ID from dashboard
base_url="https://api.apilens.ai/api/v1",
env="production",
enable_request_logging=True,
log_request_body=True,
log_response_body=True,
)
def identify_consumer(request: Request, user_id: Annotated[str, Depends(lambda: "user_123")]):
set_consumer(request, identifier=user_id, name="Demo User", group="starter")
app.router.dependencies.append(Depends(identify_consumer))
@app.get("/v1/orders")
def list_orders():
return {"ok": True}
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"),
base_url=os.getenv("APILENS_BASE_URL", "https://api.apilens.ai/api/v1"),
env=os.getenv("APILENS_ENVIRONMENT", "production"),
enable_request_logging=True,
log_request_body=True,
log_response_body=True,
)
Starlette
from starlette.applications import Starlette
from apilens import ApiLensClient, ApiLensConfig
from apilens.starlette import instrument_app
app = Starlette()
client = ApiLensClient(
ApiLensConfig(
api_key="your_app_api_key",
base_url="https://api.apilens.ai/api/v1",
environment="production",
)
)
instrument_app(
app,
client,
app_id="your_app_id" # Required: Your app ID from dashboard
)
Flask
from flask import Flask
from apilens import ApiLensClient, ApiLensConfig
from apilens.flask import instrument_app
app = Flask(__name__)
client = ApiLensClient(
ApiLensConfig(
api_key="your_app_api_key",
base_url="https://api.apilens.ai/api/v1",
environment="production",
)
)
instrument_app(
app,
client,
app_id="your_app_id" # Required: Your app ID from dashboard
)
@app.get("/v1/invoices")
def invoices():
return {"ok": True}
Django (DRF + Django Ninja)
Add middleware in Django settings:
MIDDLEWARE = [
# ...
"apilens.django.ApiLensDjangoMiddleware",
]
# Required configuration
APILENS_API_KEY = "your_app_api_key"
APILENS_APP_ID = "your_app_id" # Required: Your app ID from dashboard
APILENS_BASE_URL = "https://api.apilens.ai/api/v1"
APILENS_ENVIRONMENT = "production"
Litestar
from litestar import Litestar
from apilens import ApiLensClient, ApiLensConfig
from apilens.litestar import ApiLensPlugin
client = ApiLensClient(
ApiLensConfig(
api_key="your_app_api_key",
base_url="https://api.apilens.ai/api/v1",
environment="production",
)
)
app = Litestar(
route_handlers=[],
plugins=[ApiLensPlugin(
client=client,
app_id="your_app_id" # Required: Your app ID from dashboard
)]
)
BlackSheep
from blacksheep import Application
from apilens import ApiLensClient, ApiLensConfig
from apilens.blacksheep import instrument_app
app = Application()
client = ApiLensClient(
ApiLensConfig(
api_key="your_app_api_key",
base_url="https://api.apilens.ai/api/v1",
environment="production",
)
)
instrument_app(
app,
client,
app_id="your_app_id" # Required: Your app ID from dashboard
)
Configuration Options
| Parameter | Required | Default | Description |
|---|---|---|---|
api_key |
✅ Yes | - | Your API key from API Lens dashboard |
app_id |
✅ Yes | - | Your app ID from API Lens dashboard |
base_url |
No | https://api.apilens.ai/api/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
422 Unprocessable Entity Error
If you're getting 422 errors, make sure you've included the app_id parameter:
# ❌ Old way (will fail)
app.add_middleware(
ApiLensMiddleware,
api_key="your_key",
)
# ✅ New way (required since v0.1.4)
app.add_middleware(
ApiLensMiddleware,
api_key="your_key",
app_id="your_app_id",
)
Finding Your App ID
- Log in to API Lens Dashboard
- Navigate to your project
- Go to the Apps tab
- Select your app or create a new one
- Copy the App ID from the URL or app settings
Example App ID format: c2537f6e-9b59-47ec-ab13-3559ae645c60
Data Not Appearing in Dashboard
- Verify
app_idis correct - Check that
api_keyis valid - Ensure
base_urlpoints to the correct endpoint - Check application logs for SDK errors
- Wait up to 30 seconds for data to appear (batching delay)
Migration from v0.1.3 to v0.1.4
Breaking change: The app_id parameter is now required.
Update all middleware/client configurations to include app_id:
# Before (v0.1.3)
client = ApiLensClient(ApiLensConfig(api_key="..."))
# After (v0.1.4)
client = ApiLensClient(ApiLensConfig(
api_key="...",
# No app_id needed for client, but required when calling capture()
))
client.capture(
app_id="your_app_id", # Now required
method="GET",
path="/health",
# ...
)
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.5.tar.gz.
File metadata
- Download URL: apilenss-0.1.5.tar.gz
- Upload date:
- Size: 75.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba194a23ffbf80a6f52105cd7b5f7b89fff0f0d8d068cb59d9bc0aca487888a9
|
|
| MD5 |
01bb278d492879cc9131c34ca4a155c6
|
|
| BLAKE2b-256 |
25273016f1258f0b842dacc020ddec8bfa9962b0d1c5d2befb4b60ff94034f68
|
Provenance
The following attestation bundles were made for apilenss-0.1.5.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.5.tar.gz -
Subject digest:
ba194a23ffbf80a6f52105cd7b5f7b89fff0f0d8d068cb59d9bc0aca487888a9 - Sigstore transparency entry: 1154909868
- Sigstore integration time:
-
Permalink:
apilens/apilens@4fefee1a23544d75137a17e4f29e981f4906d730 -
Branch / Tag:
refs/tags/apilens-sdk-v0.1.5 - Owner: https://github.com/apilens
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@4fefee1a23544d75137a17e4f29e981f4906d730 -
Trigger Event:
push
-
Statement type:
File details
Details for the file apilenss-0.1.5-py3-none-any.whl.
File metadata
- Download URL: apilenss-0.1.5-py3-none-any.whl
- Upload date:
- Size: 20.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
483e56e476be111f3f807dbbf057baa910619bb1969e16ab9f0020df13986f3a
|
|
| MD5 |
27db0f8d514585128bfaeec40880983e
|
|
| BLAKE2b-256 |
725578a288b4484ca6588d0308e4bd5b949007b8ce9b28807639e49bbb9fc552
|
Provenance
The following attestation bundles were made for apilenss-0.1.5-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.5-py3-none-any.whl -
Subject digest:
483e56e476be111f3f807dbbf057baa910619bb1969e16ab9f0020df13986f3a - Sigstore transparency entry: 1154909869
- Sigstore integration time:
-
Permalink:
apilens/apilens@4fefee1a23544d75137a17e4f29e981f4906d730 -
Branch / Tag:
refs/tags/apilens-sdk-v0.1.5 - Owner: https://github.com/apilens
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@4fefee1a23544d75137a17e4f29e981f4906d730 -
Trigger Event:
push
-
Statement type: