Skip to main content
This guide shows how to pay x402-protected endpoints using a Crossmint wallet. The example uses the @x402/core client library to handle the 402 negotiation and payment flow automatically.

Prerequisites

Install dependencies

npm i @crossmint/client-sdk-react-ui @crossmint/wallets-sdk @x402/core @x402/evm viem

Pay an x402 endpoint

x402-payment.tsx
import { EVMWallet } from "@crossmint/client-sdk-react-ui";
import { x402Client, x402HTTPClient, wrapFetchWithPayment } from "@x402/core/client";
import { ExactEvmScheme } from "@x402/evm/exact/client";
import type { Hex } from "viem";
import { CrossmintWallets, createCrossmint } from "@crossmint/wallets-sdk";

const crossmint = createCrossmint({
    apiKey: "<your-server-api-key>",
});

const crossmintWallets = CrossmintWallets.from(crossmint);

const wallet = await crossmintWallets.getWallet(
    "<wallet-address>",
    { chain: "base" }
);

// Use the agent signer
await wallet.useSigner({
    type: "server",
    secret: process.env.CROSSMINT_SIGNER_SECRET,
});

const evmWallet = EVMWallet.from(wallet);

const x402Signer = {
  address: evmWallet.address as `0x${string}`,
  async signTypedData(typedData: any) {
    const { signature } = await evmWallet.signTypedData({
      ...typedData,
      chain: "base",
    });
    return signature as Hex;
  },
};

const client = new x402Client();
client.register("eip155:*", new ExactEvmScheme(x402Signer));

const fetchWithPayment = wrapFetchWithPayment(fetch, client);

const response = await fetchWithPayment("https://api.example.com/protected", {
  method: "GET",
});

const responseText = await response.text();
console.log("Response:", response.status, responseText);

if (response.ok) {
  const httpClient = new x402HTTPClient(client);
  const paymentResponse = httpClient.getPaymentSettleResponse(
    (name) => response.headers.get(name)
  );
  console.log("Payment settled:", paymentResponse);
}

How the code works

1

Create an EVM signer from the Crossmint wallet

The EVMWallet.from(wallet) call wraps the Crossmint wallet into an EVM-compatible interface. A custom signer object is created that delegates signTypedData calls to the Crossmint wallet, which signs on Base using the agent’s delegated permissions.
2

Register the signer with the x402 client

The x402Client is initialized and the EVM scheme is registered for all EIP-155 chains (eip155:*). This tells the x402 client how to sign payments on any EVM chain.
3

Wrap fetch with x402 payment handling

wrapFetchWithPayment returns a modified fetch function that automatically handles the 402 flow: if a request returns 402 Payment Required, it reads the payment terms, signs a stablecoin payment, and retries with the payment proof attached.
4

Verify the payment receipt

After a successful response, getPaymentSettleResponse extracts the settlement receipt from the response headers, confirming the payment was processed onchain.

Learn more

Wallets quickstart

Set up a stablecoin wallet for your agent.