x402-avm-js decodes the 402, builds the payment group, has the wallet sign its part, and encodes the PAYMENT-SIGNATURE header. It works with ALGO or any ASA, on every network.
The whole flow in one call
payForRequirement:
- Fetches suggested parameters from algod (
algod.getTransactionParams().do()). - Picks the
exactoffer for algod’s network withselectRequirement. A server can offer several networks (say MainNet USDC and TestNet ALGO); if none matches the wallet’s, it throwsNetworkMismatchErrorinstead of signing a useless payment. - Checks the offer against the spend controls.
- Builds the group with
buildExactAvmGroup: the two-transaction group when the offer hasextra.feePayer, otherwise one payment that pays the normal fee. - Asks your signing function to sign only the buyer’s transaction.
- Encodes the signed and unsigned transactions into the
PAYMENT-SIGNATUREvalue.
Build the paywall
The files are inlocalnet/browser_paywall/ of the examples repository; run pip install -r requirements.txt and npm install from its root. They run on LocalNet.
1
The server
A FastAPI app that serves the page at Source: examples/localnet/browser_paywall/server.py
/ and protects GET /api/premium with the official x402 middleware, as on Paywall an HTTP API.localnet/browser_paywall/server.py
2
The page's client code
app.js requests the report, and on 402 pays with the visitor’s signing function and retries. The page (index.html) imports fetchPremiumContent from the bundle.localnet/browser_paywall/app.js
localnet/browser_paywall/bundle.js, an ES module containing algosdk and x402-avm-js):3
Check it from Node
verify.mjs runs the same calls as app.js, with a LocalNet account instead of a browser wallet. It does not drive a browser.localnet/browser_paywall/verify.mjs
Connect a wallet
payForRequirement hands signing to a callback:
txns are the encoded unsigned transactions of the group and indexes the ones the buyer must sign. Return an array as long as txns, with signed bytes at each index in indexes and null elsewhere; unsigned positions are sent as they are, for the server to sign.
Wallets return signatures either as the full group (as long as txns, null where unsigned) or sparse (as long as indexes). This function, written for @txnlab/use-wallet-react (Pera, Defly, Lute, KMD and others), normalizes both:
localnet/browser_wallet.ts
Spend controls
The server chooses the amount in a402, so a compromised or buggy server could ask for anything. payForRequirement and buildExactAvmGroup check every offer before anything is built or signed. By default, any payment above 1,000,000 base units (1 ALGO or 1 USDC; DEFAULT_MAX_AMOUNT) is refused, matching the Python client. Unlike the Python client, any asset is allowed unless you set allowedAssets.
Pass the options as the last argument:
checkSpendControls(requirement, controls) runs the same check on its own, for example to show a price before asking the wallet to sign. A malformed option throws InvalidOptionsError. Every error class and its code: Errors.
Next
- Pay from Node.js: scripts, services and agents.
payForRequirementreference