Logo
Beta

Python guide

The respository for our open source Python middleware can be found here.

Installation

$ pip install apilytics

Enable the middleware and set your API key: A good practice is to securely store the API key as an environment variable. You can leave the env variable unset in e.g. development and test environments, the middleware will be automatically disabled if the key is None.

Django:

# settings.py

import os

APILYTICS_API_KEY = os.getenv("APILYTICS_API_KEY")

MIDDLEWARE = [
    "apilytics.django.ApilyticsDjangoMiddleware",
]

FastAPI:

# main.py

import os
from fastapi import FastAPI
from apilytics.fastapi import ApilyticsFastAPIMiddleware

app = FastAPI()
app.add_middleware(ApilyticsFastAPIMiddleware, api_key=os.getenv("APILYTICS_API_KEY"))

Other Python frameworks:

# my_apilytics_middleware.py

import os
from apilytics.core import ApilyticsSender

def my_apilytics_middleware(request, get_response):
  with ApilyticsSender(
      api_key=os.getenv("APILYTICS_API_KEY"),
      path=request.path,
      method=request.method,
  ) as sender:
      response = get_response(request)
      sender.set_response_info(status_code=response.status_code)
  return response

Help us improve these docs by giving us feedback.