> ## Documentation Index
> Fetch the complete documentation index at: https://raveculture-mintlify-api-provision-docs-1774045983.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# X402

description: "Accept USDC payments via x402 protocol"

# x402 Payments

Accept USDC cryptocurrency payments using the x402 protocol on Base network.

## Overview

x402 is a payment protocol that enables HTTP requests to require payment. Agents can pay for API access programmatically.

## Setup

### Environment Variables

```bash theme={null}
# x402 Configuration
X402_PAY_TO=0xd8fd0e1dce89beaab924ac68098ddb17613db56f
X402_FACILITATOR_URL=https://x402.org/facilitator
```

### Supported Networks

| Network      | Chain ID | Status      |
| ------------ | -------- | ----------- |
| Base Mainnet | 8453     | ✅           |
| Base Sepolia | 84532    | ✅ (Testnet) |

## Creating Paid Endpoints

### Step 1: Import x402

```typescript theme={null}
import { getX402Server, x402Config } from "@/lib/x402";
```

### Step 2: Define Payment Requirements

```typescript theme={null}
const paymentRequirements = {
  accepts: {
    scheme: "exact",
    price: "$0.001",
    network: "eip155:8453",
    payTo: x402Config.payTo,
  },
  description: "Premium API endpoint",
  mimeType: "application/json",
};
```

### Step 3: Check for Payment

```typescript theme={null}
export async function GET(req: NextRequest) {
  const server = getX402Server();
  
  const authHeader = req.headers.get("x-payments");
  if (!authHeader) {
    return new NextResponse(
      JSON.stringify({ 
        error: "Payment required",
        payment: paymentRequirements 
      }), 
      { status: 402 }
    );
  }

  // Verify and process payment
  // Return data
  return Response.json({ data: "Hello, paid user!" });
}
```

## Agent Payment Flow

Agents can make paid API calls:

```typescript theme={null}
// Agent makes a paid request
const response = await fetch('https://api.example.com/paid-endpoint', {
  headers: {
    'x-payments': paymentHeader // Automatically handled by x402 SDK
  }
});

if (response.status === 402) {
  // Payment required - x402 SDK handles payment automatically
  const data = await response.json();
  // Retry with payment
}
```

## Pricing Examples

| Action           | Price   |
| ---------------- | ------- |
| Basic API call   | \$0.001 |
| AI generation    | \$0.01  |
| File upload      | \$0.05  |
| Video generation | \$1.00  |

## Wallet Setup

Users need a wallet with USDC on Base:

1. Install MetaMask or Coinbase Wallet
2. Bridge funds to Base network
3. Get USDC on Base

## Troubleshooting

<AccordionGroup>
  <Accordion icon="error" title="Payment failed">
    * Ensure wallet has sufficient USDC on Base
    * Check network is correct (Base, not Ethereum)
    * Verify payTo address is correct
  </Accordion>

  <Accordion icon="error" title="402 response">
    * Include x-payments header in request
    * Use x402 SDK for automatic payment handling
  </Accordion>
</AccordionGroup>
