Using x402 payments with Privy
Enable your users to pay for APIs and content using x402, the new HTTP payment protocol. Privy’suseX402Fetch hook makes it seamless to integrate automatic stablecoin payments with embedded wallets.
What is x402?
x402 is an open payment protocol that enables instant, automatic payments for APIs and digital content over HTTP. When a resource requires payment, the server responds with402 Payment Required. The client constructs an X-PAYMENT header with a signed payment authorization and retries the request.
Installation
useX402Fetch hook is built into @privy-io/react-auth (v3.7.0+).
Usage
Basic example
Using default connected wallet
With maximum payment protection
How it works
- User requests content: Client calls
fetchWithPayment() - Server responds 402: Returns payment requirements (USDC amount, recipient address, time window)
- Build typed data: Hook constructs EIP-712 typed data for USDC’s
transferWithAuthorization - Sign with Privy: User signs the authorization using their embedded wallet (no gas required)
- Build X-PAYMENT: Hook creates a base64-encoded JSON payload with
authorization+signature - Retry with payment: Request repeats with
X-PAYMENTheader - Server verifies: Resource server calls facilitator to verify the payment
- Facilitator settles: Facilitator submits the authorization onchain and confirms the transaction
- Server delivers: Returns content with
200 OK
Key details
Requirements:- Users need USDC in their Privy embedded wallet on the correct network (e.g. Base, Base Sepolia, or Solana)
- Use Privy’s
useFundWallethook to help users add funds if needed - The facilitator pays gas fees (users only need USDC, not ETH or SOL)
- For testnet: Get free USDC from Circle’s faucet
x402 facilitators
Facilitators are services that verify payment authorizations and submit transactions onchain on behalf of users. They handle gas fees and transaction settlement, allowing users to pay only with USDC without needing native tokens like ETH or SOL. Several x402 facilitators are available, including:- Pay AI: facilitator, docs
- Corbits: facilitator, docs

