Skip to main content
x402 is an internet-native payments protocol that enables agents to send and receive payments for services. This helps developers build autonomous agents and agentic applications directly on SKALE. This guide covers the essential concepts for building agents that interact with x402 payments.

What is x402?

x402 is a payment protocol that extends HTTP with native payment capabilities using the 402 Payment Required status code. It enables:
  • Paywalled Resources: Protect APIs and services behind payments
  • Autonomous Payments: Agents can automatically pay for and access resources
  • ERC-3009 Compatibility: Uses TransferWithAuthorization for gasless payments
  • Multi-Token Support: Accept payments in various ERC-20 tokens

Core Components

1. Facilitator

A facilitator processes x402 payments by exposing endpoints that verify and settle payment authorizations:
  • /verify - Validates payment signatures without executing on-chain
  • /settle - Executes the on-chain payment settlement
Facilitators generally support a 3rd endpoint — /supported — which allows agents to discover resources.
Learn More: Run a Facilitator

2. Payment Middleware

Middleware that protects HTTP endpoints by enforcing the x402 payment handshake:
  • Returns 402 Payment Required when payment is missing
  • Forwards payment to facilitator for verification
  • Allows request to proceed after successful settlement
The middleware is implemented at the seller/merchant level and is able to be fully stateless. This is done through the use of a 3rd party facilitator who handles the above verification and settlement on your behalf.
Learn More: Accept Payments

3. Payment Client

Client libraries that handle the x402 payment flow automatically:
  • Detect 402 Payment Required responses
  • Create and sign payment authorizations
  • Retry requests with proper payment headers
Learn More: Make Payments

Quick Start: Build a Payment-Enabled Agent

Here’s a minimal example of an agent that can access paywalled resources:
import { x402Client, x402HTTPClient } from "@x402/core/client";
import { ExactEvmScheme } from "@x402/evm";
import { privateKeyToAccount } from "viem/accounts";

// Setup wallet
const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);

// Create x402 client
const evmScheme = new ExactEvmScheme(account);
const coreClient = new x402Client().register("eip155:*", evmScheme);
const httpClient = new x402HTTPClient(coreClient);

// Access paywalled resource
async function accessResource(url: string) {
  try {
    const response = await fetch(url);
    
    if (response.status === 402) {
      // Get payment requirements
      const responseBody = await response.json();
      const paymentRequired = httpClient.getPaymentRequiredResponse(
        (name) => response.headers.get(name),
        responseBody
      );
      
      // Create payment payload
      const paymentPayload = await httpClient.createPaymentPayload(paymentRequired);
      const paymentHeaders = httpClient.encodePaymentSignatureHeader(paymentPayload);
      
      // Retry with payment
      const paidResponse = await fetch(url, {
        headers: { ...paymentHeaders }
      });
      
      if (!paidResponse.ok) {
        throw new Error(`Request failed: ${paidResponse.status}`);
      }
      
      return paidResponse.json();
    }
    
    if (!response.ok) {
      throw new Error(`Request failed: ${response.status}`);
    }
    
    return response.json();
  } catch (error) {
    console.error("Error accessing resource:", error);
    throw error;
  }
}

Use Cases

Autonomous Service Access

Build agents that pay for API calls, data feeds, or AI services:
// Agent pays for weather data automatically
const weatherData = await agent.accessResource("https://api.example.com/weather");

Monetize Agent Services

Create services where agents pay to access your AI models or data:
// Protect your AI endpoint with x402
app.get("/ai/analyze", paymentMiddleware, async (c) => {
  const result = await model.analyze(c.req.query);
  return c.json(result);
});

Multi-Agent Economies

Enable agents to pay each other for services in a decentralized economy:
// Agent A requests work from Agent B, pays automatically
const result = await agentA.requestWork(agentB.endpoint, taskData);

Payment Tokens on SKALE

SKALE Base Sepolia Testnet supports various tokens for x402 payments:
TokenAddressDecimals
Axios USD0x61a26022927096f444994dA1e53F0FD9487EAfcf6
Bridged USDC0x2e08028E3C4c2356572E096d8EF835cD5C6030bD6

Next Steps

Resources