Skip to main content

PeekAPI — Python SDK

PeekAPI has shut down. The service closed in November 2026 and its ingest endpoint is offline, so this SDK can no longer deliver events. The package stays published for reference only; remove it from your project. See peekapi.dev.

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

Bug reports and feature requests: peekapi-dev/community

  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

Support

If you find PeekAPI useful, give this repo a star — it helps others discover the project.

Badge

Show that your API is monitored by PeekAPI:

[![Monitored by PeekAPI](https://img.shields.io/badge/monitored%20by-PeekAPI-06b6d4)](https://peekapi.dev)

License

MIT

Metadata

Release files for peekapi 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for peekapi 0.1.1
File Size Uploaded Provenance Actions
peekapi-0.1.1.tar.gz 30.3 kB Without provenance Details for peekapi-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for peekapi 0.1.1
File Interpreter ABI Platform Actions
peekapi-0.1.1-py3-none-any.whl Python 3 none any Details for peekapi-0.1.1-py3-none-any.whl

Total release size: 46.4 kB

Release files / peekapi-0.1.1.tar.gz

Download URL peekapi-0.1.1.tar.gz
Size 30.3 kB
Tags Source
SHA-256 checksum
How to use checksums
0f71ab56beeb0000e7bbeb4bd4d4ccb197dcf799102b14ae324926738a7bf3ae
BLAKE2b-256 checksum
How to use checksums
6caaa1b87681d1df565b3ecc3856668778ccbc4e6f978e68d032ff9905bc1d44
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release files / peekapi-0.1.1-py3-none-any.whl

Download URL peekapi-0.1.1-py3-none-any.whl
Size 16.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
38a7db0e1b6094825c873305da9e84eee777a725efc69888a18f4c8581198897
BLAKE2b-256 checksum
How to use checksums
177fad19baabe634877f668277c312b16c3a308f2108913bf6c6106613aa3c2f
Upload date
Uploaded using Trusted Publishing?
What is 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}

Security for peekapi 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page