Skip to main content

Python SDK Setup

This guide mirrors what you expect from a premium SDK setup (inspired by Apitally-class DX) and applies to FastAPI, Flask, and Django Ninja.

1) Install

Use extras to pull the right integration in one line.
pip install "apilenss[fastapi]"
# or
pip install "apilenss[flask]"
# or
pip install "apilenss[django]"
Import path note:
  • install package: apilenss
  • import modules: apilens.* (recommended)
  • compatibility aliases: apilenss.* also work

2) Configure environment

Add these to your secrets manager or .env:
APILENS_API_KEY=your-app-api-key
APILENS_BASE_URL=https://api.apilens.ai/api/v1
APILENS_ENVIRONMENT=production

3) Framework wiring

from fastapi import FastAPI
from apilens.fastapi import ApiLensMiddleware

app = FastAPI()
app.add_middleware(
    ApiLensMiddleware,
    api_key="APILENS_API_KEY_FROM_ENV",
    base_url="https://api.apilens.ai/api/v1",
    env="production",
    enable_request_logging=True,
    log_request_headers=True,
    log_request_body=True,
    log_response_body=True,
)

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_API_KEY_FROM_ENV",
        base_url="https://api.apilens.ai/api/v1",
        environment="production",
        enable_request_logging=True,
        log_request_headers=True,
        log_request_body=True,
        log_response_body=True,
    )
)

instrument_app(app, client)

Django Ninja

from django.conf import settings
from apilens import ApiLensClient, ApiLensConfig
from apilens.django import instrument_django

apilens_client = ApiLensClient(
    ApiLensConfig(
        api_key=settings.APILENS_API_KEY,
        base_url="https://api.apilens.ai/api/v1",
        environment=settings.APILENS_ENVIRONMENT,
        enable_request_logging=True,
        log_request_headers=True,
        log_request_body=True,
        log_response_body=True,
    )
)

instrument_django(apilens_client)
Associate requests with user/device identity to unlock consumer filters. FastAPI example:
from fastapi import Depends, Request
from apilens.fastapi import set_consumer

def identify_consumer(request: Request, current_user=Depends(get_current_user)):
    set_consumer(
        request,
        identifier=current_user.user_id,
        name=current_user.name,
        group=current_user.team,
    )

app = FastAPI(dependencies=[Depends(identify_consumer)])

5) Verify traffic

  1. Deploy/restart your service.
  2. Hit a few endpoints.
  3. Open Endpoints in API Lens; you should see data in under a minute.

6) Troubleshooting

  • No data: confirm API key matches the app and env; check network egress to api.apilens.ai.
  • 500 on ingest: verify migrations are applied and ClickHouse/Postgres env are present.
  • Payloads missing: ensure enable_request_logging and body logging flags are True.