eip155:84532) and a small price. Once the route returns a paid response end to end, move shared values such as PRIVATE_KEY, PAY_TO, and NETWORK into environment variables.
Prerequisites
Before you build, you need:- Node.js 18 or later.
- A package manager such as
pnpm,npm,yarn, orbun. - A dedicated EVM wallet for your app or agent.
- Testnet gas for the network you want to use.
- Collateral deposited into 4Mica before the wallet signs paid requests.
Install
Install the 4Mica server package when you protect routes. Install the x402 client wrappers when your app needs to pay protected routes.Create wallet
Create a dedicated EVM wallet for the buyer, seller, or agent. For wallet setup, see Wallet. For local development, convert the private key into a viem account:Deposit collateral
Deposit collateral before the buyer signs 4Mica payment guarantees. Collateral backs each guarantee and covers unpaid net positions according to protocol rules. To learn more about collateral setup, see Deposits and Withdrawals.Protect a route
For seller-side protection, usepaymentMiddlewareFromConfig from @4mica/x402/server/express. The middleware returns HTTP 402 responses, verifies retried payment headers, settles the guarantee, and serves the protected route after payment succeeds.
Support payment
Use@x402/fetch or @x402/axios to wrap your HTTP client with automatic 402 handling. The wrappers create unique request IDs, sign payment guarantees using FourMicaEvmScheme, and retry requests without any additional code.
If you are already using Coinbase x402 clients, keep the same wrapper and swap in FourMicaEvmScheme instead of the exact scheme.
Using wrapFetchWithPaymentFromConfig
The convenience config wrapper is the quickest way to get started. Pass your scheme registrations and the wrapper handles everything else.Using wrapAxiosWithPaymentFromConfig
Wrap an existing Axios instance and use it exactly as before.Multi-Network setup
Register multiple networks to route payments to the right scheme based on what the server advertises in its 402 response.Builder pattern
Build a reusablex402Client registry when you need to share the client across multiple wrappers or want explicit control over scheme registration.