Overview
When attempting to access x402-protected content without a payment payload, the gateway returns payment requirements. After making a payment, requesters receive a payment proof that grants access to the content.The Payment Flow
The content creator provides their dedicated Pinata gateway URL. Replace
your-gateway.mypinata.cloud in examples with the actual gateway domain provided.Step 1: Request the Content
Make a GET request to the x402 gateway URL:Step 2: Receive Payment Requirements (402 Response)
The gateway returns HTTP 402 with payment details:network: Blockchain network (e.g.,base)asset: Token contract address (USDC on Base)payTo: Recipient wallet address (content creator receives payment here)maxAmountRequired: Payment amount in smallest unit (e.g.,10000= 0.01 USDC)
Step 3: Access Content with Payment Proof
After successful payment, include theX-Payment header in the request:
- Validate the
X-Paymentheader - Verify the payment proof
- Check that amount, recipient, and network match
- Serve the private content
Using x402 Libraries
The x402 protocol has client libraries that automate the payment flow. First, set up your wallet:Setting Up Your Wallet
The x402 libraries require a Viem account or Coinbase Developer Platform wallet:Option 1: Viem Local Account
Option 2: Coinbase CDP Wallet
Using the Libraries
Once you have your wallet set up, use the x402 libraries to access paid content:@x402/fetch
@x402/axios
Technical Details (Optional)
Pinata uses Coinbase Facilitator to process x402 payments, which provides access to Coinbase’s Discovery Bazaar network for broader content discovery.
Understanding Payment Amounts
USDC uses 6 decimals, so amounts use the token’s smallest unit:
Formula: USD Amount × 1,000,000 = token amount
Error Handling
402 Payment Required
Payment has not been made yet. Follow the payment flow above.403 Forbidden
Payment proof is invalid or expired. Make a new payment.404 Not Found
The CID doesn’t exist or isn’t attached to a payment instruction.500 Internal Server Error
Gateway or facilitator error. Contact the content creator.Network Support
USDC is currently the only supported token.Best Practices
- Handle 402 responses gracefully: Display payment requirements clearly
- Use the x402 libraries: They handle the complex payment flow automatically
- Test on Base Sepolia first: Verify integration before using mainnet
- Store payment proofs: If accessing content multiple times (check expiry)
- Monitor USDC balance: Ensure sufficient funds for payments