Skip to content

Payer API

Reference for x402-paysdk/pay payment, wallet, offer, receipt, recovery, DID, and transaction APIs.

TypeScript
import { pay } from 'x402-paysdk/pay';

This entry point is safe for payer-facing runtimes. It never receives a private key; it calls the host's WalletSigner only after validating payment requirements and signed offer context.

Core payment functions

FunctionSignaturePurpose
getPaymentRequired(refUrl: string, amount?: string) => Promise<PaymentRequired>Resolve a ref and validate the 402 Payment Required response without signing
pay(options: PayOptions) => Promise<PayResult>Negotiate, verify offer, sign EIP-3009, submit, and return settlement evidence
prepareEip3009TypedData(required: PaymentRequired, payer: string) => Eip712TypedDataBuild bounded TransferWithAuthorization typed data
toReceiveUrl(refUrl: string) => URLConvert x402pay:// transport form into the gateway receive URL
decodePaymentRequired(encoded: string) => PaymentRequiredDecode a payment-required header value

pay(options)

TypeScript
interface PayOptions { refUrl: string; wallet: WalletSigner; paymentId?: string; amount?: string; trustedOfferSigners?: string[]; requireSignedOffer?: boolean; // default: true fetch?: typeof fetch;}
OptionRequiredBehavior
refUrlYesx402pay:// reference or supported receive URL
walletYesHost adapter with address and signTypedData
paymentIdNoIdempotency id; defaults to invoice id or a random public id
amountFor open refsAtomic amount applied to the receive URL
trustedOfferSignersNoExplicit gateway signer allowlist; otherwise same-origin DID discovery is used
requireSignedOfferNoDefaults to true; disable only in controlled development
fetchNoCustom bounded/test transport implementation
TypeScript
interface WalletSigner { address: string; signTypedData(params: Eip712TypedData): Promise<string>;}

The returned signature must be a 65-byte EVM signature. The SDK builds the nonce and valid time window; wallet code should not mutate the typed data.

Return value

TypeScript
interface PayResult { txHash: string; payTo: string; amount: string; network: string; payer: string; receiptStatus: string; signedPaymentReceipt?: string; receipt?: PaymentReceipt; status: 'settled';}

The signed receipt and receipt JSON form a pair. Archive both without modifying fields.

Offer trust and gateway DID

FunctionPurpose
verifySignedOffer(required, options)Verify signed offer presence, context, signature, signer trust, payment id, and expiry
fetchGatewayDidDocument(gateway, options?)Load and validate /.well-known/did.json with a 16 KiB default limit
discoverGatewayDidSigners(gateway, options?)Return trusted assertion signers from the same-origin DID document
gatewayDidSigners(document, gateway, options?)Validate an already loaded DID document
didWebIdFromGateway(gateway)Derive the expected did:web identifier from an origin

VerifySignedOfferResult exposes present, verified, trusted, signer, kid, and the parsed offer. Verification fails closed when an expected origin, payment id, signer, or context differs.

Receipt APIs

FunctionNetwork callPurpose
verifySignedPaymentReceipt(options)NoVerify envelope signature, content hash, expected fields, and optional signer allowlist
verifyReceipt(options)YesCall /verify-receipt and verify chain status plus expected amount/payTo
paymentReceiptContentHash(receipt)NoProduce the canonical sha256: payment receipt reference
verifySignedRefundReceipt(options)NoVerify a refund receipt signature and expected refund context
verifyRefundReceipt(options)YesVerify refund receipt and reverse transfer on-chain
refundReceiptContentHash(receipt)NoProduce the canonical refund receipt hash
TypeScript
const result = await verifyReceipt({ gateway: 'https://pay.example.com', signedReceipt, receipt, expectedPayTo, expectedAmount, minConfirmations: 3});

The result separates valid from confirmed. A valid signed receipt can still be below the required confirmation policy.

Recovery and chain queries

FunctionSignatureNotes
recoverSettlement(options: RecoverSettlementOptions) => Promise<RecoverSettlementResult>Reconstruct a known settlement without calling settle again
getTxStatus(gateway: string, txHash: string) => Promise<TxStatus>Validate tx hash and query bounded chain status
getPaymentHistory(gateway: string, options: PaymentHistoryOptions) => Promise<PaymentHistoryResult>Query bounded USDC Transfer logs

getPaymentHistory requires payTo, from, or a cursor. Options include asset, fromBlock, limit, and cursor.

Exported types

The entry point exports payment and receipt models including PaymentRequired, PaymentOffer, PaymentPayload, PaymentReceipt, RefundReceipt, Eip712TypedData, PayOptions, PayResult, WalletSigner, TxStatus, PaymentHistoryOptions, and the verification/recovery option and result types.