@nuwa-ai/x402 package layers AI-friendly ergonomics on top of the upstream coinbase/x402 reference implementation. It keeps your focus on business logic while handling payment requirements, facilitator calls, and settlement bookkeeping for both MCP tools and standard HTTP/gRPC gateways.
Modules at a glance
Every helper runs the same x402 facilitator flow, so you can swap Coinbase CDP for a self-hosted verifier as long as it exposes
/verify, /settle, /supported, and (optionally) /list.
Installation
@coinbase/x402, viem, and zod if you need facilitator utilities, key derivation, or schema validation:
base-sepolia; set NETWORK=base in production.
Example: paid MCP tool
Next.js App Router example—mirrors the tutorial inxnuwa/x402-mcp.mdx:
- Declare tools in the initializer callback; use
server.paidToolfor paid methods andserver.toolfor free ones. - Incoming calls lacking
_meta["x402/payment"]receive a402response that echoes theacceptsrequirements you configured. - After verification and settlement, a successful response includes
_meta["x402/payment-response"]so clients can reconcile spend.
Example: LLM proxy with receipts
Edge-friendly handler that forwards paid calls to OpenRouter:gateWithX402Payment validates the X-PAYMENT header, forwards the request once paid, and appends an X-PAYMENT-RESPONSE header containing the settlement transaction hash. Use the optional onSettle hook to report usage.
Facilitator configuration
Both modules accept aFacilitatorConfig with url and createAuthHeaders() that returns per-endpoint headers for /verify, /settle, /supported, and optionally /list. Coinbase’s @coinbase/x402 package already exposes a ready-to-use implementation (facilitator). If you roll your own verifier, implement the same shape.
Environment variables
On mainnet, fund the derived address with USDC and monitor balances. The helpers settle before returning, so insufficient facilitator credit or wallet funds will keep the endpoint in a 402 challenge loop.
Roadmap & support
- Tracking x402 v2 (
uptoscheme) for usage-based billing. - Additional facilitator adapters and dynamic pricing utilities.
nuwa-protocol/nuwa-x402 for changelog updates and examples.
See also (ERC8004): https://github.com/nuwa-protocol/nuwa-8004/tree/main
