Payer API
Reference for x402-paysdk/pay payment, wallet, offer, receipt, recovery, DID, and transaction APIs.
TypeScriptimport { 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
| Function | Signature | Purpose |
|---|---|---|
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) => Eip712TypedData | Build bounded TransferWithAuthorization typed data |
toReceiveUrl | (refUrl: string) => URL | Convert x402pay:// transport form into the gateway receive URL |
decodePaymentRequired | (encoded: string) => PaymentRequired | Decode a payment-required header value |
pay(options)
TypeScriptinterface PayOptions { refUrl: string; wallet: WalletSigner; paymentId?: string; amount?: string; trustedOfferSigners?: string[]; requireSignedOffer?: boolean; // default: true fetch?: typeof fetch;}
| Option | Required | Behavior |
|---|---|---|
refUrl | Yes | x402pay:// reference or supported receive URL |
wallet | Yes | Host adapter with address and signTypedData |
paymentId | No | Idempotency id; defaults to invoice id or a random public id |
amount | For open refs | Atomic amount applied to the receive URL |
trustedOfferSigners | No | Explicit gateway signer allowlist; otherwise same-origin DID discovery is used |
requireSignedOffer | No | Defaults to true; disable only in controlled development |
fetch | No | Custom bounded/test transport implementation |
TypeScriptinterface 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
TypeScriptinterface 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
| Function | Purpose |
|---|---|
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
| Function | Network call | Purpose |
|---|---|---|
verifySignedPaymentReceipt(options) | No | Verify envelope signature, content hash, expected fields, and optional signer allowlist |
verifyReceipt(options) | Yes | Call /verify-receipt and verify chain status plus expected amount/payTo |
paymentReceiptContentHash(receipt) | No | Produce the canonical sha256: payment receipt reference |
verifySignedRefundReceipt(options) | No | Verify a refund receipt signature and expected refund context |
verifyRefundReceipt(options) | Yes | Verify refund receipt and reverse transfer on-chain |
refundReceiptContentHash(receipt) | No | Produce the canonical refund receipt hash |
TypeScriptconst 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
| Function | Signature | Notes |
|---|---|---|
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.