Skip to main content
After you complete this guide, you will have a wallet ready to pay with 4Mica credit and a client that can handle HTTP 402 responses automatically.

Before you begin

4Mica’s approach is to deposit collateral once, sign payment guarantees as requests happen, and settle net positions through clearing cycles instead of sending an on-chain transaction for every paid request. To start, you need:
  • A wallet private key or supported signer for your agent or service.
  • Testnet ETH for gas if you are using Base Sepolia or Ethereum Sepolia.
  • Collateral in ETH, USDC, or USDT, depending on the network and route.
  • Node.js if you want to follow the example below.
Use Base Sepolia (eip155:84532) for development. Move to Base (eip155:8453) when you are ready for production traffic. If you do not have these yet, start with a test wallet. Create a new wallet in MetaMask, Coinbase Wallet, Rabby, or another EVM wallet, then export the private key for that wallet only. Use this key for development and keep your main wallet separate. Add testnet gas from a Base Sepolia faucet or an Ethereum Sepolia faucet, then deposit a small amount of supported collateral into 4Mica. For stablecoin testing, use the token and network supported by the route you plan to call.
PRIVATE_KEY=0xYourPrivateKey
Keep private keys in environment variables instead of committing them to your code.
For production, use a dedicated service wallet, hardware-backed signer, MPC wallet, or hosted key management system. Rotate keys regularly and limit how much collateral any one wallet can control.

Register collateral

Deposit collateral into 4Mica before your first paid request. This registers the wallet and gives 4Mica a balance to secure future payment guarantees. During registration, choose the same network you plan to use in your x402 client. Follow the deposits and withdrawals guide for the collateral flow, then wait for the deposit to finalize before continuing.

Install the packages

Install the 4Mica x402 client, the x402 fetch wrapper, and viem:
pnpm install @4mica/x402 @x402/fetch viem

Make your first paid request

Wrap your normal fetch client with the 4Mica scheme. When the API returns HTTP 402 payment requirements, the wrapper creates a unique request ID, signs the guarantee, retries the request, and returns the paid response.
import { wrapFetchWithPaymentFromConfig } from "@x402/fetch";
import { FourMicaEvmScheme } from "@4mica/x402/client";
import { privateKeyToAccount } from "viem/accounts";

const account = privateKeyToAccount(process.env.PRIVATE_KEY);
const scheme = await FourMicaEvmScheme.create(account);

const fetchWithPayment = wrapFetchWithPaymentFromConfig(fetch, {
  schemes: [{ network: "eip155:84532", client: scheme }],
});

const response = await fetchWithPayment("https://api.example.com/data");
const data = await response.json();
If the request succeeds, your client received the protected response and the seller received a settled payment guarantee. For the full setup, see the Buyer Quick Start.

Protect a route

If you are building the seller side, protect one route with the 4Mica payment middleware. Start with a small test price.
app.use(paymentMiddlewareFromConfig({
  "GET /premium-content": {
    accepts: {
      scheme: "4mica-credit",
      price: "$0.10",
      network: "eip155:84532",
      payTo: "0xYourAddress",
    },
  },
}));
The middleware returns HTTP 402 responses, verifies payment payloads, settles guarantees, and serves the protected route after payment succeeds. For the full setup, see the Seller Quick Start.

Confirm it worked

A successful first run should end with:
  • The buyer receiving a normal HTTP response from the protected route.
  • The seller receiving a settled guarantee for the request amount.
  • A payable guarantee that can enter a clearing cycle.

See it in action

Use a demo app or the REPL to see the full payment loop before wiring 4Mica into your own product. Start with testnet collateral, a small protected route, and a buyer client that can handle HTTP 402 retries.

Demo apps

Explore example product flows that show buyers, sellers, guarantees, clearing, and settlement together.

REPL

Run the full 4Mica payment flow in an interactive environment.