Tools involved
| Tool | Role in x402 flow |
|---|---|
discover_services | Search the x402 service catalog (query, network, max price) |
get_service | Resolve one service by resource URL before calling |
paid_fetch | HTTP request; handles 402 → pay → retry |
get_token_price | Fixed-price MCP tool (internal x402, ~$0.001) |
transfer and wallet read tools are separate — used for moving funds, not for paying x402 merchants.
High-level flow
Step 1 — Discover payable services
Calldiscover_services to search the service catalog Agent Kit exposes for x402 merchants. Pass a semantic query and optional filters:
catalog summary and services[] entries (each with service_id as the resource URL, accepts, paymentsProtocolConfig, and metadata). Pick a service_id or endpoint URL for the next step. Optionally confirm details with get_service:
Step 2 — Probe with paid_fetch
First call without paymentPayload:
paymentRequired object) including:
x402Versionaccepts[](network, asset, payTo, amount)resourcemetadata
Step 3 — Sign paymentPayload on your backend
Payments must be signed with the agent’s server wallet access key (from createAgent). Never do this in the browser.
The sample backend API implements this in AgentSigningService.createX402PaymentPayloadForAgent:
- Load agent row with encrypted
accessKey. - Decrypt with
ENCRYPTION_KEY. AgentKitClient.getServerSigner().authenticate({ userIdentity, accessKey }).- Build viem public client for the chain in
accepts[0].network. - Sign EIP-712 typed data required by x402 v2.
- Return
PaymentPayloadfor MCP retry.
Step 4 — Retry paid_fetch with payment
Pass paymentPayload at the top level of tools/call params (same level as name / arguments):
paid_fetch returns fields such as:
| Field | Meaning |
|---|---|
status / ok | HTTP outcome |
data | Response body from the merchant |
payment_made | Whether x402 settlement ran |
payment_details | Settlement summary |
hint | Human-readable guidance on failure |
Fixed-price MCP tools (get_token_price)
Tools with a listed price in the dashboard (e.g. $0.001 for get_token_price) use Agent Kit’s internal x402 facilitator:
- First
tools/call→paymentRequiredwithacceptsbuilt by Agent Kit. - Backend signs
paymentPayload(same access key flow). - Retry with
paymentPayload. - Spend policy may block the call if the agent exceeded
budgetUsd(SDKupdateSpendPolicy).
paid_fetch to third-party URLs does not use the same spend-policy price table — filter expensive services in discover_services (maxUsdPrice).
MCP client example (NestJS)
From the sample backend’sMcpClientService:
error.code === -32402 by signing and retrying once.
Facilitator (verify / settle)
Hosted facilitators process payment verification and on-chain settlement.| Tier | URL | When used | Auth |
|---|---|---|---|
| Testnet | https://x402.org/facilitator | Gateway: /v1/x402-testnet/…. Agent Kit: X402_NETWORK is Base Sepolia, Solana Devnet, etc. | None |
| Mainnet | https://api.cdp.coinbase.com/platform/v2/x402 | Gateway: /v1/x402-mainnet/…. Agent Kit: X402_NETWORK is Base, Polygon, etc. | CDP_API_KEY_ID + CDP_API_KEY_SECRET |
X402_FACILITATOR_URL for everything. Set CDP_API_KEY_* on the gateway and Agent Kit hosts so mainnet settle/verify works.
Environment (Agent Kit host)
| Variable | Purpose |
|---|---|
X402_TESTNET_FACILITATOR_URL | Testnet facilitator (default https://x402.org/facilitator) |
CDP_API_KEY_ID / CDP_API_KEY_SECRET | Required for mainnet verify/settle (gateway x402-mainnet + mainnet X402_NETWORK) |
X402_NETWORK | Default CAIP-2 network |
X402_PAY_TO_ADDRESS | Optional fixed pay-to; else agent EVM address |
SPEND_POLICY_ENABLED | Enforce per-agent budgets on fixed-price tools |
PAID_FETCH_HOST_ALLOWLIST | Restrict paid_fetch hosts (recommended in production) |
Operational tips
- Preflight: Use
get_balanceon the agent’s EVM address before paid calls. - Discovery: Prefer URLs from
discover_services/get_serviceover guessing endpoints. - GET requests: Put query parameters in the
urlstring, not in custom headers unless the API requires it. - Logging: Never log
accessKeyor fullpaymentPayloadsecrets in production.
Related guides
- MCP tools reference
- SDK quickstart —
accessKeystorage - Full-stack app integration — chat loop + auto-sign transfers