Use this page when Python code needs to pay: an MCP client calling paid tools, or a script calling a paywalled HTTP API. In both cases your code makes ordinary calls; the client answers each 402 with a signed payment and retries. You need a funded Algorand account on the server’s network. Its private key is the base64 string returned by algosdk.account.generate_account() or algosdk.mnemonic.to_private_key().

Call paid MCP tools

localnet/quickstart_client.py
Source: examples/localnet/quickstart_client.py paid_mcp_session yields an initialized mcp.ClientSession, so every MCP client method works. Listing tools is free; only tools/call is priced. Run it against the Quickstart server. Use the transport the server serves: .../mcp for transport="streamable-http", .../sse for transport="sse". Full reference: paid_mcp_session.

Spend controls

Both clients on this page use the official x402 client, which checks every payment against spend controls before signing. A refused payment signs nothing:
  • paid_mcp_session raises SpendLimitExceededError (an AIEconomyError, exported from ai_economy_sdk) from the async with block. Its message includes the amount and asset the server asked for; x402’s PaymentError (caused by NoMatchingRequirementsError) is the __cause__. In 0.5.0 the refusal arrived as an ExceptionGroup and over SSE the session could hang; fixed in 0.5.1.
  • x402Client raises NoMatchingRequirementsError; through the httpx and requests wrappers it surfaces as the wrapper’s PaymentError.
Defaults: ALGO ("0") on every Algorand network and USDC on MainNet ("31566704") and TestNet ("10458941"), at most $1 per payment (DEFAULT_MAX_AMOUNT_PER_PAYMENT). A dollar cap is converted with 6 decimals, so $1 is 1,000,000 base units: 1 USDC, and also 1 ALGO whatever ALGO’s market price is. Allow another ASA with its own cap. A per-asset cap is an integer string in base units; a dollar value raises ValueError:
With an x402Client, pass the same dictionary to client.set_spend_controls(...) (or False to turn them off), or use x402Client.from_config(x402ClientConfig(schemes=..., spend_controls=...)). x402Client.__init__ takes only payment_requirements_selector.
Spend controls cap each payment, not the total. Fund the paying account with only what it may spend. For agents, see AI agents.

Pay HTTP APIs

Register the Algorand scheme on an x402Client with a signer, then wrap httpx. This pays the FastAPI paywall on port 9211:
localnet/httpx_client.py
Source: examples/localnet/httpx_client.py
wrapHttpxWithPayment returns an httpx.AsyncClient with an x402AsyncTransport. x402HttpxClient(x402_client, ...) is an httpx.AsyncClient subclass that does the same and accepts the usual httpx arguments; paid_mcp_session uses it. The client does not loop: after a bounded number of paid retries it returns the server’s answer, so a 402 after paying reaches your code. For synchronous code, use x402ClientSync and a requests.Session (install the extra with pip install "x402[requests]"):
wrapRequestsWithPayment comes from the official x402 package and follows its documented usage. The examples repository runs the httpx client, not the requests variant.

Registration options and policies

It also registers the scheme for the x402 v1 names algorand-mainnet and algorand-testnet. A policy is a function (x402_version, requirements) -> requirements that sees every offer before one is chosen. If it removes them all, the client raises NoMatchingRequirementsError and pays nothing:
paid_mcp_session builds its own client and takes spend_controls, not policies.

Signers

The client signs with an object that implements ClientAvmSigner: an address, and a method that signs the transactions at the given indexes of a group and returns None for the others. PayerAvmSigner is backed by a private key: PayerAvmSigner.from_private_key(key) or PayerAvmSigner.from_mnemonic(words). The key stays in a private attribute and never appears in repr(), exception messages or logs. A malformed key or mnemonic raises SigningError, and so does a request to sign a transaction sent by another account. To sign with a KMS, a hardware wallet or a remote service, implement the protocol yourself. DefaultAvmSigner(private_key, algod_client=...) from ai-economy-sdk also implements it.

Pay a challenge by hand

create_payment_payload:
  1. Fetches suggested parameters from algod and checks that algod is on the requirement’s network. If not, it raises NetworkMismatchError("algod is on <id> but the payment is for <id>") (a ValueError). A network with no configured algod and no algod_url raises NetworkError.
  2. Builds the two-transaction group if the requirement has extra.feePayer (the fee payer’s transaction unsigned, yours with fee 0 and signed), or a single payment with the normal fee if not. Your payment is a pay for ALGO, an axfer for an ASA.
  3. Returns only signed transaction bytes; the private key never leaves the signer.

Next

  • AI agents: a budget wallet, limits and price checks for unattended callers.
  • Networks: move from LocalNet to TestNet.