Skip to main content

apipilot

ApiPilot for Python. A framework-neutral core implementing the full ApiPilot wire contract — envelopes, error codes, pagination, validation field keys, origin policy, fetch-metadata policy, cookie rules, and CSRF protection — plus an ASGI adapter that works with FastAPI, Starlette, and any ASGI-capable framework including Django.

The core has zero dependencies. The ASGI adapter requires cryptography (AES-256-GCM for CSRF token protection).

Install

pip install apipilot              # core only
pip install "apipilot[asgi]"      # core + ASGI adapter

Why ASGI and not FastAPI-specific?

ApiPilot is a web-framework integration library — its job is middleware, headers, cookies, and CSRF. Binding to one framework would repeat the mistake of picking a single web stack. ASGI is the Python equivalent of the C# HttpContext abstraction: one middleware covers FastAPI, Starlette, Django (via asgiref), Litestar, and Quart. The core package stays framework-neutral so non-HTTP uses (CLI validation, testing) can import it without pulling in a web stack.

Quick start (FastAPI)

from fastapi import FastAPI
from apipilot.asgi import ApiPilotASGIMiddleware, ApiPilotOptions

app = FastAPI()
app.add_middleware(ApiPilotASGIMiddleware, options=ApiPilotOptions())

Quick start (Starlette / raw ASGI / Django)

# Starlette
app.add_middleware(ApiPilotASGIMiddleware, options=ApiPilotOptions())

# Raw ASGI / Django (ASGI deployment — uvicorn/daphne/hypercorn)
application = ApiPilotASGIMiddleware(django_asgi_app, options=ApiPilotOptions())

Runnable samples: samples/fastapi_app.py, samples/starlette_app.py, samples/django_app.py (Django via get_asgi_application()).

WSGI frameworks (Flask, Django-WSGI, Bottle, Pyramid)

apipilot.wsgi.ApiPilotWSGIMiddleware implements the same pipeline for WSGI apps — same options, same CSRF key ring, same binding resolvers (the middleware builds an ASGI-compatible scope from the WSGI environ):

from apipilot.wsgi import ApiPilotWSGIMiddleware

# Flask
app.wsgi_app = ApiPilotWSGIMiddleware(app.wsgi_app, options=ApiPilotOptions())

# Django (WSGI deployment — gunicorn)
application = ApiPilotWSGIMiddleware(get_wsgi_application(), options=ApiPilotOptions())

Requires pip install "apipilot[wsgi]" (cryptography for CSRF). Parsed pagination is available at environ["apipilot.pagination"].

CSRF protection

from apipilot.asgi import ApiPilotOptions, CsrfKeyRing, generate_key_material
import base64

options = ApiPilotOptions()
options.csrf.enabled = True

key_ring = CsrfKeyRing(
    active_key_id="k1",
    keys={"k1": base64.b64decode(generate_key_material())},
)

Or via environment (multi-instance contract):

export APIPILOT_CSRF_KEYS="k1:<base64-32-byte-key>"
export APIPILOT_CSRF_ACTIVE_KEY="k1"

When enabled the middleware:

  • registers GET /api/csrf returning { "token": "…" } with Cache-Control: no-store
  • rejects unsafe methods missing X-CSRF-Token with CSRF_HEADER_MISSING (403)
  • validates signature, expiry, and binding fingerprint, returning CSRF_TOKEN_INVALID / CSRF_TOKEN_EXPIRED on failure

Binding resolution order: subject extractor → session extractor → pre-auth source (all configurable on DefaultCsrfBindingResolver).

Middleware order

Mirrors the C# pipeline:

  1. Correlation — X-Request-Id in/out, GUIDv7 generation, invalid-ID policy
  2. Content negotiation — 406/415 on Accept/Content-Type
  3. Origin policy — RFC 6454 matching on unsafe methods
  4. Fetch metadata — Sec-Fetch-Site/Mode evaluation
  5. CSRF — token validation on protected methods
  6. Exception mapping — exceptions → ApiPilot error envelopes

Core modules

Module Mirrors (C#)
error_codes ApiErrorCode — 14 standard codes
envelope ApiResponse/ErrorResponse builders
pagination PaginationOptions, PageRequest, query parsing
field_key FieldKeyNormalizer
origin OriginValidator
fetch_metadata FetchMetadataEvaluator
cookie_profile CookieProfileOptionsValidator
csrf_format CsrfTokenFormat (pipe-delimited payload)
csrf_binding CsrfBindingProvider.Fingerprint
correlation CorrelationMiddleware logic
content_negotiation ApiPilotContentNegotiationMiddleware

Conformance

apipilot passes all 107 ApiPilot conformance vectors (251 assertions) — the same vectors the C# and TypeScript ports run. See tests/conformance_runner.py.

License

Apache-2.0 — same as ApiPilot.

Metadata

Release files for apipilot 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 apipilot 0.1.1
File Size Uploaded Provenance Actions
apipilot-0.1.1.tar.gz 48.7 kB With provenance Details for apipilot-0.1.1.tar.gz

Built distribution (wheel)

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

Total release size: 94.6 kB

Release files / apipilot-0.1.1.tar.gz

Download URL apipilot-0.1.1.tar.gz
Size 48.7 kB
Tags Source
SHA-256 checksum
How to use checksums
6de967862f5f0385fa227e57f93834e469e6e4cfd2722f35227f2f7192d5da95
BLAKE2b-256 checksum
How to use checksums
2aa479bab15f9f0960d3a48d80a979cfff61e46b9037d5cd6acffc0369847c2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

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

Download URL apipilot-0.1.1-py3-none-any.whl
Size 45.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd71b5b397f7457c67d554df34c383366cc91ed7a73da3195f6cd878958eaa45
BLAKE2b-256 checksum
How to use checksums
2505430aa0690609e6e4756b2aaaf27846aae860d908ff253c2d029a59b3d84e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Security for apipilot 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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for apipilot-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

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 Full 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