What HTTP 402 Means for APIs

HTTP 402 was reserved for future use for nearly three decades. It sat in the specification as a placeholder, rarely seen in the wild. Today, protocols like x402 and L402 are activating it for machine-to-machine payments. This shift moves the status code from a theoretical footnote to a functional standard for autonomous agents.

The x402 specification reinterprets this status code to handle micropayments directly within the HTTP flow. Instead of relying on external payment gateways or complex wallet integrations for every small transaction, an API can return a 402 with a payment proof. The agent verifies this proof and retries the request. This creates a seamless pay-per-call model that fits naturally into existing API architectures.

This approach is particularly useful for AI agents that need to access data or computational resources on a per-use basis. By embedding payment logic into the HTTP layer, developers can enforce scope, budget, and payment requirements without building separate financial infrastructure. The result is a more efficient and scalable way to monetize API endpoints for autonomous systems.

Compare x402 and L402 protocols

Choosing between x402 and L402 comes down to your infrastructure stack and preferred currency. x402 is a Coinbase-backed implementation that leverages USDC stablecoins for seamless pay-per-request transactions. L402, often associated with SatGate, focuses on using HTTP 402 as a paid rail for authority decisions, enforcing scope, budget, and payment layers over standard APIs.

The table below breaks down the technical differences to help you decide which protocol fits your agent's needs.

Featurex402 (Coinbase)L402 (SatGate)
CurrencyUSDC (Stablecoin)Bitcoin (Sats)
Primary FocusPay-per-request API callsAuthority & budget enforcement
Integration LayerHTTP 402 response codesHTTP 402 middleware
Best ForHigh-volume, predictable costsComplex scope & access control

x402 is ideal if your agents need predictable, stable costs. Because it uses USDC, you avoid the volatility of Bitcoin, making it easier to budget for high-frequency API calls. L402 shines when you need granular control over access. It treats payment as a gatekeeper for authority, allowing you to enforce strict scope and budget limits before the request even reaches your resource server.

Both protocols rely on the HTTP 402 status code, but their ecosystems differ. x402 integrates well with Coinbase's broader financial infrastructure, while L402 is built for developers who want to monetize API access using Bitcoin's native properties.

Implementing pay-per-call endpoints

Integrating the x402 standard into your API requires shifting from standard authentication to cryptographic payment verification. The goal is to ensure that every request carries a valid, signed payment proof before the server processes the payload. This flow relies on the interaction between the client, the resource server, and the blockchain network to settle USDC payments atomically.

The implementation follows a strict sequence: generate a unique nonce, sign the request with the client’s private key, attach the payment header, and finally verify the signature on the server side. Below is the step-by-step workflow for adding this logic to an existing endpoint.

1
Generate a unique nonce

Start by ensuring your server issues a fresh, unique nonce for each request. The nonce prevents replay attacks by ensuring that a signed payment proof can only be used once. When a client initiates a call, your endpoint should return a 402 status code alongside this nonce if no payment is present. The nonce must be stored temporarily in your cache (e.g., Redis) with a short TTL to track its validity.

2
Sign the request payload

The client must sign the request data using their private key. This signature proves ownership of the wallet funding the transaction. The payload typically includes the nonce, the target endpoint, and the amount of USDC being transferred. The signing process ensures that the payment intent cannot be tampered with during transmission. Ensure your client library supports EIP-712 or similar structured signing for clear intent representation.

Pay-Per-API 402 Analysis
3
Send the payment header

Once signed, the client attaches the payment proof to the HTTP header. This header contains the signature, the nonce, the wallet address, and the transaction hash (or pending transaction ID) on the blockchain. The server receives this header alongside the standard API request data. This step bridges the gap between traditional HTTP requests and blockchain-based settlement.

4
Verify signature and settle payment

On the server side, verify the signature against the claimed wallet address and ensure the nonce has not been used. Simultaneously, monitor the blockchain for the incoming USDC transfer. Once the transaction is confirmed (or sufficiently verified for your risk tolerance), mark the nonce as consumed and process the API request. If verification fails, return a 402 Payment Required error with a clear reason for the rejection.

Checklist for deployment:

  • Nonce Rotation: Ensure nonces are single-use and expire quickly to prevent replay attacks.
  • Signature Verification: Implement robust cryptographic checks for wallet signatures.
  • Error Handling: Define clear 402 error responses for invalid signatures or insufficient funds.
  • Blockchain Monitoring: Set up reliable listeners for USDC transaction confirmations.

Handle Agent Payment Flows

Once an agent receives an HTTP 402 response, the interaction shifts from simple inference to a transactional loop. The agent must parse the Paywall header, which typically contains a payment URI and the required amount, then execute a payment transaction before retrying the original request.

The Retry Loop

The agent’s workflow follows a strict sequence to ensure reliability without blocking the main thread:

  1. Parse the 402 Response: The agent extracts the payment URI, amount, and metadata from the Paywall header.
  2. Check Budget: Before initiating a transaction, the agent verifies its current wallet balance against the required amount.
  3. Execute Payment: The agent sends a transaction to the specified URI. If the payment succeeds, the response includes a proof-of-payment token.
  4. Retry Request: The agent resubmits the original request, attaching the payment proof in the Authorization or Payment-Proof header.

Managing Budget and Retries

Autonomous agents operate with finite budgets, making error handling critical. If a payment fails due to network congestion or insufficient funds, the agent should not retry indefinitely. Instead, it should log the failure and either switch to a fallback model or pause execution until the budget is replenished.

Implementing a maximum retry count (e.g., 3 attempts) prevents infinite loops and resource exhaustion. Each retry should include exponential backoff to avoid overwhelming the payment gateway.

Idempotency and Proof

To prevent double-charging, agents must use idempotency keys for each payment transaction. The API server validates these keys to ensure that even if the agent retries the payment request, the user is only charged once. The resulting proof-of-payment token is then cached locally to avoid redundant transactions for subsequent identical requests.

For more details on how x402 structures these headers, see the HTTP 402 specification overview.

Common Integration Mistakes

Even with the x402 specification, implementation errors can break the trust layer between your API and AI agents. These pitfalls aren't just about syntax; they undermine the security model that makes pay-per-use viable.

Reusing Nonces

Every request must carry a unique nonce. If you allow nonce reuse, you open the door to replay attacks where an agent captures a valid payment signature and replays it for unlimited calls. Always validate that a nonce has not been seen before in your session or request window.

Weak Signature Validation

Your server must verify the cryptographic signature before processing any payload. Skipping this step or using weak hashing algorithms allows agents to forge payment proofs. Ensure you are using the recommended elliptic curve cryptography standards and validating the signer's identity against your trusted key registry.

Ignoring Rate Limits

x402 doesn't automatically enforce rate limits. You must implement them separately to prevent denial-of-service attacks. If an agent pays for one call but sends a thousand, your infrastructure will collapse. Pair payment validation with strict throttling rules.

Pay-Per-API 402 Analysis

Frequently asked: what to check next

How do I implement HTTP 402 for AI agents?

Implementing pay-per-API 402 requires a middleware layer that intercepts requests and enforces payment before execution. Tools like SatGate allow you to deploy this logic in front of your APIs to manage scope, budget limits, and payment verification automatically. This ensures that only funded agents can trigger expensive inference or data retrieval tasks.

Which stablecoins are supported for API payments?

Most x402 infrastructure supports major stablecoins like USDC and USDT to minimize volatility during transaction settlement. Since AI agents operate at high velocity, using stable assets prevents price slippage from eating into the per-call margin. Check your specific gateway documentation for the full list of supported chains and token standards.

How does per-call billing compare to subscriptions?

Per-call billing aligns costs directly with usage, which is ideal for variable AI workloads. Unlike subscriptions, you only pay for the requests your agent actually processes. This model reduces waste for sporadic tasks but requires careful rate-limiting to prevent unexpected spikes in API spend.