Skip to main content

Zero-dependency Python SDK for PeekAPI

Project description

PeekAPI — Python SDK

PyPI license CI

Zero-dependency Python SDK for PeekAPI. Built-in middleware for ASGI (FastAPI, Starlette, Litestar), WSGI (Flask, Bottle), and Django.

Install

pip install peekapi

Quick Start

FastAPI / Starlette (ASGI)

from fastapi import FastAPI
from peekapi import PeekApiClient
from peekapi.middleware import PeekApiASGI

client = PeekApiClient({"api_key": "ak_live_xxx"})

app = FastAPI()
app.add_middleware(PeekApiASGI, client=client)

Flask (WSGI)

from flask import Flask
from peekapi import PeekApiClient
from peekapi.middleware import PeekApiWSGI

client = PeekApiClient({"api_key": "ak_live_xxx"})

app = Flask(__name__)
app.wsgi_app = PeekApiWSGI(app.wsgi_app, client=client)

Django

# settings.py
PEEKAPI = {
    "api_key": "ak_live_xxx",
}

MIDDLEWARE = [
    "peekapi.middleware.django.PeekApiMiddleware",
    # ... other middleware
]

Standalone Client

from peekapi import PeekApiClient

client = PeekApiClient({"api_key": "ak_live_xxx"})

client.track({
    "method": "GET",
    "path": "/api/users",
    "status_code": 200,
    "response_time_ms": 42,
})

# Graceful shutdown (flushes remaining events)
client.shutdown()

Configuration

Option Default Description
api_key required Your PeekAPI key
endpoint PeekAPI cloud Ingestion endpoint URL
flush_interval 10.0 Seconds between automatic flushes
batch_size 100 Events per HTTP POST (triggers flush)
max_buffer_size 10000 Max events held in memory
max_storage_bytes 5242880 Max disk fallback file size (5MB)
max_event_bytes 65536 Per-event size limit (64KB)
storage_path auto Custom path for JSONL persistence file
debug False Enable debug logging
on_error None Callback (Exception) -> None for flush errors

How It Works

  1. Middleware intercepts every request/response
  2. Captures method, path, status code, response time, request/response sizes, consumer ID
  3. Events are buffered in memory and flushed in batches on a daemon thread
  4. On network failure: exponential backoff with jitter, up to 5 retries
  5. After max retries: events are persisted to a JSONL file on disk
  6. On next startup: persisted events are recovered and re-sent
  7. On SIGTERM/SIGINT: remaining buffer is flushed or persisted to disk

Consumer Identification

By default, consumers are identified by:

  1. X-API-Key header — stored as-is
  2. Authorization header — hashed with SHA-256 (stored as hash_<hex>)

Override with the identify_consumer option to use any header or request property:

client = PeekApiClient({
    "api_key": "...",
    "identify_consumer": lambda headers: headers.get("x-tenant-id"),
})

The callback receives a dict[str, str] of lowercase header names and should return a consumer ID string or None.

Features

  • Zero runtime dependencies — uses only Python stdlib
  • Background flush — daemon thread with configurable interval and batch size
  • Disk persistence — undelivered events saved to JSONL, recovered on restart
  • Exponential backoff — with jitter, max 5 consecutive failures before disk fallback
  • SSRF protection — private IP blocking, HTTPS enforcement (HTTP only for localhost)
  • Input sanitization — path (2048), method (16), consumer_id (256) truncation
  • Per-event size limit — strips metadata first, drops if still too large (default 64KB)
  • Graceful shutdown — signal handlers (SIGTERM/SIGINT) with disk persistence

Requirements

  • Python >= 3.10

Contributing

  1. Fork & clone the repo
  2. Install dev dependencies — uv sync --no-install-project
  3. Run tests — uv run pytest -v
  4. Lint & format — uv run ruff check src/ tests/ / uv run ruff format src/ tests/
  5. Submit a PR

License

MIT

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

peekapi-0.1.0.tar.gz (30.2 kB view details)

Uploaded Source

Built Distribution

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

peekapi-0.1.0-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file peekapi-0.1.0.tar.gz.

File metadata

  • Download URL: peekapi-0.1.0.tar.gz
  • Upload date:
  • Size: 30.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.5 {"installer":{"name":"uv","version":"0.10.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for peekapi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f7be4dfd187bdd009f385716ef7f640cbb963a7bdc43ee262775846bedd4e77a
MD5 f4af3460db72f3cc2b503e3e2de190ac
BLAKE2b-256 b54a3d62c07208a682b723606666264a85ddfa15dfcac91de0cb9ae15122c592

See more details on using hashes here.

File details

Details for the file peekapi-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: peekapi-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.5 {"installer":{"name":"uv","version":"0.10.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for peekapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7380be844833ab317e93dd49ac413919f8ed05ddd3d569d30613c5b0f17e99a2
MD5 99bdf0d92dc69847c288a89800dcb4c7
BLAKE2b-256 d3e0dcb8a7519dc92dff9da0bb1ad5a4966a475d39738873a24499b433ccc1c3

See more details on using hashes here.

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