x402

The X402 extension adds payment support to agents using the X402 Payment Required protocol. It enables agents to require and process payments for their services.

Installation

bun add @regent/x402

Basic Usage

import { createAgent } from '@regent/core';
import { http } from '@regent/http';
import { payments, paymentsFromEnv } from '@regent/x402';

const agent = await createAgent({
  name: 'my-agent',
  version: '1.0.0',
})
  .use(http())
  .use(payments({ config: paymentsFromEnv() }))
  .build();

API Reference

payments(options?)

Creates the payments extension.

PaymentsConfig

paymentsFromEnv()

Configure payments from environment variables:

Environment Variables:

Variable
Description

PAYMENTS_RECEIVABLE_ADDRESS

Wallet address for receiving payments

FACILITATOR_URL

Payment facilitator service URL

NETWORK

Blockchain network identifier

X402 Protocol Flow

Payment Storage

Storage Options

SQLite Storage (Default)

PostgreSQL Storage

In-Memory Storage

Policy Groups

Policy groups control payment limits and restrictions.

PaymentPolicyGroup

Outgoing Limits

Incoming Limits

Policy Configuration File

Create payment-policies.json:

Load policies:

Entrypoint Pricing

Setting Prices

Price Resolution

X402 Fetch

Creating X402-Enabled Fetch

Account from Private Key

Runtime Payment Context

RuntimePaymentContext

X402-Enabled LLM Client

PaymentsRuntime

When the extension is used, agent.payments provides:

Payment Tracker

Rate Limiter

HTTP Headers

402 Payment Required Response

Header
Description

X-Price

Payment amount in USD (e.g., "1.5")

X-Pay-To

Recipient wallet address

X-Network

Blockchain network

X-Facilitator

Payment facilitator URL

Successful Payment Response

Header
Description

X-PAYMENT-RESPONSE

Base64-encoded payment confirmation

Amount Units

All amounts use 6 decimal places (USDC standard):

Utility Functions

parsePriceAmount

extractPayerAddress

validatePaymentsConfig

Supported Networks

EVM Networks

  • base, base-sepolia

  • ethereum, sepolia

  • optimism, arbitrum

  • polygon

SVM Networks

  • solana, solana-testnet

Exports

Last updated