Skip to main content

x403

0.1.1: the decision word is a state, allowed, escalated or denied (0.1.0 used allow, escalate, deny). Reason-code effects are unchanged.

The x403 protocol for Python: the objects, RFC 8785 canonical JSON, ES256 signing and verifying, the HTTP binding, a sync and an async client for any authority, and a FastAPI gate. One dependency, cryptography. Python 3.10+.

pip install x403            # objects, canonical JSON, signing, verifying
pip install "x403[client]"  # plus the HTTP client (httpx)
pip install "x403[fastapi]" # plus the FastAPI gate

Ask before you execute

from x403 import X403

x403 = X403(authority="https://api.kredit.sh", api_key=os.environ["KREDIT_API_KEY"])

decision = x403.decide(
    direction="inbound",
    policy_id="shop-checkout-v1",
    payment={"type": "payment", "amount": "189.00", "currency": "USD", "rail": "card", "executor": {"id": "shop.example"}},
    actor={"agent": {"status": "asserted", "id": "did:key:z6Mk..."}},
    evidence=[{"type": "kredit.passport", "ref": passport_hash, "inline": passport}],
    budget_ms=150,
)

if decision["decision"] == "allowed":
    ...  # within decision["constraints"]
else:
    ...  # decision["http_status"], decision["reasons"], decision.get("escalation")

Verify offline

result = x403.verify(decision)   # keys fetched once from authority.json
assert result, result.reason

Or without a client, against keys you already hold:

from x403 import verify_decision
ok = verify_decision(decision, jwks)

FastAPI

from fastapi import Depends, FastAPI
from x403 import AsyncX403
from x403.middleware.fastapi import X403Gate, install, post_receipt

app = FastAPI()
install(app)
authority = AsyncX403("https://api.kredit.sh", api_key=os.environ["KREDIT_API_KEY"])

gate = X403Gate(
    authority,
    payment=lambda request: {"type": "payment", "amount": "189.00", "currency": "USD", "rail": "card", "executor": {"id": "shop.example"}},
    policy_id="shop-checkout-v1",
    budget_ms=150,
)

@app.post("/checkout")
async def checkout(decision: dict = Depends(gate)):
    auth = await charge(decision["constraints"])
    await post_receipt(authority, decision, executed=True, execution={"rail": "card", "ref": auth.id},
                       executor={"id": "did:web:shop.example", "kid": kid}, private_jwk=private_jwk)
    return {"ok": True}

On allowed the route receives the decision. On anything else the gate answers with the decision's http_status, the decision as the body and X403-Decision on the response. If the authority cannot be reached it fails closed with an executor-signed authority_timeout decision.

Sign your side

from x403 import generate_key, sign_receipt, decision_hash

pair = generate_key()   # publish pair.public_jwk at /.well-known/x403/jwks.json
receipt = sign_receipt({"decision_ref": d["id"], "decision_hash": decision_hash(d), "executed": True,
                        "executor": {"id": "did:web:shop.example", "kid": pair.kid}}, pair.private_jwk)

Status codes

status_for(decision) implements section 6.3 of the spec. The decision already carries http_status; use the function to pin the table in your own tests.

Apache-2.0. Spec and registry: https://github.com/kreditsh/x403

Metadata

Release files for x403 0.3.0

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

Source distribution (sdist)

Source distribution for x403 0.3.0
File Size Uploaded Provenance Actions
x403-0.3.0.tar.gz 24.3 kB Without provenance Details for x403-0.3.0.tar.gz

Built distribution (wheel)

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

Total release size: 46.5 kB

Release files / x403-0.3.0.tar.gz

Download URL x403-0.3.0.tar.gz
Size 24.3 kB
Tags Source
SHA-256 checksum
How to use checksums
79ff50a9a23c7e1d9be8da50abc0f232eca8fab9a6db6bb83bcf47bedd1e3263
BLAKE2b-256 checksum
How to use checksums
d06ea4986e8ea8139b89e3b91e993b38307b4182154789159fe1e85da971de6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.3

Release files / x403-0.3.0-py3-none-any.whl

Download URL x403-0.3.0-py3-none-any.whl
Size 22.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2f78fcc9dd4a24d52ab908386d5d68b05d5d83e341643f48257830818c307c7f
BLAKE2b-256 checksum
How to use checksums
4bf17422e04855e8304eb4d2022512a99cc9ccbf3e2636653b43a7a0d9ea2dca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.3

Security for x403 0.3.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.

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.

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