PaymentGate argument; the gate prices each call before it sends the 402, so the caller always sees the exact amount.
Prices are integers in the base units of the gate’s asset: microALGO when asset_id=0, or the ASA’s base units (for USDC, 10-6 USDC). A price of 0 makes a call free and skips the challenge.
Fixed and per-tool prices
default_price, every tool costs the same.
Dynamic prices
Pass a callable instead of a dictionary. It receives the tool name, the call’s arguments and the caller’s verified address (None when the caller sent no valid identity proof), and returns the price.
Bulk discounts
With aChainReader, the gate looks up how much the caller has paid to pay_to in a recent window. Above bulk_threshold, the price is multiplied by bulk_discount. The caller is identified by its identity proof.
ChainReader.for_network("testnet") creates a reader for a public network’s default indexer.
Example
This server (port 9202) has a dictionary price, a price per word, and a 50% discount above 10,000 microALGO of past spend. It makes three calls:localnet/pricing.py
The fee floor
The server’s fee payer pays 2,000 microALGO per settled call (why). An ALGO price at or below that loses money on every call, so the gate guards against it:- A fixed price (
pricesdict ordefault_price) in0 < price <= 2000raisesConfigurationError(aValueError) when the gate is created. - A price that lands in that range at call time (from a callable or after a discount) is raised to 2,001 microALGO, with a warning logged once per tool.
allow_loss=Trueturns both checks off, for deliberate subsidies.- The floor applies only when
asset_id=0. USDC and other ASA prices are not compared with ALGO fees, although the server still pays the fees in ALGO.
Passes
A pass lets a buyer pay once and then call your tools for free forpass_days (30 by default). How passes work. Passes need an indexer, because the gate finds them in the caller’s payment history.
pay_to received it, in the gate’s asset, and whose pass_scopes include its scope, so one vendor’s pass never unlocks another vendor’s tools. The note prefix is the constant PASS_NOTE_PREFIX (x402-pass:).
Buy a pass
Any wallet can buy one: send at least the scope’s price, in the gate’s asset, topay_to with the note x402-pass:<scope>.
paid_mcp_session sends one by default.
Example
On port 9203, a buyer pays 5,000 microALGO forecho, buys a pass with the note x402-pass:listing:echo-1, waits for the indexer to see it, and calls again for free:
localnet/passes.py
Subscription tokens
The gate can also treat holders of an ASA as subscribers. If the caller holdssubscription_asset_id and received it within subscription_max_age_days, calls are free. Set subscription_issuer to count only tokens received from your own issuing account.
Order of evaluation
For each call,PaymentGate.price_for:
- Takes the base price from
prices(dict or callable) ordefault_price. - If it is 0, the call is free.
- If the caller holds a valid pass for one of
pass_scopes, or a valid subscription token, the call is free. - If the caller’s past spend is above
bulk_threshold, appliesbulk_discount. - For ALGO prices, applies the fee floor.
Next
- Identity: how the gate learns who is calling.
ChainReaderreference