Skip to main content
This page documents the public API of the @circle-fin/x402-batching package, which provides buyer and seller integrations for Circle Gateway nanopayments.
npm install @circle-fin/x402-batching

Buyer APIs

GatewayClient

import { GatewayClient } from "@circle-fin/x402-batching/client";
The primary client for buyers. Handles deposits, gasless payments, withdrawals, and balance queries.

Constructor

new GatewayClient(config: GatewayClientConfig)
ParameterTypeRequiredDescription
config.chainSupportedChainNameYesBlockchain to connect to (for example, 'arcTestnet', 'baseSepolia')
config.privateKeyHexYesPrivate key for signing ('0x...')
config.rpcUrlstringNoCustom RPC URL (overrides the default for the chain)

Properties

PropertyTypeDescription
addressAddressThe account’s wallet address
chainNamestringHuman-readable chain name
domainnumberGateway domain identifier
publicClientPublicClientviem public client instance
walletClientWalletClientviem wallet client instance

deposit(amount, options?)

Deposits USDC from your wallet into the Gateway Wallet contract. This is an onchain transaction that requires gas.
ParameterTypeRequiredDescription
amountstringYesAmount in decimal USDC (for example, '10.5')
options.approveAmountstringNoERC-20 approval amount (defaults to amount)
Returns Promise<DepositResult>:
interface DepositResult {
  approvalTxHash?: Hex;
  depositTxHash: Hex;
  amount: bigint;
  formattedAmount: string;
}

pay<T>(url, options?)

Pays for an x402-protected resource. Handles the full 402 negotiation flow automatically: sends the request, receives payment requirements, signs the authorization, and retries with the payment header.
ParameterTypeRequiredDescription
urlstringYesURL of the x402-protected resource
optionsRequestInitNoStandard fetch options (method, body, headers)
Returns Promise<PayResult<T>>:
interface PayResult<T> {
  data: T;
  amount: bigint;
  formattedAmount: string;
  transaction: string;
  status: number;
}

withdraw(amount, options?)

Withdraws USDC from Gateway to your wallet. Supports same-chain (instant) and crosschain withdrawals.
ParameterTypeRequiredDescription
amountstringYesAmount in decimal USDC
options.chainSupportedChainNameNoDestination blockchain (defaults to same chain)
options.recipientAddressNoRecipient address (defaults to your address)
Returns Promise<WithdrawResult>:
interface WithdrawResult {
  mintTxHash: Hex;
  amount: bigint;
  formattedAmount: string;
  sourceChain: string;
  destinationChain: string;
  recipient: Address;
}

getBalances(address?)

Returns both the wallet’s USDC balance and the Gateway balance.
ParameterTypeRequiredDescription
addressAddressNoAddress to query (defaults to the client’s address)
Returns Promise<Balances>:
interface Balances {
  wallet: { balance: bigint; formatted: string };
  gateway: GatewayBalance;
}

interface GatewayBalance {
  total: bigint;
  available: bigint;
  withdrawing: bigint;
  withdrawable: bigint;
  formattedTotal: string;
  formattedAvailable: string;
}

supports(url)

Checks whether a URL supports Gateway batching before attempting payment.
ParameterTypeRequiredDescription
urlstringYesURL to check
Returns Promise<SupportsResult>:
interface SupportsResult {
  supported: boolean;
  requirements?: Record<string, unknown>;
  error?: string;
}

BatchEvmScheme

import { BatchEvmScheme } from "@circle-fin/x402-batching/client";
A SchemeNetworkClient implementation for Circle Gateway batched payments. Use this when integrating with an existing x402Client instance or building a custom payment flow.

Constructor

new BatchEvmScheme(signer: BatchEvmSigner)
ParameterTypeRequiredDescription
signerBatchEvmSignerYesObject with address and signTypedData function

createPaymentPayload(x402Version, paymentRequirements)

Creates a signed payment payload by constructing and signing an EIP-3009 TransferWithAuthorization message.
ParameterTypeRequiredDescription
x402VersionnumberYesx402 protocol version (use 2)
paymentRequirementsPaymentRequirementsYesRequirements from the 402 response
Returns Promise<PaymentPayload>.

CompositeEvmScheme

import { CompositeEvmScheme } from "@circle-fin/x402-batching/client";
Routes payment requests between Gateway batched payments and standard onchain payments. Use this when your client needs to support both payment methods simultaneously.

Constructor

new CompositeEvmScheme(batchScheme: BatchEvmScheme, fallbackScheme: SchemeNetworkClient)
ParameterTypeRequiredDescription
batchSchemeBatchEvmSchemeYesHandles Gateway batched payments
fallbackSchemeSchemeNetworkClientYesHandles standard onchain payments (must use the 'exact' scheme)

Behavior

  • If the payment requirements include extra.name === "GatewayWalletBatched", delegates to batchScheme.
  • Otherwise, delegates to fallbackScheme.

Usage

import { ExactEvmScheme } from "@x402/evm/exact/client";
import {
  CompositeEvmScheme,
  BatchEvmScheme,
} from "@circle-fin/x402-batching/client";

const composite = new CompositeEvmScheme(
  new BatchEvmScheme(signer),
  new ExactEvmScheme(signer),
);

client.register("eip155:*", composite);

registerBatchScheme

import { registerBatchScheme } from "@circle-fin/x402-batching/client";
Helper that registers a BatchEvmScheme with an x402Client:
registerBatchScheme(client, { signer: account });

Seller APIs

createGatewayMiddleware

import { createGatewayMiddleware } from "@circle-fin/x402-batching/server";
Creates Express-compatible middleware that handles x402 payment negotiation, verification, and settlement.

Configuration

createGatewayMiddleware(config: GatewayMiddlewareConfig)
ParameterTypeRequiredDescription
config.sellerAddressstringYesYour wallet address for receiving payments
config.networksstring | string[]NoNetworks to accept (defaults to all supported)
config.facilitatorUrlstringNoCustom facilitator URL for verify/settle. Routes through this facilitator instead of Circle Gateway directly.
config.descriptionstringNoResource description included in 402 responses

require(price)

Returns Express middleware that requires payment for the route.
ParameterTypeDescription
pricestringPrice in USD (for example, '$0.01' or '0.01')
The middleware attaches payment information to req.payment:
interface PaymentInfo {
  verified: boolean;
  payer: string;
  amount: string;
  network: string;
  transaction?: string;
}

BatchFacilitatorClient

import { BatchFacilitatorClient } from "@circle-fin/x402-batching/server";
A FacilitatorClient implementation that communicates with Circle Gateway’s x402 endpoints. Use this for custom server frameworks or when you need fine-grained control over verification and settlement.

Constructor

new BatchFacilitatorClient(config?: BatchFacilitatorConfig)

verify(payload, requirements)

Verifies a payment signature through the Gateway API.
ParameterTypeDescription
payloadPaymentPayloadThe payment payload from the client
requirementsPaymentRequirementsThe payment requirements you declared
Returns Promise<VerifyResponse>:
interface VerifyResponse {
  isValid: boolean;
  invalidReason?: string;
  payer?: string;
}

settle(payload, requirements)

Submits a payment for batched settlement through the Gateway API. This is the recommended method for production flows because it has low latency and guarantees settlement.
ParameterTypeDescription
payloadPaymentPayloadThe payment payload from the client
requirementsPaymentRequirementsThe payment requirements you declared
Returns Promise<SettleResponse>:
interface SettleResponse {
  success: boolean;
  errorReason?: string;
  payer?: string;
  transaction: string;
}

getSupported()

Fetches the payment kinds (networks and contract addresses) supported by Gateway. Returns Promise<SupportedResponse>:
interface SupportedResponse {
  kinds: Array<{
    x402Version: number;
    scheme: string;
    network: string;
    extra?: { verifyingContract?: string };
  }>;
}

GatewayEvmScheme

import { GatewayEvmScheme } from "@circle-fin/x402-batching/server";
Server-side EVM scheme that extends ExactEvmScheme with Gateway-specific behavior. Required when using x402ResourceServer with BatchFacilitatorClient.

Constructor

new GatewayEvmScheme();
No parameters required. On construction, it automatically registers USDC money parsers for all Gateway-supported networks.

Key behaviors

  • enhancePaymentRequirements(): Merges extra metadata (verifyingContract, name, version) from supported kinds into payment requirements. Sets maxTimeoutSeconds to 345600 (4 days) for batched settlement.
  • USDC money parsers: Automatically converts dollar amounts to USDC atomic units (6 decimals) for all supported networks using the USDC addresses from CHAIN_CONFIGS.
The base ExactEvmScheme discards the extra field from supported kinds when building payment requirements. Gateway clients require extra.verifyingContract to construct valid EIP-712 signatures. GatewayEvmScheme preserves this data.

Usage

import { x402ResourceServer } from "@x402/express";
import {
  BatchFacilitatorClient,
  GatewayEvmScheme,
} from "@circle-fin/x402-batching/server";

const server = new x402ResourceServer([new BatchFacilitatorClient()]);
server.register("eip155:*", new GatewayEvmScheme());
await server.initialize();

Utilities

supportsBatching

import { supportsBatching } from "@circle-fin/x402-batching";
Checks whether a PaymentRequirements object supports Gateway batched settlement. Returns true if the requirements include the GatewayWalletBatched scheme.

isBatchPayment

import { isBatchPayment } from "@circle-fin/x402-batching/server";
Server-side alias for supportsBatching. Use this in facilitator routing logic to determine whether a payment should be routed to Gateway.

getVerifyingContract

import { getVerifyingContract } from "@circle-fin/x402-batching";
Extracts the Gateway Wallet contract address from a PaymentRequirements object.

Constants

import {
  CIRCLE_BATCHING_NAME,
  CIRCLE_BATCHING_VERSION,
  CIRCLE_BATCHING_SCHEME,
} from "@circle-fin/x402-batching";
ConstantValueDescription
CIRCLE_BATCHING_NAME'GatewayWalletBatched'EIP-712 domain name used for batch payment signatures
CIRCLE_BATCHING_VERSION'1'EIP-712 domain version
CIRCLE_BATCHING_SCHEME'exact'x402 payment scheme identifier

Chain configuration

import {
  CHAIN_CONFIGS,
  GATEWAY_DOMAINS,
} from "@circle-fin/x402-batching/client";
  • CHAIN_CONFIGS: Full configuration (USDC address, Gateway Wallet address, RPC URL) for all supported blockchains.
  • GATEWAY_DOMAINS: Domain IDs for EIP-712 signing, keyed by chain name.

Error reference

Gateway API error codes

These are the errorReason (settle) and invalidReason (verify) values returned by the Gateway API:
Error codeCauseRecovery
unsupported_schemeScheme is not exactOnly the exact scheme is supported
unsupported_networkNetwork not supported for batchingUse a supported network
unsupported_assetToken address not recognized on this blockchainUse the correct USDC address for the network
invalid_payloadMalformed payment payloadCheck EIP-712 signature fields
address_mismatchAuthorization to address does not match payToEnsure the payment is addressed to the correct seller
amount_mismatchAuthorization value does not match required amountEnsure the signed amount matches the price
invalid_signatureEIP-3009 signature verification failedRe-sign the payment and check the signing key matches from
authorization_not_yet_validAuthorization validAfter is in the futureWait until the authorization becomes valid
authorization_expiredAuthorization validBefore has passedCreate a new payment with a future expiry
authorization_validity_too_shortValidity window is shorter than 3 daysSet validBefore to at least 3 days in the future
self_transferfrom and to are the same addressBuyer and seller must be different addresses
insufficient_balanceBuyer’s Gateway balance is too lowDeposit more USDC before paying
nonce_already_usedPayment nonce was already submittedCreate a new payment (each payment needs a unique nonce)
unsupported_domainGateway domain not configured for this blockchainCheck network configuration
wallet_not_foundGateway Wallet contract not found on this blockchainVerify contract addresses for the network
unexpected_errorInfrastructure error (HTTP 500)Retry the request and contact support if the error persists

GatewayClient errors

All errors are thrown as standard Error objects with descriptive messages.
Error messageCauseRecovery
Unsupported chain: {chain}Invalid SupportedChainName passed to the constructorUse a valid chain name from the chain configuration table
Request failed with status {N}Non-402, non-2xx response from the serverCheck server status and URL
Missing PAYMENT-REQUIRED header in 402 responseServer returned 402 without the required headerVerify the server uses x402 v2
No payment options in 402 responseEmpty accepts array in the 402 responseThe server may not have configured payment options
No Gateway batching option availableServer does not support Gateway batchingUse supports() to check first; the server may only accept onchain
Payment failed: {reason}Server rejected the paymentCheck the Gateway API error code in the message
Insufficient USDC balanceWallet USDC balance is less than the deposit amountGet more USDC from the faucet or transfer from another wallet
Approval transaction failed: {txHash}USDC approval transaction reverted onchainCheck gas, wallet balance, and network status
Deposit transaction failed: {txHash}Deposit transaction reverted onchainCheck gas and contract state
Insufficient available balanceGateway balance is too low for the withdrawalDeposit more or reduce the amount
Unsupported destination chain: {chain}Invalid chain name for crosschain withdrawalUse a valid SupportedChainName

Middleware errors

Error messageCauseRecovery
Invalid price: {price}Price string could not be parsedUse format '$0.01' or '0.01'
No payment networks availableNo supported networks found (503 response)Check Gateway API connectivity
Payment verification failedVerify returned isValid: falseCheck the Gateway API error code in the response body reason field
Payment settlement failedSettle returned success: falseCheck the Gateway API error code in the response body reason field

BatchFacilitatorClient errors

Error messageCauseRecovery
Circle Gateway verify failed ({status}): {details}Verify endpoint returned an unexpected responseCheck payload format and API status
Circle Gateway settle failed ({status}): {details}Settle endpoint returned an unexpected responseRetry the request and contact support if the error persists
Circle Gateway settle returned empty response ({status})Empty body from settle endpointRetry the request
Circle Gateway getSupported failed ({status}): {details}Cannot fetch supported networksCheck API connectivity and URL

BatchEvmScheme errors

Error messageCauseRecovery
BatchEvmScheme: unsupported network format "{network}"Network string is not in eip155:<chainId> formatUse CAIP-2 format (for example, eip155:5042002)
BatchEvmScheme can only handle Circle batching optionsPayment requirements missing extra.name="GatewayWalletBatched"Use this scheme only for Gateway payments
Circle batching option missing extra.verifyingContractPayment requirements missing the Gateway Wallet addressEnsure the server includes verifyingContract in extra