Skip to main content

Payments (x402) — @lucid-fdn/pay

The @lucid-fdn/pay package provides a drop-in replacement for the fetch() function, enabling automatic x402 payments for AI agents. It supports multiple blockchain networks, including Base, Ethereum, Arbitrum, Optimism, Polygon, ApeChain, Monad, Solana, and Sui.

Installation

To get started, install the package using npm:

Quick Start

Here’s a quick example to demonstrate how to use @lucid-fdn/pay:

How It Works

  1. Your request is sent to the API as a normal fetch() call.
  2. If the server returns HTTP 402, the response body contains an x402 v2 options[] array — one entry per supported chain and facilitator, each with the recipient address, token, amount, and chain.
  3. @lucid-fdn/pay selects the option matching your wallet’s chain (or falls back to the first option), checks the amount against your budget, sends an on-chain USDC transfer, and retries with X-Payment-Proof (tx hash) and X-Payment-Chain headers.
  4. The server delegates verification to the appropriate facilitator and returns the response.
This setup works with any x402-enabled API, including Lucid TrustGate and MCPGate endpoints.

Supported Chains

API Reference

createLucidFetch(config): lucidFetch

This function returns a configured fetch function that automatically handles x402 payments.

lucidFetch(url, init?, config?)

A one-shot convenience function that accepts an inline config as the third argument. If no config is provided, it behaves as a standard fetch().

Configuration (LucidPayConfig)

Per-Request Overrides (LucidFetchOptions)

This extends the standard RequestInit and allows you to override the wallet or budget on individual requests:

PaymentInfo

The object passed to the onPayment callback includes:

Wallets

ViemWallet (production)

This wallet wraps a viem WalletClient to sign and send real ERC-20 transfers on-chain. Consumers need to bring their own viem dependency.

MemoryWallet (testing)

An in-memory wallet that records payments without sending real transactions. This is useful for tests and local development.

Custom Wallets

You can implement the PaymentWallet interface to use any signing backend:

License

MIT