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

# x402 How It Works

> How the x402 v2 protocol turns an HTTP 402 response into a verified onchain payment, with one round-trip and no signup.

x402 turns the HTTP 402 status code into a real payment channel. A client requests a paid resource, the server responds with a price and a payment requirement, the client signs and retries with a payment header, the server verifies (locally or through a facilitator) and settles onchain, then returns the resource with a receipt. The whole exchange is two HTTP round-trips and no signup, API key, or contract.

Polygon's mainnet and Amoy facilitators run **x402 v2**. If you have a V1 integration, see [Migration: V1 to V2](./migration-v1-to-v2).

***

## How it works

<div style={{border:"1px solid #C8CFE1",borderRadius:"12px",overflow:"hidden",marginBottom:"24px"}}>
  <div style={{background:"linear-gradient(180deg,#EAE4F5 0%,#F6F3FB 100%)",borderBottom:"1px solid #D5C4F2",padding:"10px 16px",fontSize:"11px",fontWeight:"700",color:"#670DE5",letterSpacing:"0.06em",textTransform:"uppercase"}}>x402 payment flow</div>

  <div style={{borderBottom:"1px solid #EEF0F9",padding:"9px 16px",display:"flex",alignItems:"center",gap:"10px"}}>
    <span style={{color:"#929EBA",fontSize:"11px",fontWeight:"700",minWidth:"16px",textAlign:"right"}}>1</span>
    <span style={{background:"#EEF0F9",color:"#48526F",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"600",whiteSpace:"nowrap"}}>Client</span>
    <span style={{color:"#670DE5",fontWeight:"700"}}>→</span>
    <span style={{background:"#EEF0F9",color:"#48526F",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"600",whiteSpace:"nowrap"}}>Resource Server</span>
    <span style={{fontFamily:"'Geist Mono',ui-monospace,monospace",fontSize:"12px",color:"#141635"}}>GET /endpoint</span>
  </div>

  <div style={{borderBottom:"1px solid #EEF0F9",padding:"9px 16px",display:"flex",alignItems:"center",gap:"10px"}}>
    <span style={{color:"#929EBA",fontSize:"11px",fontWeight:"700",minWidth:"16px",textAlign:"right"}}>2</span>
    <span style={{background:"#EEF0F9",color:"#48526F",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"600",whiteSpace:"nowrap"}}>Resource Server</span>
    <span style={{color:"#670DE5",fontWeight:"700"}}>→</span>
    <span style={{background:"#EEF0F9",color:"#48526F",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"600",whiteSpace:"nowrap"}}>Client</span>
    <span style={{fontSize:"13px",color:"#141635"}}>402 Payment Required + PaymentRequirements (accepts array)</span>
  </div>

  <div style={{borderBottom:"1px solid #EEF0F9",padding:"9px 16px",display:"flex",alignItems:"center",gap:"10px"}}>
    <span style={{color:"#929EBA",fontSize:"11px",fontWeight:"700",minWidth:"16px",textAlign:"right"}}>3</span>
    <span style={{background:"#EEF0F9",color:"#48526F",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"600",whiteSpace:"nowrap"}}>Client</span>
    <span style={{color:"#670DE5",fontWeight:"700"}}>→</span>
    <span style={{background:"#EEF0F9",color:"#48526F",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"600",whiteSpace:"nowrap"}}>Resource Server</span>
    <span style={{fontSize:"13px",color:"#141635"}}>Retries with `PAYMENT-SIGNATURE` header</span>
  </div>

  <div style={{borderBottom:"1px solid #EEF0F9",padding:"9px 16px",display:"flex",alignItems:"center",gap:"10px"}}>
    <span style={{color:"#929EBA",fontSize:"11px",fontWeight:"700",minWidth:"16px",textAlign:"right"}}>4</span>
    <span style={{background:"#EEF0F9",color:"#48526F",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"600",whiteSpace:"nowrap"}}>Resource Server</span>
    <span style={{color:"#670DE5",fontWeight:"700"}}>→</span>
    <span style={{background:"#EAE4F5",color:"#670DE5",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"700",whiteSpace:"nowrap"}}>Facilitator</span>
    <span style={{fontFamily:"'Geist Mono',ui-monospace,monospace",fontSize:"12px",color:"#141635"}}>POST /verify (signature + requirements)</span>
  </div>

  <div style={{borderBottom:"1px solid #EEF0F9",padding:"9px 16px",display:"flex",alignItems:"center",gap:"10px"}}>
    <span style={{color:"#929EBA",fontSize:"11px",fontWeight:"700",minWidth:"16px",textAlign:"right"}}>5</span>
    <span style={{background:"#EAE4F5",color:"#670DE5",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"700",whiteSpace:"nowrap"}}>Facilitator</span>
    <span style={{color:"#670DE5",fontWeight:"700"}}>→</span>
    <span style={{background:"#EAE4F5",color:"#670DE5",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"700",whiteSpace:"nowrap"}}>Polygon</span>
    <span style={{fontSize:"13px",color:"#141635"}}>Settles USDC onchain</span>
  </div>

  <div style={{padding:"9px 16px",display:"flex",alignItems:"center",gap:"10px"}}>
    <span style={{color:"#929EBA",fontSize:"11px",fontWeight:"700",minWidth:"16px",textAlign:"right"}}>6</span>
    <span style={{background:"#EEF0F9",color:"#48526F",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"600",whiteSpace:"nowrap"}}>Resource Server</span>
    <span style={{color:"#670DE5",fontWeight:"700"}}>→</span>
    <span style={{background:"#EEF0F9",color:"#48526F",padding:"2px 8px",borderRadius:"4px",fontSize:"11px",fontWeight:"600",whiteSpace:"nowrap"}}>Client</span>
    <span style={{fontSize:"13px",color:"#141635"}}>200 OK + body + `PAYMENT-RESPONSE` receipt header</span>
  </div>
</div>

<Tip>
  Steps 2 and 3 collapse into a single call if the client already knows the route's price ahead of time (for example, from a manifest). The signed payment header travels with the first request.
</Tip>

***

## Protocol resources

| Resource                   | Role                                                                                                                               |
| -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| Client                     | Requests a resource and signs the payment (browser, agent, SDK)                                                                    |
| Resource Server            | Protects endpoints, issues 402, verifies payment payloads                                                                          |
| Facilitator                | Optional verification + onchain settlement layer (Polygon: `x402.polygon.technology` mainnet, `x402-amoy.polygon.technology` Amoy) |
| Payment Scheme             | Defines chain, token, network, format (`exact` or `upto`)                                                                          |
| `PaymentRequirements`      | `accepts` array in the 402 body: scheme, network, amount, description                                                              |
| `PAYMENT-SIGNATURE` header | Signed payment payload on the retry request                                                                                        |
| `PAYMENT-RESPONSE` header  | Base64-encoded receipt with the settlement transaction hash                                                                        |

***

## Server side (sellers)

Protect routes with v2 middleware that pairs an `x402ResourceServer` with a facilitator client. The first unpaid request returns a 402 with an `accepts` array:

```json theme={null}
{
  "x402Version": 2,
  "accepts": [
    {
      "scheme": "exact",
      "network": "eip155:137",
      "maxAmountRequired": "1000",
      "description": "Access to premium API"
    }
  ],
  "error": null
}
```

`maxAmountRequired` is in the smallest unit of the currency. USDC has six decimals, so `1000` = \$0.001. On retry, verify the `PAYMENT-SIGNATURE` against the facilitator and respond `200 OK` with a `PAYMENT-RESPONSE` header carrying the receipt.

For setup, see the [x402 Quickstart for Sellers](./quickstart-sellers).

***

## Client side (buyers)

The buyer sends the initial request, reads the 402, picks one `accepts` entry, signs against its wallet, and retries with the payment header.

* Create an `x402Client`, register `ExactEvmScheme` for `"eip155:*"`.
* Use `wrapFetchWithPayment` or `wrapAxiosWithPayment` to handle 402s transparently.
* The signed payload becomes a base64 string on the retry:

```http theme={null}
PAYMENT-SIGNATURE: <base64-encoded payload>
```

* Read the resulting receipt via `x402HTTPClient.getPaymentSettleResponse()` after a successful response.

For setup, see the [x402 Quickstart for Buyers](./quickstart-buyers).

***

## Facilitator (optional)

The facilitator exposes verification and settlement endpoints:

```http theme={null}
POST /verify
{
  "x402Version": 2,
  "paymentHeader": "<payload>",
  "paymentRequirements": { ... }
}
```

Some facilitators settle each payment instantly; others batch settlements for high-volume sellers. Polygon's facilitators settle instantly on mainnet and Amoy.

| Network         | Facilitator URL                        |
| --------------- | -------------------------------------- |
| Polygon mainnet | `https://x402.polygon.technology`      |
| Polygon Amoy    | `https://x402-amoy.polygon.technology` |

***

## Design goals

* **HTTP-native.** Uses standard status codes and headers. No additional protocol layer.
* **Chain and token agnostic.** Works across any chain or stablecoin.
* **Minimal integration.** Middleware on the server side; a few wrappers on the buyer side.
* **Micropayments.** Low friction and low cost, suitable for per-request pricing.
* **Autonomous agents.** AI systems can transact without per-transaction human approval.

***

## Implementation

<CardGroup cols={2}>
  <Card title="x402 Quickstart for Sellers" icon="code" href="./quickstart-sellers">
    Add x402 v2 middleware to Express, Next.js, or Hono.
  </Card>

  <Card title="x402 Quickstart for Buyers" icon="bolt" href="./quickstart-buyers">
    Pay x402-protected endpoints programmatically.
  </Card>

  <Card title="Migration: V1 to V2" icon="arrow-up-right-from-square" href="./migration-v1-to-v2">
    Upgrade an existing V1 integration to v2.
  </Card>

  <Card title="Mainnet Facilitator Addresses" icon="server" href="../analytics">
    Signer addresses for the Polygon mainnet and Amoy facilitators.
  </Card>
</CardGroup>
