Use CDP with an existing x402 server
- TypeScript
- Python
Already have an x402 server? Add To cover This works with
createCdpFacilitatorClient() for CDP settlement without
migrating to createX402Server. It uses your CDP API key and secret.exact and upto on Base and Solana, pass getCdpDefaultSchemes():x402HTTPResourceServer and x402MCPResourceServer. See
server.ts
for a runnable example.Choose your environment
createX402Server supports Base and Solana by default:
"development"uses testnets and test funds."production"uses mainnets and real funds.
- TypeScript
- Python
Routes inherit this setting unless they list specific networks. Switching To accept specific networks, list them on the route:
environment changes
every inherited route:Choose a payment scheme
A payment scheme defines when the amount is determined and how the payment settles:exactcharges a known price for a synchronous endpoint.uptoauthorizes a maximum, then settles the amount used by a synchronous endpoint.batch-settlementsupports repeated, high-throughput payments by redeeming per-request commitments later in batches.
exact unless your endpoint requires usage-based pricing or payment channels.
How each scheme moves money
- Exact
- Upto
- Batch-settlement
exact moves an exact amount of money from the buyer to the seller.Configure non-default schemes
- TypeScript
- Python
exact and upto are registered for Base and Solana. exact is the default for every route.
To use upto, set it on the route:networks expands the route to Base and Solana. Pass an explicit networks list to
limit which networks the route accepts.Your handler must report the final amount. See the
Express usage-based pricing example
for the complete flow.batch-settlement requires the full x402 RouteConfig and direct scheme registration. See the
x402 batch-settlement guide.(Optional) Choose an exact payment flow
EVM and Solanaexact support two payment flows:
- Upfront
paymentFlow on an exact route to use Upfront:
auth-capture and Solana upto; you cannot select it for a scheme
that does not support it. See section 6.1, “Asset Transfer Methods and Payment Flow Models,” of the
x402 specification
for details.
Configure who receives payment
Configure an onchain address to receive payments. This can be a CDP custodial wallet, a CDP non-custodial wallet, Coinbase Prime, Coinbase Business, a Coinbase retail deposit address, or a wallet you custody yourself. You only needCDP_WALLET_SECRET if the SDK should provision an
API Key Wallet to receive payments.
- TypeScript
- Python
By default, To provision a Smart Contract Wallet instead:
createX402Server provisions an API Key Wallet, which requires
CDP_WALLET_SECRET. Use payToConfig with type: "address" to receive at an
address you already control, with no wallet secret:Resolve the recipient dynamically
Use the full x402 route format when the receiving address depends on the request:Accept other tokens
Routes can accept tokens other than USDC.- TypeScript
- Python
Use the full x402
RouteConfig to set the asset for each payment option. Check the CDP
Facilitator’s supported networks and tokens first.Add onchain attribution
Builder Codes attribute the application that exposed a paid endpoint. The CDP Facilitator records this code in ERC-8021 Schema 2 calldata when it settles an EVM payment.- TypeScript
- Python
Pass your Builder Code to The server advertises the code on every EVM route. Solana-only routes are skipped because Builder
Code attribution uses EVM calldata.
createX402Server:Lifecycle hooks
- TypeScript
- Python
Use resource-server hooks for payment lifecycle events. Available hooks include
onProtectedRequest, onBeforeVerify, onAfterVerify, onVerifyFailure, onBeforeSettle,
onAfterSettle, onSettleFailure, and onVerifiedPaymentCanceled.Handle pending settlements
Resource servers using the CDP SDK or the canonical x402 Foundation packages automatically retry once when the CDP Facilitator broadcasts a transaction but cannot confirm it within the synchronous settlement window. If you call the Facilitator API directly, or if the automatic retry remains pending, reconcile the original transaction before requesting another payment. See Settlement Pending and Reconciliation for the automatic SDK behavior and the manual procedure.What to read next
- CDP Facilitator for supported chains, tokens, and schemes.
- Charge over MCP to protect an MCP tool instead of an HTTP route.