x402-algorand
Algorand (AVM) exact payment scheme plugin for the official x402 Python SDK.
This project is not affiliated with GoPlausible's x402-avm packages. It started as a fork of GoPlausible/x402-avm 2.0.2 (see NOTICE).
x402-algorand provides Algorand Virtual Machine (AVM) payment scheme support for the official x402 payment protocol, implementing the exact scheme on Algorand specification. It supports Algorand Standard Asset (ASA) transfers such as USDC, native ALGO payments, atomic transaction groups with fee pooling, and integration with x402Client spend controls.
Requirements
- Python 3.10+
x402>= 2.28, < 3.0py-algorand-sdk>= 2.6
Installation
pip install x402-algorand
Installing x402-algorand automatically pulls the official x402 package.
Quickstart
Register the Algorand exact scheme with the official x402Client, x402ResourceServer
and x402Facilitator. PayerAvmSigner and ServerAvmSigner are ready-made signers
backed by an Algorand private key or 25-word mnemonic.
Paid API (FastAPI, in-process facilitator, AlgoKit LocalNet)
The server's account is both the payee and the fee payer, so the buyer pays exactly the price with no network fee.
import os
from algosdk.v2client.algod import AlgodClient
from fastapi import FastAPI
from x402 import x402Facilitator, x402ResourceServer
from x402.http.middleware.fastapi import payment_middleware
from x402_algorand import ServerAvmSigner, caip2_from_genesis_hash
from x402_algorand.exact import register_exact_avm_facilitator, register_exact_avm_server
algod = AlgodClient("a" * 64, "http://localhost:4001") # AlgoKit LocalNet
network = caip2_from_genesis_hash(algod.suggested_params().gh)
signer = ServerAvmSigner.from_mnemonic(os.environ["SERVER_MNEMONIC"], algod)
facilitator = x402Facilitator()
register_exact_avm_facilitator(facilitator, signer, network)
server = x402ResourceServer(facilitator)
register_exact_avm_server(server)
app = FastAPI()
routes = {
"GET /weather": {
"accepts": [
{
"scheme": "exact",
"price": {"amount": "10000", "asset": "0"}, # 0.01 ALGO
"network": network,
"pay_to": signer.address,
}
]
}
}
app.middleware("http")(payment_middleware(routes, server))
@app.get("/weather")
async def weather():
return {"city": "Boston", "temperature": 68}
Paying client (httpx)
import asyncio
import os
from x402 import x402Client
from x402.http.clients.httpx import wrapHttpxWithPayment
from x402_algorand import PayerAvmSigner
from x402_algorand.exact import register_exact_avm_client
async def main() -> None:
client = x402Client()
register_exact_avm_client(
client,
PayerAvmSigner.from_mnemonic(os.environ["PAYER_MNEMONIC"]),
algod_url="http://localhost:4001",
algod_token="a" * 64,
)
async with wrapHttpxWithPayment(client) as http:
response = await http.get("http://127.0.0.1:8000/weather")
print(response.status_code, response.json())
asyncio.run(main())
On MainNet and TestNet, omit algod_url / the AlgodClient argument to use the public
AlgoNode endpoints configured in NETWORK_CONFIGS (overridable with the ALGOD_MAINNET_URL
and ALGOD_TESTNET_URL environment variables), and use the network constants
ALGORAND_MAINNET_CAIP2 / ALGORAND_TESTNET_CAIP2.
Signers
| Class | Role | Constructors |
|---|---|---|
PayerAvmSigner |
ClientAvmSigner: signs the buyer's payment transaction |
from_private_key(key), from_mnemonic(words) |
ServerAvmSigner |
FacilitatorAvmSigner: signs fee payer transactions, simulates, sends and confirms groups. Also implements ClientAvmSigner. |
from_private_key(key, algod_client=None, *, algod_url=None, algod_token=None), from_mnemonic(...), or ServerAvmSigner([key1, key2], ...) for several fee payers |
- Keys are kept in private attributes and are not included in
repr(), exception messages or logs. ServerAvmSignerwith an algod client checks, on first use for a network, that the node's genesis hash matches that network and raisesNetworkMismatchErrorotherwise. Without one, it uses the algod URL configured for the network.- To use a KMS, HSM or wallet instead, implement the
ClientAvmSigner/FacilitatorAvmSignerprotocols fromx402_algorand.signer.
Errors
Exceptions raised by the package derive from X402AlgorandError. Each also derives from the
built-in type raised before 0.3.0, so existing except ValueError handlers keep working.
| Exception | Also a | Raised when |
|---|---|---|
NetworkError |
ValueError |
A network id is malformed, unsupported, or not registered |
NetworkMismatchError |
NetworkError |
algod is connected to a different network than requested |
TransactionDecodeError |
ValueError |
Bytes or base64 are not a decodable Algorand transaction |
SigningError |
ValueError |
Bad key or mnemonic, or an address the signer does not manage |
SimulationError |
RuntimeError |
algod simulate reports that the group would fail |
Payment verification never raises for an invalid payment: verify() and settle() return a
VerifyResponse / SettleResponse with a protocol invalid_reason / error_reason code (see
x402_algorand.constants, ERR_*). The package logs to the x402_algorand logger.
Specification conformance
The facilitator follows the verification steps of the exact scheme on Algorand:
paymentGrouphas at most 16 transactions; all decode; all share one group id and the genesis hash of the requested network.paymentGroup[paymentIndex]pays exactlyamountofassettopayTo, and is authorized by an ed25519 signature (of the sender or its auth address), a k-of-n multisig, or a logic signature.- Every transaction sent by a facilitator address is a zero-amount
payto itself with noclose/rekeyand a fee of at most 16,000 microAlgos; the advertisedfeePayer's transactions are signed only after these checks. - The group is simulated before it is accepted, and settlement returns the ID of
paymentGroup[paymentIndex].
Additional checks in this package: close-to fields and keyreg transactions are rejected in any
transaction, and rekeys are only accepted as a balanced pair that returns authority to the sender.
asset: "0" (native ALGO via a pay transaction) is an extension of this package; the
specification defines ASA transfers only.
Network Identifiers (CAIP-2)
In accordance with the Algorand CAIP-2 profile, network references use the first 32 characters of the URL-safe base64 encoding of the 32-byte genesis hash:
| Network | Genesis Hash (full base64) | CAIP-2 Identifier |
|---|---|---|
| MainNet | wGHE2Pwdvd7S12BL5FaOP20EGYesN73ktiC1qzkkit8= |
algorand:wGHE2Pwdvd7S12BL5FaOP20EGYesN73k |
| TestNet | SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI= |
algorand:SGO1GKSzyE7IEPItTxCByw9x8FmnrCDe |
| CAIP-2 spec example | mFgazF+2uRS1tMiL9dsj01hJGySEmPN28B/TjjvpVW0= |
algorand:mFgazF-2uRS1tMiL9dsj01hJGySEmPN2 |
| LocalNet (default) | tg0cyjEz14FZK9BTpL2q0TOyFbc0McwYsFJbszmFANk= |
algorand:tg0cyjEz14FZK9BTpL2q0TOyFbc0McwY |
Custom networks can be registered dynamically via register_network(genesis_hash).
Spend Controls
The official x402Client applies spend controls by default to prevent unintentional or excessive payments. ExactAvmScheme integrates directly with these controls via find_default_asset.
Default Allowances and Limits
By default, without custom configuration:
- Allowed Assets:
- ALGO (
asset: "0"): Allowed on all valid Algorand networks (MainNet, TestNet, LocalNet, custom). - USDC: Allowed on MainNet (
asset: "31566704") and TestNet (asset: "10458941").
- ALGO (
- Default Cap:
- Payments are capped at
$1(DEFAULT_MAX_AMOUNT_PER_PAYMENT), corresponding to 1,000,000 base atomic units (6 decimals):- 1 ALGO (1,000,000 microAlgos)
- 1 USDC (1,000,000 atomic units)
- Payments are capped at
- Any payment requesting an unknown asset or an amount exceeding 1,000,000 base units is rejected with
NoMatchingRequirementsError.
Allowing Other ASAs and Custom Caps
You can whitelist additional ASAs, adjust per-asset caps, raise global USD caps, or disable spend controls using client.set_spend_controls(...) or x402Client.from_config(...):
# 1. Allow a custom ASA with an atomic spending cap:
client.set_spend_controls(
{
"allowed_assets": [
{
"network": "algorand:wGHE2Pwdvd7S12BL5FaOP20EGYesN73k",
"asset": "12345678", # ASA ID
"max_amount_per_payment": "5000000", # Base atomic units
}
]
}
)
# 2. Raise the default USD cap for recognized assets (e.g. up to $10 per payment):
client.set_spend_controls({"max_amount_per_payment": "$10"})
# 3. Allow any asset without asset-filtering restrictions:
client.set_spend_controls({"allowed_assets": True})
# 4. Disable all spend controls entirely:
client.set_spend_controls(False)
Migration from 0.1.x
In version 0.1.x, x402-algorand replaced the entire x402 package and used from x402.mechanisms.avm....
In version 0.2.0:
- Use
from x402_algorand import ...instead offrom x402.mechanisms.avm import .... - Core x402 classes (
x402Client,x402ResourceServer,x402Facilitator) are imported directly fromx402. - Network identifiers follow the 32-character CAIP-2 profile. Legacy 44-character IDs (
algorand:<hash>=) remain accepted and are automatically converted.
Related Packages and Links
- Documentation: https://x402-avm.github.io
- Examples Repository: https://github.com/x402-avm/examples
- Python High-Level SDK: ai-economy-sdk
- TypeScript Browser / Node Client: x402-avm-js
- Official x402 Specification: x402 Foundation
Development
Install the package in editable mode with development dependencies:
pip install -e ".[dev]"
Run test tiers:
# 1. Offline tests (unit tests, mock tests, CAIP-2 verification), with coverage
pytest --cov
# 2. LocalNet tests (requires running AlgoKit LocalNet: algokit localnet start)
LOCALNET=1 pytest
# 3. Public network simulations (read-only simulations on MainNet and TestNet)
PUBLIC_NETS=1 pytest
Linting and formatting:
ruff check .
ruff format --check .
Build distribution packages:
python -m build
twine check dist/*
Releases are published to PyPI by the maintainers. .github/workflows/release.yml is prepared
for PyPI trusted publishing and only runs when started manually.
License
Metadata
Release files for x402-algorand 0.3.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 |
|---|---|---|---|---|
| x402_algorand-0.3.0.tar.gz | 55.9 kB | Without provenance | Details for x402_algorand-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| x402_algorand-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for x402_algorand-0.3.0-py3-none-any.whl |
Total release size: 101.7 kB
Release files / x402_algorand-0.3.0.tar.gz
| Download URL | x402_algorand-0.3.0.tar.gz |
|---|---|
| Size | 55.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d1f7f0766018fdf0ae498998c37f8a7b4e99473351c60fdaac41d190a87b3e37
|
|
BLAKE2b-256 checksum How to use checksums |
8b369c29d30be5eeff4dfd4a96a2c38a266ea5639bf1600a4465136d65b4172d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.4
|
Release files / x402_algorand-0.3.0-py3-none-any.whl
| Download URL | x402_algorand-0.3.0-py3-none-any.whl |
|---|---|
| Size | 45.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
26c8d0f5c4866fa8290803308f4eb47ea3d124ab1e389715eb4958a1eded08c6
|
|
BLAKE2b-256 checksum How to use checksums |
0c5d5f7f7efe43757c30dbeabbbf39f8e67a0cfcb1ebf6c83246e3d3086985ad
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.4
|
Security for x402-algorand 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.