What is x402?
x402 is an HTTP payment protocol that enables gasless, signature-based payments to authenticate API requests. Instead of on-chain transactions, users sign an EIP-712 message that authorizes a payment facilitator to transfer tokens on their behalf. Key benefits:- ✅ No gas fees for users
- ✅ Instant payment verification
- ✅ Works over standard HTTP
- ✅ Perfect for API monetization
Why Combine Them?
Many facilitators only accept specific tokens on specific chains. In addition, x402 requires EIP-3009 on the token contract further limiting token support. However, Trails enables x402 using any token from any chain. Trails + x402 creates a powerful payment flow:- User pays with Trails using any token from any chain
- Payment arrives on your chosen chain and token that the facilitator supports (e.g., Base USDC)
- Automatic trigger of x402 signature for API access
- Gasless authentication to your protected endpoints
- API responds with paid content/service
Architecture
Implementation
Frontend Setup
1. Install Dependencies
2. Configure Providers
3. Create x402 API Client
The
withPaymentInterceptor
automatically handles EIP-712 signature creation and attaches payment headers to HTTP requests.4. Build Payment Widget Component
The
onCheckoutComplete
callback is the bridge between Trails and x402. It fires when the cross-chain payment succeeds, then triggers the x402 signature flow.Backend Setup
1. Install Dependencies
2. Configure Environment
3. Create x402 Protected API
The
paymentMiddleware
intercepts requests to protected routes and verifies the x402 payment signature before allowing access.Payment Flow Breakdown
Step 1: User Initiates Payment (Trails)
- Selects their source chain and token (e.g., ETH on Arbitrum)
- Trails calculates optimal route (swap → bridge → execute)
- User approves transaction
- Payment arrives as USDC on Base
Trails handles all complexity: token swaps, bridge routing, gas estimation, and delivery confirmation.
Step 2: Trails Callback Triggers x402
onCheckoutComplete
fires:
- API client is configured with the user’s wallet
- HTTP request is made to protected endpoint
- x402 interceptor prompts for EIP-712 signature
- Signature is attached to request headers
Step 3: x402 Signature Creation
- Detects
402 Payment Required
response - Parses payment details from response headers
- Creates EIP-712 typed data structure
- Prompts user to sign (gasless, off-chain)
- Retries request with payment signature
Users sign a message, not a transaction. This is gasless and instant.
Step 4: Backend Verification
- Validates payment signature
- Sends transfer request to facilitator
- Facilitator transfers tokens from user → your wallet
- If successful, request proceeds to handler
- If failed, returns
402 Payment Required
Step 5: Protected Response
Conclusion
Combining Trails and x402 creates a seamless payment experience:- Trails handles the complexity of cross-chain payments
- x402 provides gasless, instant API authentication
- Together they enable truly user-friendly paid services - paid with any token from any chain.
- 🔐 API monetization
- 📰 Content paywalls
- 🎮 Gaming microtransactions
- 📊 Data services
- 🤖 AI API access
- 💼 SaaS applications