@4mica/x402 package ships Express middleware that handles HTTP 402 responses, guarantee verification, and settlement for protected routes.
Prerequisites
- Node.js 18 or later.
- An Express app.
- A seller wallet address that receives payments.
- A buyer wallet with 4Mica collateral if you want to test the paid client.
Install
Create wallet
Create one wallet for the buyer client and one seller address forpayTo. For wallet setup, see Wallet.
Keep the buyer private key in an environment variable:
Deposit collateral
Deposit collateral for the buyer wallet before it calls the protected route. Use Base Sepolia (eip155:84532) while you test, then wait for the deposit to finalize.
To learn more about collateral setup, see Deposits and Withdrawals.
Protect a route
Support payment
What happens
- The buyer requests
/premium-contentwithout payment. - Express returns HTTP 402 with payment requirements.
- The wrapper creates a unique request ID, signs a guarantee, and retries with a payment header.
- The middleware verifies and settles the payment before your handler serves the response.