By the end of this page you will have a web page whose premium report unlocks after the visitor pays 4,000 microALGO from their wallet, and you will know how to plug in any Algorand wallet. 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:
  1. Fetches suggested parameters from algod (algod.getTransactionParams().do()).
  2. Picks the exact offer for algod’s network with selectRequirement. A server can offer several networks (say MainNet USDC and TestNet ALGO); if none matches the wallet’s, it throws NetworkMismatchError instead of signing a useless payment.
  3. Checks the offer against the spend controls.
  4. Builds the group with buildExactAvmGroup: the two-transaction group when the offer has extra.feePayer, otherwise one payment that pays the normal fee.
  5. Asks your signing function to sign only the buyer’s transaction.
  6. Encodes the signed and unsigned transactions into the PAYMENT-SIGNATURE value.
The browser submits nothing to the chain. The server verifies, runs the service and settles.

Build the paywall

The files are in localnet/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 / and protects GET /api/premium with the official x402 middleware, as on Paywall an HTTP API.
localnet/browser_paywall/server.py
Source: examples/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
Source: examples/localnet/browser_paywall/app.jsBundle it with esbuild (the repository’s npm script writes 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
Source: examples/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
Source: examples/localnet/browser_wallet.ts
To let returning visitors prove who they are without paying, sign an identity proof with the same wallet.

Spend controls

The server chooses the amount in a 402, 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