Skip to main content

Constructor Options

All options are optional. An empty {} creates a read-only instance (explore mode).

Credentials

Coinbase CDP (x402 protocol)

The three Coinbase fields must all be provided together. If any are missing, the SDK runs in explore mode: quote(), discover(), diagnose(), getBudget(), and getHistory() work normally, but fetch() will fail on paid endpoints.
The Coinbase SDK (@coinbase/cdp-sdk) is loaded lazily. It is only imported when the first payment is executed, not at construction time.

NWC (L402 protocol)

Adding an NWC connection string enables the L402 (Lightning) adapter alongside x402. The SDK auto-detects which protocol each endpoint uses.
The NWC connection string must start with nostr+walletconnect://. Get one for free from Coinos, Primal, or any wallet listed on nwc.dev. See Installation for step-by-step instructions.

Wallets

The wallets array configures multiple payment methods. Each wallet targets a specific protocol. The SDK selects the appropriate wallet based on endpoint protocol detection.
Legacy flat credentials (coinbaseApiKeyId, nwcConnectionString at root level) still work for backward compatibility. The wallets array is preferred for new integrations.

Registry URL

The registry is the source of truth for discover(). Points to the BoltzPay registry by default.

Session Deposit

Cap the maximum deposit per payment session. Useful for controlling exposure on long-running agents.
Actual deposit = min(available budget, sessionMaxDeposit, user-provided maxDeposit). Defaults to $10 if no constraints are set.

Budget

Set spending limits to protect against unexpected costs. All amounts are in USD.

Configuration

Amounts accept both strings ("5.00") and numbers (5). All fields are optional. If no budget is configured, payments proceed without caps.

How Enforcement Works

Budget checks run before every payment:
  1. Checks perTransaction: rejects if the amount exceeds the per-request cap.
  2. Checks daily: rejects if dailySpent + amount > daily limit.
  3. Checks monthly: rejects if monthlySpent + amount > monthly limit.
If any check fails, a BudgetExceededError is thrown and the payment is not executed. After a successful payment, the amount is recorded against both daily and monthly totals.

Checking Budget State

getBudget() returns a BudgetState:

Resetting Budget

Resets the daily counter to zero. The monthly counter is not affected. Useful if you implement your own daily reset cron.
Budget state is in-memory by default. Enable persistence to keep budget and history across restarts:
When persistence is enabled, budget counters and payment history survive process restarts. Daily budgets auto-reset on calendar day change. Files are stored as budget.json and history.jsonl in the configured directory.

Events

Subscribe with agent.on(event, listener).

payment

Emitted after every successful payment.

budget:warning

Emitted when spending reaches the warningThreshold (default 80%) of a daily or monthly limit.

budget:exceeded

Emitted when a payment is blocked because it would exceed a budget limit.

error

Emitted on any error during payment (budget exceeded, protocol error, network error).

session:open

Emitted when a payment session channel is opened.

session:voucher

Emitted when a voucher is issued within a session.

session:close

Emitted when a session is closed and the channel is settled.

session:error

Emitted on session-level errors (channel failures, deposit errors).

mcp:payment

Emitted when a payment is triggered by an MCP tool call.

Payment History

The SDK keeps a history of payments (last 500 by default). When persistence is enabled, history survives restarts. Returns readonly PaymentRecord[] (same type as the payment event payload).

Multi-Chain

Supported Chains

All payments use USDC as the settlement currency, regardless of chain.

Auto-Negotiation

When you call agent.fetch(url), the SDK probes the endpoint, receives one or more accept options (each specifying a chain, amount, and payment address), and selects the best match. The selection algorithm (selectBestAccept):
  • Filters out unsupported chains.
  • If preferredChains is set, narrows to those (falls back to all supported if none match).
  • Picks the cheapest option among remaining candidates.
  • On ties, EVM wins over SVM.

Chain Preferences

Set a default preference at construction time:
Override per-request (takes priority over config-level preferredChains):
Via the CLI:

Network

Testnet vs Mainnet

Or via environment variable:
Testnet endpoints only accept testnet payments and vice versa. The Nickel Joke endpoint (nickeljoke.vercel.app) runs on Base Sepolia and is useful for testing without spending real funds.
Get free testnet USDC from the Circle faucet (select Base Sepolia). No gas fees needed — x402 uses EIP-3009 (gasless signatures).

Log Level

Timeouts

Configure per-phase timeouts for protocol detection, quoting, and payment execution.

Environment Variables

The CLI and MCP server read configuration from environment variables. The SDK constructor does not read env vars automatically. You pass them explicitly.

Example .env file

The MCP server and CLI both use dotenv, so they automatically read from a .env file in your working directory.