x402
  • Welcome to x402
  • x402 Landing Page
  • CDP's x402 Docs
  • FAQ
  • Getting Started
    • Quickstart for Buyers
    • Quickstart for Sellers
  • Core Concepts
    • HTTP 402
    • Client / Server
    • Facilitator
    • Wallet
  • Guides
    • MCP Server with x402
Powered by GitBook
On this page
  • Prerequisites
  • 1. Install Dependencies
  • 2. Create a Wallet Client
  • 3. Make Paid Requests Automatically
  • 4. Error Handling
  • Summary
  1. Getting Started

Quickstart for Buyers

PreviousFAQNextQuickstart for Sellers

Last updated 15 days ago

This guide walks you through how to use x402 to interact with services that require payment. By the end of this guide, you will be able to programmatically discover payment requirements, complete a payment, and access a paid resource.

Prerequisites

Before you begin, ensure you have:

  • A crypto wallet with USDC (any EVM-compatible wallet, e.g., )

  • and npm installed

  • A service that requires payment via x402

Note We have pre-configured , including examples for fetch, Axios, and MCP.

1. Install Dependencies

Install or :

npm install x402-axios
# or
npm install x402-fetch

2. Create a Wallet Client

Create a wallet client using CDP's Wallet API or :

import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { baseSepolia } from "viem/chains";

// Create a wallet client (using your private key)
const account = privateKeyToAccount("0xYourPrivateKey"); // we recommend using an environment variable for this
import { CdpClient } from "@coinbase/cdp-sdk";
import { createWalletClient, http } from "viem";
import { baseSepolia } from "viem/chains";
const cdp = new CdpClient();
const account = await cdp.evm.createAccount();

3. Make Paid Requests Automatically

You can use either x402-fetch or x402-axios to automatically handle 402 Payment Required responses and complete payment flows.

x402-fetch extends the native fetch API to handle 402 responses and payment headers for you.

import { wrapFetchWithPayment, decodeXPaymentResponse } from "x402-fetch";

const fetchWithPayment = wrapFetchWithPayment(fetch, account);

fetchWithPayment(url, { //url should be something like https://api.example.com/paid-endpoint
  method: "GET",
})
  .then(async response => {
    const body = await response.json();
    console.log(body);

    const paymentResponse = decodeXPaymentResponse(response.headers.get("x-payment-response")!);
    console.log(paymentResponse);
  })
  .catch(error => {
    console.error(error.response?.data?.error);
  });

Features:

  • Automatically handles 402 Payment Required responses

  • Verifies payment and generates payment headers

  • Retries the request with proof of payment

  • Supports all standard fetch options

x402-axios adds a payment interceptor to Axios, so your requests are retried with payment headers automatically.

import { withPaymentInterceptor, decodeXPaymentResponse } from "x402-axios";
import axios from "axios";

// Create an Axios instance with payment handling
const api = withPaymentInterceptor(
  axios.create({
    baseURL, // e.g. https://api.example.com
  }),
  account,
);

api
  .get(endpointPath) // e.g. /paid-endpoint
  .then(response => {
    console.log(response.data);

    const paymentResponse = decodeXPaymentResponse(response.headers["x-payment-response"]);
    console.log(paymentResponse);
  })
  .catch(error => {
    console.error(error.response?.data?.error);
  });

Features:

  • Automatically handles 402 Payment Required responses

  • Retries requests with payment headers

  • Exposes payment response headers

4. Error Handling

Both x402-fetch and x402-axios will throw errors if:

  • The request configuration is missing

  • A payment has already been attempted for the request

  • There is an error creating the payment header

Summary

  • Install either x402-fetch or x402-axios

  • Use the provided wrapper/interceptor to make paid API requests

  • Payment flows are handled automatically for you


References:

Create a wallet client (using or CDP Wallet API)

For questions or support, join our .

CDP Wallet
Node.js
examples available in our repo
x402-axios
x402-fetch
viem
viem
x402-fetch npm docs
x402-axios npm docs
Discord