> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chipipay.com/llms.txt
> Use this file to discover all available pages before exploring further.

# x402 Payment Protocol

> Pay-per-request APIs using USDC on Starknet

## What is x402?

[x402](https://www.x402.org/) is an open HTTP payment protocol backed by Coinbase, Cloudflare, Google, Visa, Stellar, and Lightning Labs. It enables **pay-per-request APIs** where clients pay with crypto before accessing a resource.

The protocol uses HTTP status code **402 Payment Required** — a status code reserved since 1997 for exactly this purpose.

## How it Works

```
Client                          Server                          Chipi Paymaster
  |                               |                                |
  |  GET /api/resource            |                                |
  |------------------------------>|                                |
  |  402 + PAYMENT-REQUIRED hdr   |                                |
  |<------------------------------|                                |
  |                               |                                |
  |  [sign SNIP-12 payment]       |                                |
  |                               |                                |
  |  GET /api/resource            |                                |
  |  + X-PAYMENT header           |                                |
  |------------------------------>|                                |
  |                               |  verify + settle (gasless)     |
  |                               |------------------------------->|
  |                               |  { txHash }                    |
  |                               |<-------------------------------|
  |  200 + resource               |                                |
  |<------------------------------|                                |
```

1. Client requests a protected resource
2. Server returns **402** with a `PAYMENT-REQUIRED` header containing the price
3. Client signs a USDC payment using SNIP-12 typed data
4. Client retries the request with the signed payment in the `X-PAYMENT` header
5. Server verifies the signature and settles via Chipi's gasless paymaster
6. Server returns the resource

## Why x402 + Chipi?

Chipi's existing infrastructure maps perfectly to x402:

| x402 Concept       | Chipi Infrastructure                        |
| ------------------ | ------------------------------------------- |
| Payment signing    | `account.signMessage()` (SNIP-12)           |
| Gasless settlement | `PaymasterAdapter` / sponsored transactions |
| Automated payments | Session keys (SNIP-9)                       |
| Client wallets     | CHIPI / READY wallets                       |

**Key advantages:**

* **Gasless**: Payments are settled through Chipi's paymaster — users never need STRK for gas
* **Session keys**: Combined with SNIP-9 sessions, payments happen automatically without user interaction per request
* **Native USDC**: All payments use native USDC on Starknet mainnet

## SDK Support

| Package                | What's Available                                 |
| ---------------------- | ------------------------------------------------ |
| `@chipi-stack/types`   | TypeScript type definitions                      |
| `@chipi-stack/core`    | `X402Client` — automatic 402 payment handling    |
| `@chipi-stack/backend` | `X402Facilitator` + `x402Middleware` for Express |
| `chipi-react`          | `useX402Payment` hook                            |
| `chipi_sdk` (Python)   | `X402Client` + `X402Facilitator` + middleware    |

## Quick Examples

### Client: Pay for APIs

```typescript theme={null}
import { X402Client } from "@chipi-stack/core";

const client = new X402Client(account, {
  maxPaymentAmount: "1.00", // Max $1 USDC per request
});

// Automatically handles 402 responses
const response = await client.fetch("https://api.example.com/premium-data");
const data = await response.json();
```

### Server: Monetize APIs

```typescript theme={null}
import { x402Middleware } from "@chipi-stack/backend";

const facilitator = sdk.createX402Facilitator();

app.get("/premium", x402Middleware({
  facilitator,
  paymentConfig: { amount: "0.01", payTo: "0xYOUR_ADDRESS" },
}), (req, res) => {
  res.json({ data: "premium content", txHash: req.x402.txHash });
});
```

## Next Steps

<CardGroup cols={3}>
  <Card title="Client Guide" icon="credit-card" href="/sdk/guides/x402-client">
    Pay for APIs from your app
  </Card>

  <Card title="Server Guide" icon="server" href="/sdk/guides/x402-server">
    Monetize your API endpoints
  </Card>

  <Card title="Session Keys" icon="key" href="/sdk/guides/x402-sessions">
    Automated payments with sessions
  </Card>
</CardGroup>
