Now the server side. The paywall is one function,
createPaywallServer, that
takes the wallet address to pay and a price, and returns a Node.js node:http
server. Read the code, then press Run to step through its responses on a
local test network (Surfpool). You run it yourself in the next step.
1. The messages
x402 messages are JSON objects, sent base64-encoded in HTTP headers. These are the types the server and client share:paywall-api/x402.ts
PaymentRequirementsis one way to pay.maxAmountRequiredis the price in the token’s base units,assetis the token’s mint address, andpayTois the wallet that gets paid.PaymentPayloadis what the client sends back in theX-PAYMENTheader: a fully signed transaction, base64-encoded.SettlementResponseis the receipt the server returns in theX-PAYMENT-RESPONSEheader.transactionis the transaction’s signature.networkis a CAIP-2 identifier.solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1is Solana devnet.
2. The server
paywall-api/server.ts
An unpaid request, printed by paywall-api/run.tsLocal test network (Surfpool)
await fetch(url)
GET /api/forecast -> 402 X-PAYMENT header is required
price: 1000 base units of 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU (0.001 USDC)
payTo: 4SnM7PENj3Amhyc6rRA5XG2rq3NpiyZbPvpgAQbA3FLE network: solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1
402 Payment Required
- No payment, no data. A request without an
X-PAYMENTheader gets402withaccepts: [requirements]. That is the output above. - Decode, never trust. The header is untrusted input. Any decoding
problem is a
402 malformed X-PAYMENT header, not a crash. The server also refuses transactions that use address lookup tables, because it only checks accounts listed in the transaction itself. - Check that it pays the price. The server looks for a USDC
TransferCheckedinstruction whose destination is thepayTowallet’s USDC token accountToken accountsWallets don't hold tokens directly. Each token (USDC, or your own asset) gets a small account owned by the wallet, and transfers move between these accounts.Learn more (Token accounts: Wallets don’t hold tokens directly. Each token (USDC, or your own asset) gets a small account owned by the wallet, and transfers move between these accounts.) and whose amount is at least the price. - One payment, one response. The transaction’s
signatureSignaturesEvery transaction is signed by the wallets it spends from. The first signature doubles as the transaction's ID, which is what your code gets back and looks up later.Learn more (Signatures: Every transaction is signed by the wallets it spends from. The first signature doubles as the transaction’s ID, which is what your code gets back and looks up later.) is its
ID. The server remembers every signature it has accepted and refuses a
repeat with
payment already used. - Settle. The server sends the transaction to the network and waits until
it reaches
confirmed. - Check again on the network.
verifyPayment, the same check as in the checkout tutorial, reads the settled transaction back and confirms thatpayToreceived the amount. Only then does the server return200with the data and anX-PAYMENT-RESPONSEheader.
The client pays the network fee here, because it signs as the fee payer. With
the x402 SDK, a facilitator can pay it instead (step 4).