Overview
This guide shows how to use Magic’s Embedded Wallet to pay for x402-protected API endpoints. x402 is an open payment protocol by Coinbase that uses the HTTP402 Payment Required status code to enable instant, gasless stablecoin payments over HTTP. When a server responds with 402, your app automatically signs a USDC payment and retries — no gas fees, no manual transfers.
Prerequisites
Before starting, ensure you have:- A Magic Publishable API Key from your Magic Dashboard
- A Base RPC endpoint (e.g., from Alchemy or QuickNode)
- USDC on Base Sepolia in the user’s wallet (for testing)
How It Works
- User authenticates with Magic
- Your app makes a request to an x402-protected endpoint
- The server responds with HTTP 402 and payment requirements
- The x402 client signs a gasless USDC transfer (EIP-3009) using the user’s wallet
- The request is retried with the payment signature
- A facilitator verifies and settles the payment on-chain
- The server returns the requested resource
x402 payments are gasless for the payer. The protocol uses EIP-3009
transferWithAuthorization, which means the user signs a typed data message — no ETH needed for gas. The facilitator submits the on-chain transaction.Setting Up the Clients
Install dependencies and initialize Magic with viem.TypeScript
Creating a Custom Account
The x402 SDK expects a viemAccount object that can sign typed data. Create a custom account adapter that delegates signing to Magic’s wallet.
TypeScript
Setting Up the x402 Client
Register the Magic account with the x402 client and create a payment-enabled fetch wrapper.TypeScript
Making Paid Requests
UsefetchWithPayment just like the regular fetch API. If the server responds with 402, the x402 client automatically handles the payment flow.
TypeScript
- Receives the 402 response with payment requirements
- Signs a gasless USDC transfer using the Magic wallet
- Retries the request with the payment signature in the header
- Returns the successful response
Setting Up a Test Server
To test the payment flow, you can set up a simple Express server that requires x402 payment.TypeScript
The testnet facilitator at
https://x402.org/facilitator requires no API keys or signup. For production, switch to the Coinbase CDP facilitator with network eip155:8453 (Base mainnet).Switching to Production
To move from testnet to mainnet, update the network and facilitator:TypeScript
Key Dependencies
| Package | Purpose |
|---|---|
magic-sdk | Magic authentication and Embedded Wallet |
viem | Ethereum client and account utilities |
@x402/fetch | Wraps fetch with automatic x402 payment handling |
@x402/evm | EVM payment scheme for x402 |
@x402/core | Core x402 client and server utilities |
@x402/express | Express middleware for x402-protected endpoints |
Troubleshooting
Payment signature rejected
Payment signature rejected
Symptoms: The facilitator rejects the payment signature.Solutions:
- Ensure the user has sufficient USDC on the correct network (Base Sepolia for testing, Base mainnet for production)
- Verify the Magic wallet is connected to the right chain
- Check that the
signTypedDatacall is not being blocked by content security policy
402 response not handled
402 response not handled
Symptoms: The fetch call returns a raw 402 response instead of automatically paying.Solutions:
- Make sure you’re using
fetchWithPayment(the wrapped version), not the nativefetch - Verify the x402 client has a scheme registered for the server’s network
- Check that
ExactEvmSchemeis imported from@x402/evm/exact/client(not/server)
Wrong network error
Wrong network error
Symptoms: Payment fails with a network mismatch error.Solutions:
- The Magic instance must be configured for the same chain as the x402 server
- Use
eip155:84532for Base Sepolia oreip155:8453for Base mainnet - Ensure the viem chain matches (
baseSepoliaorbase)
Resources
Magic Embedded Wallets
Learn about Magic’s Embedded Wallet product
x402 Documentation
Official x402 protocol documentation
x402 GitHub
Reference implementations and examples
x402 Foundation
Protocol specification and facilitator info