API Provider Guide

Integrate x402 payments into your API and start earning from autonomous agents.

Why x402 for Your API?

💰

Better Monetization

Pay-per-use model means no subscription friction. Agents pay for exactly what they use.

Instant Settlement

Receive USDC instantly on Solana, no 30-day payment delays.

🤖

Agent-Native

Built for autonomous systems. No UI, no checkout flows, no friction.

🔐

Fraud Prevention

Cryptographic proofs and on-chain verification prevent payment fraud.

Integration (3 Steps)

Step 1: Return 402 for Unpaid Requests

// Next.js example
export async function GET(req: NextRequest) {
  const paymentHeader = req.headers.get('x-payment');
  
  if (!paymentHeader) {
    return Response.json({
      x402Version: 1,
      resource: 'https://api.yourdomain.com/data',
      accepts: [{
        method: 'solana-pay',
        network: 'devnet',
        asset: 'USDC',
        amount: 0.01, // Price in USDC
        payTo: 'YourWalletAddress...',
        extra: { decimals: 6 }
      }]
    }, { status: 402 });
  }
  
  // Payment included, verify and return data
  // ...
}

Step 2: Verify Payment (Optional but Recommended)

// Parse payment header
const payment = JSON.parse(
  Buffer.from(paymentHeader, 'base64').toString()
);

// Verify transaction on Solana
const signature = payment.payload.transactionSignature;
const tx = await connection.getTransaction(signature);

if (!tx || tx.meta?.err) {
  return Response.json(
    { error: 'Invalid payment' }, 
    { status: 402 }
  );
}

Step 3: Return Content

return Response.json(
  { 
    data: 'Your premium content here' 
  },
  {
    status: 200,
    headers: {
      'x402-amount': '0.01', // Actual amount charged
    }
  }
);

Advanced Features

Chained Requests

Suggest follow-up endpoints for related data:

return Response.json({
  data: { /* primary data */ },
  nextResource: '/api/detailed-analysis',
  nextResourceLabel: 'detailed-analysis'
}, {
  headers: { 'x402-amount': '0.01' }
});

TAP Proof Verification

Verify that payments come from trusted agents:

const tapProofHeader = req.headers.get('x-tap-proof');
if (tapProofHeader) {
  const proof = JSON.parse(
    Buffer.from(tapProofHeader, 'base64').toString()
  );
  
  // Verify signature, check authority, validate timestamp
  // ...
}

Pricing Guidelines

Use CaseSuggested PriceExample
Simple data fetch$0.001 - $0.01Weather data, stock quote
Computed analysis$0.01 - $0.10Sentiment analysis, risk score
Complex processing$0.10 - $1.00AI inference, report generation
Premium content$1.00+Research reports, datasets

💡 Pro Tip

Start with low prices to build adoption, then increase as agents find value. You can always offer volume discounts or subscription alternatives.

Examples

See working examples in this repo:

  • web/src/app/api/premium-data/route.ts - Basic x402 endpoint
  • web/src/app/api/premium-insights/route.ts - Chained endpoint

Testing

Test your x402 API with AuditAgent:

const client = new AuditAgentClient({ /* config */ });

const result = await client.runAudit({
  resourcePath: '/your-api-endpoint'
});

console.log('Success!', result.primary);

Need Help?

We're here to help you integrate x402 payments: