Description: A payments protocol for the internet. Built on HTTP.
View coinbase/x402 on GitHub ↗
The Coinbase X402 repository introduces a novel, crypto-native standard for HTTP 402 Payment Required responses, designed to protect public endpoints from abuse, mitigate DDoS attacks, and enable flexible resource monetization. At its core, X402 leverages the existing HTTP 402 status code, enhancing it with two distinct challenge mechanisms: Proof-of-Work (PoW) and Payment, both verifiable and integrated into the standard HTTP request-response cycle.
The primary problem X402 addresses is the difficulty of securing public-facing services, especially in decentralized or open environments, without relying on centralized authentication or traditional rate-limiting methods that can be easily circumvented. By requiring a verifiable effort or payment, X402 ensures that access to resources is not free or trivial, thereby deterring malicious actors and encouraging fair usage. This approach is particularly relevant in the Web3 ecosystem, where services often need to be accessible without traditional login barriers but still require protection.
The X402 protocol operates through a simple, two-step interaction. When a client requests a resource from an X402-enabled server, and the server determines that access requires a challenge, it responds with an HTTP 402 status code. Crucially, this response includes a `WWW-Authenticate` header specifying the X402 challenge type and its parameters. The client then processes this challenge, either by performing a computational Proof-of-Work or by making a specified payment. Once the challenge is met, the client retries the original request, this time including an `Authorization` header containing the proof of work or payment verification. The server then validates this proof and, if successful, grants access to the requested resource.
Two distinct challenge types define X402's flexibility. The Proof-of-Work (PoW) challenge is akin to Hashcash, requiring the client to compute a cryptographic hash that meets a certain difficulty target. This mechanism is ideal for rate-limiting, preventing spam, and deterring denial-of-service attacks, as each request incurs a small, verifiable computational cost for the client. It offers a decentralized way to manage access without requiring any monetary transaction, making it suitable for general-purpose public services.
The Payment challenge, on the other hand, enables direct monetization of resources. In this scenario, the server provides an invoice, typically a Lightning Network BOLT11 invoice, within the `WWW-Authenticate` header. The client is expected to pay this invoice using a cryptocurrency. Upon successful payment, the server issues a macaroon – a cryptographically secure bearer token – which the client then includes in the subsequent `Authorization` header. Macaroons are central to the payment flow, offering powerful attenuation capabilities. They can be delegated with reduced permissions, have expiry times, and are resistant to replay attacks, ensuring that a single payment grants specific, controlled access rather than unlimited, perpetual access.
The benefits of X402 are multifaceted. It provides a robust, decentralized DDoS protection mechanism by imposing a cost (computational or monetary) on every request. It allows API providers to monetize their services in a granular and transparent manner, moving beyond traditional subscription models. Furthermore, it fosters fair resource allocation by ensuring that users who consume more resources contribute proportionally. The `x402-go` library provides a Go implementation, offering both client and server components, complete with examples demonstrating how to integrate X402 into existing applications. This makes it accessible for developers to adopt this innovative standard, paving the way for more resilient and economically sustainable public internet services.
Fetching additional details & charts...