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/csrfreturning{ "token": "…" }withCache-Control: no-store - rejects unsafe methods missing
X-CSRF-TokenwithCSRF_HEADER_MISSING(403) - validates signature, expiry, and binding fingerprint, returning
CSRF_TOKEN_INVALID/CSRF_TOKEN_EXPIREDon failure
Binding resolution order: subject extractor → session extractor →
pre-auth source (all configurable on DefaultCsrfBindingResolver).
Middleware order
Mirrors the C# pipeline:
- Correlation —
X-Request-Idin/out, GUIDv7 generation, invalid-ID policy - Content negotiation — 406/415 on Accept/Content-Type
- Origin policy — RFC 6454 matching on unsafe methods
- Fetch metadata — Sec-Fetch-Site/Mode evaluation
- CSRF — token validation on protected methods
- 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.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| apipilot-0.1.0.tar.gz | 48.5 kB | With provenance | Details for apipilot-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| apipilot-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for apipilot-0.1.0-py3-none-any.whl |
Total release size: 94.4 kB
Release files / apipilot-0.1.0.tar.gz
| Download URL | apipilot-0.1.0.tar.gz |
|---|---|
| Size | 48.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fe75bd17d32e26d7f362792fc92cc991f11ae17a2bd22ff775c84a23acef8b81
|
|
BLAKE2b-256 checksum How to use checksums |
1ee09eef53c07704cb0c71256c267660b77c11036da095c0b1df86e9d7345458
|
| 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 logRelease files / apipilot-0.1.0-py3-none-any.whl
| Download URL | apipilot-0.1.0-py3-none-any.whl |
|---|---|
| Size | 45.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b9d961bcb50b62fdedf6ef5d89a59c7a4cc8b66acd69425b0c56d6e483ba2cbb
|
|
BLAKE2b-256 checksum How to use checksums |
eef4b15d57d5f945fe4f4607b4ad9bcaa792f5f6a839900d19254ae737c39851
|
| 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 logSecurity for apipilot 0.1.0
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.
Attestation for apipilot-0.1.0.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 logA 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.