This step puts the server in a project on your machine, adds the client that
pays it, and runs both against a local test network or devnet.
1. Create the project
You need Node.js 20.18 or later. The paywall shares thesolana-payments
project with the Build a checkout tutorial: its files go in paywall-api/,
next to the checkout/ folder it reuses two files from. Run every command from
solana-payments. If you did the checkout tutorial, skip to
step 2. Otherwise create the project:
Terminal
"type": "module" lets the scripts use top-level await. tsx runs the
TypeScript files directly. The examples are tested with @solana/kit 8.4 and
@solana-program/token 0.17.
The server and client read their network settings from environment variables
and default to devnet. Save this as checkout/config.ts:
checkout/config.ts
verifyPayment. Save it as checkout/verify-payment.ts:
checkout/verify-payment.ts
Output of verifyPayment() with the right and a wrong amountLocal test network (Surfpool)
await verifyPayment({ ...order, signature, mint: USDC_MINT }){ paid: true }
await verifyPayment({ ...order, amount: 2_000_000n, signature, mint: USDC_MINT }){
paid: false,
reason:
'amount mismatch: expected 2000000, received 1000000'}
Right amount: paidWrong amount: refused
2. Save the server
Save the two files from the previous step aspaywall-api/x402.ts and
paywall-api/server.ts:
paywall-api/x402.ts
x402.ts reads X402_NETWORK the same way as config.ts, with the devnet
CAIP-2 identifier as the default.
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
3. Pay from a client
fetchWithPayment works like fetch, but pays when it gets a 402.
createPaymentHeader picks the USDC option, refuses any price above
maxAmount, and signs a USDC transfer to payTo. fetchWithPayment then
retries with the signed transaction in the X-PAYMENT header. Save this as
paywall-api/client.ts:
paywall-api/client.ts
maxAmount, the most a single request can cost, on every client you give
a wallet to, including AI agents.
4. Run it on your machine
This script plays both sides. It starts the paywall server on port 3000 with a price of 0.001 USDC, paid to a new wallet whose USDC account it opens, then makes four requests: one without paying, one that pays, the same payment sent again, and one from a client whosemaxAmount of 500 is below the price of
1,000. Save it as paywall-api/run.ts:
paywall-api/run.ts
Output of npx tsx paywall-api/run.tsLocal test network (Surfpool)
npx tsx paywall-api/run.ts
GET /api/forecast -> 402 X-PAYMENT header is required
price: 1000 base units of 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU (0.001 USDC)
payTo: 4SnM7PENj3Amhyc6rRA5XG2rq3NpiyZbPvpgAQbA3FLE network: solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1
GET /api/forecast (paid) -> 200
{"city":"Lisbon","tomorrow":"Sunny, 24°C"}settled signature: 4JZW7fB3h53qP6f66eat4cwDVDFvxwTtKWbV8tfJJHVo4eHzK9riSvetQSwPEtungFxfbWC968JoyDoqRo7XauLZ
fee: 5000 lamports
GET /api/forecast (replayed X-PAYMENT) -> 402 payment already used
client refused: price 1000 is above maxAmount 500; not paying
402 Payment Required200 OK402 Payment already usedRefused: price above maxAmount
CLIENT_KEYPAIR, or the Solana CLI default ~/.config/solana/id.json. If you
don’t have one yet, save this as keygen.ts in solana-payments. It writes a
new key file in the same format and prints its address:
keygen.ts
Terminal
-
Install the Surfpool CLI
and start a local network that copies accounts from devnet, so the devnet
USDC mint the code uses exists locally. It airdrops SOL to
~/.config/solana/id.jsonwhen it starts (create the key file first):Terminal -
In another terminal, in
solana-payments, give the client 1 test USDC with Surfpool’ssurfnet_setTokenAccountcheatcode. Replace<client address>with the address of your key file:Terminal -
Point the code at Surfpool.
X402_NETWORKstays at its devnet default, because Surfpool copies devnet:Terminal
Terminal
402 with the price, 200 with the forecast and the settled
signature, 402 payment already used for the same X-PAYMENT header, and the
over-cap client refusing without signing anything. Port 3000 in use? Run it
with another port, for example PORT=3001 npx tsx paywall-api/run.ts.
Run the tests instead
The code on these pages and its tests live in the docs repository underpackages/docs-examples/paths/payments/paywall-api. The test starts its own
Surfpool network and funds a test client, so you need no Surfpool CLI, keys,
SOL, or USDC. It also checks that malformed X-PAYMENT headers get 402
without stopping the server, and that payTo is credited exactly the price:
Terminal
The full example is in
packages/docs-examples/paths/payments/paywall-api.If you have no Solana CLI key file at
~/.config/solana/id.json, the test
setup creates one there. To keep it out of your home folder, run the test
with HOME set to a temporary folder.5. Move it into your app
Your server needs four files:paywall-api/x402.ts, paywall-api/server.ts,
checkout/config.ts, and checkout/verify-payment.ts. The project that pays
needs paywall-api/client.ts, paywall-api/x402.ts, and checkout/config.ts.
If you put them all in one folder, change the imports ../checkout/config and
../checkout/verify-payment to ./config and ./verify-payment. Install
@solana/kit and @solana-program/token. Then call
createPaywallServer({ payTo, price }) with your wallet address and a price in
USDC base units (1_000n is 0.001 USDC), and call listen on the server it
returns. The payTo wallet’s USDC account must exist before the first payment.
The handler only uses the request method, URL, and X-PAYMENT header, and it
sets a status, headers, and a JSON body. To use Express, Fastify, or Hono, move
the body of the createServer callback into a route handler for your paid
route. The usedSignatures set must be shared by every request, as it is here.
The server answers a paid request only after the payment reaches confirmed,
so on a public network expect roughly the confirmation time added to your
normal response time.