Minimal Express TypeScript server with x402 402 paywall.
- Requires USDC payment to access
GET /ping. - Default network:
base-sepolia.
cd basic-402
cp .env.example .env
# edit PAY_TO to your EVM address
npm i
npm run devOpen http://localhost:3000/ping. Without payment, you should get 402 with payment requirements or a paywall page. After paying with an x402 client, response will be { "message": "pong" }.
- Request without payment (JSON):
curl -i -H "Accept: application/json" http://localhost:3000/ping- Request with paywall HTML (browser-like Accept):
curl -i http://localhost:3000/ping- After generating an x402 payment header, include it to get
pong:
# Replace <BASE64_XPAYMENT> with the value produced by an x402 client (e.g., x402-axios)
curl -i -H 'X-PAYMENT: <BASE64_XPAYMENT>' http://localhost:3000/ping- Pretty-print the 402 JSON response:
curl -s -H "Accept: application/json" http://localhost:3000/ping | jq .On success, the response will include the X-PAYMENT-RESPONSE header and a JSON body { "message": "pong" }.
- Configure payer credentials (testnet):
cp .env.example .env
# set PRIVATE_KEY to the payer EVM private key (Base Sepolia recommended)
# set TARGET_URL if different- Generate header:
npm run payment:header
# outputs a long Base64 string- Use the header with curl:
HEADER=$(npm run -s payment:header)
curl -i -H "X-PAYMENT: $HEADER" $TARGET_URLNotes:
- The script first fetches
/pingto readacceptsfrom the 402 JSON, then signs anexactEVM payment usingPRIVATE_KEYand prints the header. - Works by default for
base-sepolia.