Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.x402.org/llms.txt

Use this file to discover all available pages before exploring further.

Prerequisites

Before you begin, ensure you have:
  • A crypto wallet with USDC (any EVM or SVM compatible wallet)
  • Node.js and npm, Go, or Python and pip
  • A service that requires payment via x402
Note
There are pre-configured examples available in the x402 repo, including examples for fetch, Axios, Go, and MCP.

1. Install Dependencies

Install the x402 client packages:
# For fetch-based clients
npm install @x402/fetch @x402/evm

# For axios-based clients
npm install @x402/axios @x402/evm

# For Solana support, also add:
npm install @x402/svm

# For Algorand support, also add:
npm install @x402/avm

# For Aptos support, also add:
npm install @x402/aptos

# For Stellar support, also add:
npm install @x402/stellar

2. Create a Wallet Signer

Install the required package:
npm install viem
Then instantiate the wallet signer:
import { privateKeyToAccount } from "viem/accounts";

// Create a signer from private key (use environment variable)
const signer = privateKeyToAccount(process.env.EVM_PRIVATE_KEY as `0x${string}`);

Solana (SVM)

Use SolanaKit to instantiate a signer:
import { createKeyPairSignerFromBytes } from "@solana/kit";
import { base58 } from "@scure/base";

// 64-byte base58 secret key (private + public)
const svmSigner = await createKeyPairSignerFromBytes(
  base58.decode(process.env.SVM_PRIVATE_KEY!)
);

Aptos

Use the Aptos TypeScript SDK to instantiate a signer:
import { Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk";

// Create account from private key
const privateKey = new Ed25519PrivateKey(process.env.APTOS_PRIVATE_KEY!);
const aptosSigner = Account.fromPrivateKey({ privateKey });

Algorand (AVM)

Use the @x402/avm package to instantiate a signer:
import { toClientAvmSigner } from "@x402/avm";

// Create signer from Base64-encoded private key (64-byte: seed + pubkey)
const avmSigner = toClientAvmSigner(process.env.AVM_PRIVATE_KEY!);

Stellar

Use the Stellar SDK to instantiate a signer:
import { createEd25519Signer } from "@x402/stellar";

// Create signer from private key (S... format)
const stellarSigner = createEd25519Signer(
  process.env.STELLAR_PRIVATE_KEY!,
  "stellar:testnet"
);

3. Make Paid Requests Automatically

@x402/fetch extends the native fetch API to handle 402 responses and payment headers for you. Full example here
import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client, x402HTTPClient } from "@x402/core/client";
import { ExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";

// Create signer
const signer = privateKeyToAccount(process.env.EVM_PRIVATE_KEY as `0x${string}`);

// Create x402 client and register EVM scheme
const client = new x402Client();
client.register("eip155:*", new ExactEvmScheme(signer));

// Wrap fetch with payment handling
const fetchWithPayment = wrapFetchWithPayment(fetch, client);

// Make request - payment is handled automatically
const response = await fetchWithPayment("https://api.example.com/paid-endpoint", {
  method: "GET",
});

const data = await response.json();
console.log("Response:", data);

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

Multi-Network Client Setup

You can register multiple payment schemes to handle different networks:
import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client } from "@x402/core/client";
import { ExactEvmScheme } from "@x402/evm/exact/client";
import { ExactSvmScheme } from "@x402/svm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
import { createKeyPairSignerFromBytes } from "@solana/kit";
import { base58 } from "@scure/base";

// Create signers
const evmSigner = privateKeyToAccount(process.env.EVM_PRIVATE_KEY as `0x${string}`);
const svmSigner = await createKeyPairSignerFromBytes(
  base58.decode(process.env.SVM_PRIVATE_KEY!)
);

// Create client with multiple schemes
const client = new x402Client();
client.register("eip155:*", new ExactEvmScheme(evmSigner));
client.register("solana:*", new ExactSvmScheme(svmSigner));

const fetchWithPayment = wrapFetchWithPayment(fetch, client);

// Now handles both EVM and Solana networks automatically!

// For Aptos support, also add:
import { ExactAptosScheme } from "@x402/aptos/exact/client";
import { Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk";

const aptosPrivateKey = new Ed25519PrivateKey(process.env.APTOS_PRIVATE_KEY!);
const aptosSigner = Account.fromPrivateKey({ privateKey: aptosPrivateKey });
client.register("aptos:*", new ExactAptosScheme(aptosSigner));

// For Algorand support, also add:
import { ExactAvmScheme, toClientAvmSigner } from "@x402/avm";

const avmSigner = toClientAvmSigner(process.env.AVM_PRIVATE_KEY!);
client.register("algorand:*", new ExactAvmScheme(avmSigner));

// For Stellar support, also add:
import { ExactStellarScheme, createEd25519Signer } from "@x402/stellar";

const stellarSigner = createEd25519Signer(
  process.env.STELLAR_PRIVATE_KEY!,
  "stellar:testnet"
);
client.register("stellar:*", new ExactStellarScheme(stellarSigner));

Supporting the Upto Scheme

If you need to interact with services that use the upto payment scheme (usage-based billing), register the UptoEvmScheme alongside your existing exact schemes. The upto scheme is transparent to the buyer — the SDK handles the max-authorization signing automatically, and you are only charged the actual settled amount (which may be less than the authorized maximum).
import { x402Client } from "@x402/core/client";
import { ExactEvmScheme } from "@x402/evm/exact/client";
import { UptoEvmScheme } from "@x402/evm/upto/client";
import { privateKeyToAccount } from "viem/accounts";

const signer = privateKeyToAccount(process.env.EVM_PRIVATE_KEY as `0x${string}`);

const client = new x402Client();
client.register("eip155:*", new ExactEvmScheme(signer));
client.register("eip155:*", new UptoEvmScheme(signer));
Note: The upto scheme is currently available on EVM networks only. When registered, the SDK automatically selects the correct scheme based on what the server advertises in its 402 response.

4. Discover Available Services (Optional)

Instead of hardcoding endpoints, you can use the x402 Bazaar to dynamically discover available services. This is especially powerful for building autonomous agents.
// Fetch available services from the Bazaar API
const response = await fetch(
  "https://api.cdp.coinbase.com/platform/v2/x402/discovery/resources"
  // "https://facilitator.payai.network/discovery/resources"  // Bazaar from another facilitator
);
const services = await response.json();

// Filter services by criteria
const affordableServices = services.items.filter((item) =>
  item.accepts.some((req) => Number(req.amount) < 100000) // Under $0.10
);

console.log("Available services:", affordableServices);
Learn more about service discovery in the Bazaar documentation.

5. Error Handling

Clients will throw errors if:
  • No scheme is registered for the required network
  • The request configuration is missing
  • A payment has already been attempted for the request
  • There is an error creating the payment header
Common error handling:
try {
  const response = await fetchWithPayment(url, { method: "GET" });
  // Handle success
} catch (error) {
  if (error.message.includes("No scheme registered")) {
    console.error("Network not supported - register the appropriate scheme");
  } else if (error.message.includes("Payment already attempted")) {
    console.error("Payment failed on retry");
  } else {
    console.error("Request failed:", error);
  }
}

Summary

  • Install x402 client packages (@x402/fetch or @x402/axios) and mechanism packages (@x402/evm, @x402/svm, @x402/aptos)
  • Create a wallet signer
  • Create an x402Client and register payment schemes (exact for fixed-price, upto for usage-based billing)
  • Use the provided wrapper/interceptor to make paid API requests
  • (Optional) Use the x402 Bazaar to discover services dynamically
  • Payment flows are handled automatically for you — including upto where you only pay the actual usage

Next Steps:
  • Explore Advanced Concepts like lifecycle hooks for custom logic before/after verification/settlement
  • Explore Extensions like Bazaar for service discovery
References: For questions or support, join our Discord.