---
title: "x402: HTTP-Native Stablecoin Micropayments"
slug: "x402"
description: "A deep dive into the x402 protocol, an HTTP-native micropayment standard designed for machine-to-machine stablecoin transactions."
category: "protocols"
schemaType: "TechArticle"
readingTimeMin: 6
relatedTaxonomy: "paymentProtocol"
relatedValue: "x402"
provenance: "ai_generated"
authorName: "AI Agent Gateway"
datePublished: "2026-05-01T00:00:00.000Z"
dateModified: "2026-05-20T10:00:00.000Z"
canonicalUrl: "https://theagentgateway.com/articles/x402"
---
# x402: HTTP-Native Stablecoin Micropayments

## Overview of x402 Protocol

The x402 protocol defines a structured framework for facilitating machine-to-machine micropayments directly over the HTTP protocol using stablecoins. As artificial intelligence agents become autonomous actors within the digital economy, they require standard mechanisms to negotiate, authorize, and execute transactions without human intervention. The x402 standard addresses this by building on top of the traditional HTTP status code 402 (Payment Required) and introducing specialized headers for payment negotiation.

Traditional payment rails are ill-suited for the high-frequency, low-value transactions that characterize agentic service consumption. Credit card networks impose transaction fees that dwarf the value of individual API requests, while traditional banking infrastructure introduces latency that is incompatible with real-time computation. The x402 protocol bypasses these limitations by leveraging decentralized ledger technologies and stablecoin assets, ensuring that transaction fees remain sub-cent and settlement is achieved within seconds.

## Header Specification and Handshake Flow

The interaction begins when an agent requests a restricted resource. The server responds with an HTTP 402 Payment Required status, accompanied by a set of structured headers detailing the payment requirements. These headers include `X-402-Payment-Token` to specify the acceptable stablecoin contracts, `X-402-Amount` to declare the cost per request or per token, and `X-402-Destination` to indicate the recipient's wallet address.

Once the agent receives the 402 response, it parses the headers and evaluates the cost against its pre-allocated budget. If the transaction falls within authorized bounds, the agent generates a cryptographic transaction signing request. The payment is executed on-chain or via a state channel, and the resulting transaction hash is included in a subsequent request to the resource server via the `X-402-Payment-Proof` header. The server verifies the proof against the ledger and delivers the requested content.

Here is a summary of the standard negotiation headers:

| Header Name | Type | Description |
|---|---|---|
| `X-402-Payment-Token` | Address | The smart contract address of the approved stablecoin (e.g., USDC). |
| `X-402-Amount` | Numeric | The exact amount required, denominated in micro-units of the stablecoin. |
| `X-402-Destination` | Address | The cryptographic wallet address where the payment must be sent. |
| `X-402-Payment-Proof` | Hash | The cryptographic transaction hash proving settlement on the ledger. |

## Implementation of HTTP 402 Negotiation

Developers can implement the x402 client-side handshake using standard HTTP client libraries. The following example demonstrates a TypeScript function that intercepts 402 responses, requests transaction signing from the agent's wallet, and retries the request with the transaction proof:

```typescript
async function fetchWithPayment(url: string, options: RequestInit = {}): Promise<Response> {
  const response = await fetch(url, options);
  if (response.status !== 402) {
    return response;
  }

  const token = response.headers.get('X-402-Payment-Token');
  const amount = response.headers.get('X-402-Amount');
  const destination = response.headers.get('X-402-Destination');

  if (!token || !amount || !destination) {
    throw new Error('Invalid x402 headers received from server');
  }

  const txHash = await executeOnChainPayment(token, amount, destination);
  
  const retryHeaders = new Headers(options.headers);
  retryHeaders.set('X-402-Payment-Proof', txHash);

  return fetch(url, {
    ...options,
    headers: retryHeaders
  });
}
```

Through this programmatic loop, agents can dynamically pay for resources as they consume them, paving the way for a self-sustaining internet of value.