Skip to main content

octroi

Toll gates for HTTP routes: return a 402, verify the payment, serve the 200.

The Python port of @octroi/core. Same protocol, same wire format — byte for byte. The golden files in golden/ are the contract, and tests/test_golden.py holds this package to them.

pip install octroi[fastapi]

FastAPI

from fastapi import FastAPI
from octroi.fastapi import Octroi

app = FastAPI()
tw = Octroi(pay_to=os.environ["OCT_ADDRESS"], network="base", facilitator=coinbase)
Octroi.install(app)          # renders halts, reports outcomes

@app.get("/v1/report", dependencies=[tw.gate(price="$0.004", asset="usdc")])
async def report():
    return {"report": "the paid content"}

Octroi.install(app) is required, not decoration. Without it a paid request is served but never reported, so request.served / request.failed — and therefore refund candidates — silently never happen.

Without a framework

from octroi import GateRequest, create_gate

gate = create_gate(
    price="$0.004",
    network="base",
    pay_to=address,
    facilitator=adapter,
)

result = await gate.handle(GateRequest(
    method="GET", route="/v1/report", url=str(request.url), headers=request.headers,
))

if not result.is_pass:
    return render(result.status, result.headers, result.body)

response = await handler()
result.report(status=response.status_code)

That is the whole adapter contract: map the request, render a halt, report the outcome.

Notes for readers of the TypeScript

The two implementations are deliberately the same shape, so gate.py reads as gate.ts with different punctuation. Two idiomatic differences:

  • snake_case in Python, camelCase on the wire. Field names in challenges, receipts and events are the wire's, never Python's.
  • Events deliver on the running event loop. emit stays synchronous and non-blocking, as in TS; with no loop running, await gate.flush_events() drives delivery.

One documented divergence in canonical.py: JavaScript sorts object keys by UTF-16 code unit, Python by Unicode code point. These agree below U+10000 and differ only for astral-plane keys. Octroi never emits one — and the golden files would catch it.

Develop

python -m venv .venv && .venv/bin/pip install -e ".[dev]"
.venv/bin/python -m pytest

The golden fixtures are produced by the TypeScript suite (cd packages/core && UPDATE_GOLDEN=1 pnpm test). A mismatch here means one of the two implementations changed the wire format.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

octroi-0.1.0.tar.gz (30.3 kB view details)

Uploaded Source

Built Distribution

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

octroi-0.1.0-py3-none-any.whl (28.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: octroi-0.1.0.tar.gz
  • Upload date:
  • Size: 30.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for octroi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b475b1209f79a011ffc894dd77e3f9533da7ff212cd5358a93284672c5d087bc
MD5 7d7de29c1b0fbd982dcefd348eff5244
BLAKE2b-256 94e7d215cc683bd9b9c6482a6c6a36f56ca2ce8f2918e1e65b03c5ead8338d90

See more details on using hashes here.

File details

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

File metadata

  • Download URL: octroi-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 28.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for octroi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2dc6dd27aaa500c886c4f877b205116905834131fe8133c720d5baa1ba66b92d
MD5 1db2669068f7fd3bf27c1cf55791722a
BLAKE2b-256 3d3b21f5f348344f98c8439c41549ff6255363e8c2ed1a62dac60a9c728c6dc7

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page