Skip to main content

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_key and the app_id of the app you're instrumenting. (project_slug is 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_url defaults to https://ingest.apilens.ai/v1 (override with APILENS_BASE_URL for 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

  1. Verify api_key is valid (and belongs to the right project).
  2. Verify app_id matches an app in that project.
  3. Ensure base_url points to the correct endpoint.
  4. Check application logs for SDK errors.
  5. 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

apilenss-0.1.9.tar.gz (76.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

apilenss-0.1.9-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

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

Hashes for apilenss-0.1.9.tar.gz
Algorithm Hash digest
SHA256 8bb853f0b78cb4663d13891357214ad888853c1f39c23de41cb580f3ecc7c846
MD5 1f60558bd50a6b81f29be9ffcb36b6e0
BLAKE2b-256 43f569100e68465d5bb25f8f3b09fbc68750db69dc7896a2e2d49380c2be3f1a

See more details on using hashes here.

Provenance

The following attestation bundles were made for apilenss-0.1.9.tar.gz:

Publisher: workflow.yml on apilens/apilens

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for apilenss-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 46f966ae9db2df7cfdea81e3b9b6435f86abb92d1ebe886ae33d4d8d9d55f74f
MD5 67c3f7ffead8891c849227b5420b8668
BLAKE2b-256 21cb8a8ee52dd9b0fb3d7d6567b715a624b1792d49182815b1443c190db057c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for apilenss-0.1.9-py3-none-any.whl:

Publisher: workflow.yml on apilens/apilens

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page