The x402 protocol lets clients pay for HTTP resources (APIs, endpoints, content) via blockchain transactions (with fiat support in progress). A server responds with HTTP 402 (“Payment Required”), the client pays, and the server grants access. No subscription, API key, or manual onboarding is required.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.
1. Components
| Component | Role | Example / note |
|---|---|---|
| Client | Requests a resource & pays | Browser, AI agent, mobile SDK |
| Resource Server | Protects endpoint, issues-402, verifies payment | API or web service |
| Facilitator | Optional verification + settlement layer | Offloads blockchain logic oai_citation:1‡QuickNode |
| Payment Scheme | Defines chain, token, network, and format | e.g., USDC on Polygon, ERC-3009 scheme |
2. Payment Flow Sequence
The typical sequence for one request:- Client → Resource Server: HTTP GET /endpoint
- Resource Server → Client: HTTP 402 Payment Required, body includes
PaymentRequirementsdescribing required payment. - Client selects a requirement, builds a
PaymentPayload, encodes it (e.g., Base64) in headerX-PAYMENTand retries request. - Resource Server verifies the payload (either locally or via the Facilitator).
- Facilitator / Resource Server settles payment onchain (if not already).
- Resource Server → Client: HTTP 200 OK, body includes the requested
resource, header
X-PAYMENT-RESPONSEcontains receipt details.
3. Implementation Snapshot
Server (Seller Side)
- Protect endpoint with middleware (e.g.,
paymentMiddleware("0xYourAddress", { "/path": { price:"$0.01", network:"polygon" } })). - On first unauthorized request: respond
402with JSON:
Client (Buyer Side)
- Send initial request → get 402 + payment info.
- Build payment per the scheme (client signs, selects token/chain).
-
Retry request adding header:
- Receive 200 OK and decode header X-PAYMENT-RESPONSE to confirm payment details.
Facilitator (optional)
Provides endpoints such as/verify and /settle for payment payloads.
Key Design Goals and Benefits
- HTTP-native: Uses standard HTTP codes and headers. No additional protocol layer.
- Chain and token agnostic: Works across any chain or stablecoin (e.g., USDC).
- Minimal integration: One-line middleware on the server side, or a few client calls 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.
Known Limitations and Future Directions
- Use the correct network label (e.g.,
"polygon-amoy"vs."polygon"). Mismatches cause payment failures. - The protocol is still evolving. Some paid endpoints may require settlement delays.
- For high-volume usage, consider deferred payment flows: aggregate many calls, then settle in batch.
- Protocol governance is managed by an open community to prevent vendor lock-in.