paysafe-x402 (Python)
Official Python SDK for PaySafe — the payment security firewall for x402 micropayments. One call before your agent settles a payment; allow/flag/block comes back with machine-readable reasons.
Python 3.9+. Single dependency (cryptography, for Ed25519 attestation verification).
pip install paysafe-x402
30 seconds
from paysafe_x402 import PaySafeClient, PaySafeBlockedError
paysafe = PaySafeClient(agent_id="my-agent") # mints a free API key on first use (100 free scans)
try:
paysafe.guard_outgoing(payment, expected_price_usd=0.01)
# verdict was allow (or flag) — safe to hand to your wallet
except PaySafeBlockedError as e:
print("Payment blocked:", e.scan["checks"]) # machine-readable reasons
The one-line diff: scan every payment by default
Wrap your x402 payment-capable transport and every payment is scanned before it settles:
from paysafe_x402 import PaySafeClient, wrap_transport_with_paysafe
paysafe = PaySafeClient(agent_id="my-agent")
guarded = wrap_transport_with_paysafe(my_x402_transport, paysafe)
# use `guarded` anywhere a transport goes
Non-402 responses pass through untouched (zero overhead). On a 402, the payment is guarded as an outgoing payment (overpayment, address poisoning, velocity, injection provenance — anything you observe()d feeds the detector), the offer is scanned as an incoming request (URL risk, credential demands, asset verification, reputation), and only passing verdicts reach the paying transport. A block raises PaySafeBlockedError before any payment is signed; unparseable 402 offers fail closed. Options: strict, scan_offer, expected_price_usd, on_scan telemetry, base_transport.
The important part: provenance tagging
PaySafe's strongest detector catches payments triggered by prompt-injected content — but it needs to know where your agent's decision came from. Tell it:
# After EVERY tool result / fetched page your agent reads:
paysafe.observe(tool_result_text, source_url="https://api.example.com/page")
# The next scan (within 5 min) is automatically tagged:
# context.origin = "fetched_content" | "tool_result"
# context.content = the observed text (truncated to 8 KB)
# If the pay-to address turns out to have COME FROM that content -> block.
# When the decision is the agent's own plan, or a human said so:
paysafe.note_planning()
paysafe.note_user_instruction()
Each observation is consumed by one scan; unrelated later scans aren't mislabeled. LangChain/CrewAI users: call observe() in your tool-output callback and guard_outgoing() in your payment tool — two lines total.
Verified verdicts (on by default)
Every scan response carries an Ed25519 attestation binding the verdict to the exact payment. The SDK pins the server's verdict key (fetched once, or pass verdict_key_hex to hard-pin), verifies the signature against the pinned key, recomputes the payment commitment sha256(network|pay_to|asset|amount|nonce) locally (rejecting attestations issued for a different payment — replay defense), and enforces expiry. Any failure raises AttestationError.
The verifier is cross-validated in CI against attestations signed by the production Node signer, so Python and TypeScript agree byte-for-byte.
Wallet authors: verify_attestation(scan, payment, trusted_key_hex) and compute_payment_commitment(payment) are importable standalone.
Enforcement: a wallet that refuses unscanned payments
Everything above is advisory — a compromised agent can skip the scan. The enforcement kit closes that gap at the signing layer:
from paysafe_x402 import PaySafeClient, PaySafeEnforcer
from eth_account import Account
paysafe = PaySafeClient(agent_id="my-agent")
enforcer = PaySafeEnforcer(trusted_key_hex=paysafe.verdict_key())
account = enforcer.guard_signer(Account.from_key(PRIVATE_KEY))
# hand `account` to your x402 client exactly as before — it is a drop-in proxy
scan = paysafe.guard_outgoing(payment) # raises on block
enforcer.approve(scan, payment) # registers the allow-verdict locally
# x402 pay-and-retry now succeeds. ANY other payment authorization the wallet
# is asked to sign — different recipient, amount, asset, chain, or nonce —
# raises PaySafeEnforcementError before the signature exists.
How the binding works: the wrapped signer intercepts EIP-712 payment authorizations (EIP-3009 TransferWithAuthorization/ReceiveWithAuthorization — the x402 "exact" scheme — plus ERC-2612 Permit; eth-account's positional, keyword, and full_message= call shapes are all recognized), reconstructs the payment from the typed data itself, and recomputes the commitment sha256(network|pay_to|asset|amount|nonce). Only a live approval for exactly that commitment lets the signature happen — so "scan payment A, sign payment B" fails structurally, not by convention.
Guarantees and options: approvals are verified against the pinned verdict key at approve() time (tampered/replayed/expired attestations raise), are single-use by default (reusable=True to opt out), expire with the attestation (tighten with max_age_s), gate on allow-only verdicts (allow_flagged=True to accept flags; accept_overrides=True to accept human-approved override:allow verdicts from step-up approvals — opt-in because a self-webhooked agent could approve its own flags), and can be revoke()d. Unrecognized typed data passes through by default; strict_types=True makes the signer deny-by-default. Enforcement is fully local and fail-closed — if PaySafe is unreachable, nothing new can be approved. For flags that pause for a human (scan["approval"] present), client.wait_for_approval(scan, payment=payment) polls until the operator decides and returns the signed override.
The gate is cross-validated in CI: an attestation signed by the production Node signer authorizes a signature through the Python enforcer end to end, so both SDKs enforce identical semantics.
Scope note: this guards the typed-data path x402 uses. If your signer also exposes raw sign_transaction, gate that at your policy layer too.
Paying for scans and plans (x402)
Your first 100 calls per key are free. Beyond that, pass a payment-capable transport — any callable (method, url, headers, body_bytes) -> (status, headers, body_bytes) that settles x402 challenges (e.g. wrapping an x402 Python client):
paysafe = PaySafeClient(agent_id="my-agent", transport=my_x402_transport, auto_renew=True)
paysafe.get_plans() # free catalog: Starter / Pro ($4.99/30d, $0.005/scan) / Scale ($19.99/30d, $0.002/scan)
paysafe.subscribe("pro") # pays $4.99 over x402, upgrades this key for 30 days
Plans raise your own velocity/spend thresholds and cut per-scan price. Replay detection, merchant pinning, asset verification, and PII scanning are identical on every tier — no plan can relax them.
Reputation
paysafe.report("0xbad...", "non_delivery", "paid, no data") # always free
paysafe.reputation("0xsomeone...") # report summary (paid / free-tier)
API surface
PaySafeClient — scan_outgoing, scan_incoming, guard_outgoing, guard_incoming, observe, note_planning, note_user_instruction, get_plans, subscribe, report, reputation, ensure_api_key, verdict_key, plus free_calls_remaining / plan state.
Payment path — wrap_transport_with_paysafe, payment_from_offer.
Enforcement — PaySafeEnforcer (approve, guard_signer, assert_approved, revoke, clear), payment_from_typed_data.
Standalone — verify_attestation, compute_payment_commitment.
Errors — PaySafeError (.status, .body), PaySafeBlockedError (.scan), AttestationError, PaySafeEnforcementError (.commitment, .primary_type).
MIT. PaySafe is advisory and non-custodial: this SDK never touches your keys, wallet, or funds.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file paysafe_x402-0.4.0.tar.gz.
File metadata
- Download URL: paysafe_x402-0.4.0.tar.gz
- Upload date:
- Size: 24.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea70ab296d2ac8d79aa5aebd2c5aba1325bdf13ca9cb566ad0bd3c03dda69652
|
|
| MD5 |
d12d477614f4f44175552f8a5426e0d4
|
|
| BLAKE2b-256 |
3a896789dd26786436ac65de4ee1e7b4c28f966ce3cfa3e9092b8b3a3a633e71
|
Provenance
The following attestation bundles were made for paysafe_x402-0.4.0.tar.gz:
Publisher:
publish-pypi.yml on corbinallison/paysafe
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
paysafe_x402-0.4.0.tar.gz -
Subject digest:
ea70ab296d2ac8d79aa5aebd2c5aba1325bdf13ca9cb566ad0bd3c03dda69652 - Sigstore transparency entry: 2187378816
- Sigstore integration time:
-
Permalink:
corbinallison/paysafe@4f9dd6b150e383c0fc67825daf63b47eed791f9c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/corbinallison
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@4f9dd6b150e383c0fc67825daf63b47eed791f9c -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file paysafe_x402-0.4.0-py3-none-any.whl.
File metadata
- Download URL: paysafe_x402-0.4.0-py3-none-any.whl
- Upload date:
- Size: 18.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e3c0d8e1609efd63abd8047579f9b52972993336407b4877363912ecbf46405
|
|
| MD5 |
2a0ac1b2d5334505263c276711ac8ab0
|
|
| BLAKE2b-256 |
11561d3bd27d02c4b3b319a4935c7ca03b571ef6ce43878a40b83588882a8e29
|
Provenance
The following attestation bundles were made for paysafe_x402-0.4.0-py3-none-any.whl:
Publisher:
publish-pypi.yml on corbinallison/paysafe
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
paysafe_x402-0.4.0-py3-none-any.whl -
Subject digest:
7e3c0d8e1609efd63abd8047579f9b52972993336407b4877363912ecbf46405 - Sigstore transparency entry: 2187378826
- Sigstore integration time:
-
Permalink:
corbinallison/paysafe@4f9dd6b150e383c0fc67825daf63b47eed791f9c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/corbinallison
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@4f9dd6b150e383c0fc67825daf63b47eed791f9c -
Trigger Event:
workflow_dispatch
-
Statement type: