Skip to main content

X402Adapter

X402Adapter is the default implementation of IPaymentAdapter for the x402 payment protocol. It issues challenges locally (no on-chain call) and verifies payment proofs by inspecting ERC-20 Transfer events on Base or Base Sepolia.
import { X402Adapter } from "@key0ai/key0";

Constructor

new X402Adapter({
  network: "testnet" | "mainnet",
  rpcUrl?: string,  // Override the default RPC URL
})
network
"testnet" | "mainnet"
required
The Base network to use for on-chain verification. "testnet" targets Base Sepolia (chain ID 84532); "mainnet" targets Base (chain ID 8453).
rpcUrl
string
Optional override for the JSON-RPC endpoint. When omitted, the adapter uses the default public RPC for the selected network.

Example

const adapter = new X402Adapter({ network: "mainnet" });
const adapter = new X402Adapter({
  network: "testnet",
  rpcUrl: "https://sepolia.base.org",
});

IPaymentAdapter Interface

X402Adapter implements the IPaymentAdapter interface, which any payment adapter must satisfy:
interface IPaymentAdapter {
  readonly protocol: string;
  issueChallenge(params: IssueChallengeParams): Promise<ChallengePayload>;
  verifyProof(params: VerifyProofParams): Promise<VerificationResult>;
}
The protocol property is always "x402" for this adapter.

Methods

issueChallenge

issueChallenge(params: IssueChallengeParams): Promise<ChallengePayload>
Generates a challenge payload for the client. This is a purely local operation — no on-chain transaction is required. The returned payload includes the chain ID, USDC contract address, and facilitator URL so the client knows where and how to pay. Returns a ChallengePayload with protocol: "x402" and a raw object containing:
FieldTypeDescription
typestringAlways "X402Challenge"
chainIdnumberTarget chain ID (8453 or 84532)
assetstringAlways "USDC"
usdcAddressstringThe USDC contract address for the target network
facilitatorUrlstringThe x402 facilitator endpoint for settlement

verifyProof

verifyProof(params: VerifyProofParams): Promise<VerificationResult>
Verifies a payment proof by performing on-chain checks against the transaction receipt. Returns a VerificationResult indicating success or failure with a specific error code. This method delegates to verifyTransfer, which runs the full set of on-chain verification checks described below.

Network Configuration

Both networks use USDC (6 decimals). The adapter selects the correct contract address and chain configuration automatically based on the network parameter.
NetworkChain IDUSDC AddressExplorer
testnet (Base Sepolia)845320x036CbD53842c5426634e7929541eC2318f3dCF7esepolia.basescan.org
mainnet (Base)84530x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913basescan.org

On-Chain Verification

The verifyProof method performs six sequential checks against the transaction receipt. If any check fails, it returns immediately with a verified: false result and the corresponding error code.
StepCheckError Code
1Transaction receipt exists and is retrievableTX_NOT_FOUND or RPC_ERROR
2Transaction was not revertedTX_REVERTED
3At least one USDC Transfer event sends funds to the expected destinationWRONG_RECIPIENT
4The total transferred amount meets or exceeds the expected amountAMOUNT_INSUFFICIENT
5The block timestamp is before the challenge expiryTX_AFTER_EXPIRY
6Chain ID matches the expected chainCHAIN_MISMATCH
These checks enforce the security properties required for safe on-chain payment verification.

IssueChallengeParams

FieldTypeDescription
requestIdstringUnique identifier for the access request
resourceIdstringThe resource being accessed
planIdstringThe pricing plan selected by the client
amountstringHuman-readable amount (e.g., "$0.10")
destination`0x${string}`Seller’s wallet address
expiresAtDateWhen the challenge expires
metadataRecord<string, unknown>Arbitrary metadata passed through to the challenge

ChallengePayload

FieldTypeDescription
challengeIdstringUnique identifier for the issued challenge
protocolstringProtocol name ("x402")
rawRecord<string, unknown>Protocol-specific data exposed to the client
expiresAtDateWhen the challenge expires

VerifyProofParams

FieldTypeDescription
challengeIdstringThe challenge being fulfilled
proof.txHash`0x${string}`On-chain transaction hash
proof.chainIdnumberChain the transaction was submitted on
proof.amountstringClaimed payment amount
proof.assetstringClaimed asset (e.g., "USDC")
expected.destination`0x${string}`Expected recipient address
expected.amountRawbigintExpected amount in USDC micro-units
expected.chainIdnumberExpected chain ID
expected.expiresAtDateChallenge expiry deadline

VerificationResult

FieldTypeDescription
verifiedbooleanWhether the payment proof passed all checks
txHash`0x${string}`Transaction hash (present on both success and most failures)
fromAddress`0x${string}`Payer’s wallet address, extracted from the Transfer event
confirmedAmountbigintTotal USDC transferred to the destination
confirmedChainIdnumberChain ID confirmed from network config
confirmedAtDateBlock timestamp of the transaction
blockNumberbigintBlock number containing the transaction
errorstringHuman-readable error message (present when verified is false)
errorCodeVerificationErrorCodeMachine-readable error code (present when verified is false)
For full type definitions, see Data Models.