What HTTP 402 means for APIs

HTTP 402 Payment Required is transitioning from a reserved RFC 7231 placeholder to the standard signal for machine-to-machine microtransactions. With the rise of x402 and crypto-native protocols, this status code allows AI agents to negotiate payment on the fly, replacing pre-approved API keys for small, automated interactions.

When an API returns 402, it is not rejecting the request due to bad credentials (401) or insufficient permissions (403). It explicitly states that the service is available, but the transaction must be completed first. This allows developers to build payment walls native to the HTTP protocol, letting agents handle transaction logic without custom authentication middleware.

Set up the x402 payment layer

Integrating x402 transforms your API into a monetizable asset by leveraging the 402 status code for a standardized handshake between your endpoint and AI agents. Instead of building custom payment gateways, you register your endpoints with a provider and let the network handle the transaction logic.

This approach keeps your codebase clean. You define the price, and the x402 middleware ensures payment is settled before the response is returned. Below is the practical workflow to get your first pay-per-call endpoint live.

The Pay-Per-API 402
1
Register your endpoint

Start by connecting your API to an x402 provider like XPay. These platforms act as middleware that intercepts requests. Input your API’s base URL and define which specific routes should be protected. This tells the network to check for payment before processing the request.

The Pay-Per-API 402
2
Define your pricing model

Set the cost per request. You can choose a flat fee or a tiered structure based on complexity. For example, a simple data lookup might cost $0.01, while complex LLM inference could be $0.50. This pricing metadata is embedded into the endpoint configuration, ensuring agents know the cost before sending the payload.

3
Handle the 402 response

When an agent calls your endpoint without a valid payment signature, the x402 layer returns an HTTP 402 status code. This response includes a payment request payload with the amount due and the wallet address for settlement. The agent’s runtime environment recognizes this code, pauses execution, and triggers a micro-transaction to complete the payment.

4
Verify and return data

After the agent sends the transaction, the x402 provider verifies the blockchain confirmation. Once verified, the middleware forwards the original request to your actual API server. Your backend processes the request as usual and returns the data. From your perspective, the integration is transparent; you only see successful requests that have been paid for.

Choose the right stablecoin for settlement

When building agentic payment flows, the choice of cryptocurrency directly impacts the reliability of your API. For pay-per-API models, you need an asset that acts as a neutral settlement layer rather than a speculative position. The goal is to minimize volatility risk for both the provider and the consumer, ensuring that the price of the API call doesn't fluctuate between the moment the agent decides to call and the moment the transaction clears.

USDC (USD Coin) is the standard for this use case. It is fully backed, regulated, and deeply integrated into the Web3 infrastructure that powers agentic automation. Using a volatile asset like ETH or SOL introduces unnecessary friction: the agent must constantly monitor exchange rates, and the provider faces revenue uncertainty. Stablecoins remove this variable, allowing the API to function like a traditional credit card transaction but with the immediacy of blockchain settlement.

To help you evaluate your options, here is a comparison of the most common settlement layers for x402-style payments:

AssetVolatility RiskSettlement SpeedDeveloper Adoption
USDCLowNear-instantHigh
ETHHighFastHigh
SOLHighFastMedium
DAILowNear-instantMedium

As the table shows, USDC offers the best balance of stability and ecosystem support. While DAI is also stable, USDC generally has deeper liquidity and broader support in payment processors. ETH and SOL are excellent for gas fees, but using them for the actual API payment value exposes your business to market swings.

Handle payment errors and retries

When an agent hits a 402 Payment Required status, it usually means the API gateway has blocked the request due to unpaid invoices, exceeded quotas, or missing payment methods. Unlike standard 400 or 500 errors, 402s are intentional blocks that require explicit financial resolution before the agent can proceed.

Debug the 402 cause

First, inspect the response body. Providers like Hugging Face or Microsoft often include specific details in the JSON payload, such as "You've exceeded monthly quota" or "Payment methods are being prepared." This distinction is critical: quota errors require waiting or upgrading, while payment method errors require manual intervention in the provider's dashboard.

Implement retry logic

Do not retry immediately on a 402. Unlike transient network errors, 402s are persistent until the financial state changes. Implement an exponential backoff strategy with a longer initial delay (e.g., 30 seconds) and a maximum retry count of 3. If the error persists after retries, trigger an alert or fallback mechanism rather than burning through rate limits.

Manage quota limits proactively

Set up webhook notifications for quota warnings (e.g., 80% usage) so your system can adjust behavior before hitting the hard 402 block. For agentic workflows, consider implementing a "soft stop" where the agent completes its current task and pauses, waiting for human approval or auto-top-up confirmation before resuming.

Validate the market for pay-per-API

The shift from flat subscriptions to pay-per-API is a structural response to how AI agents actually work. Agents don't think in monthly retainers; they think in tokens, operations, and latency. When an agent needs to fetch real-time data, it makes a call. When it finishes, it stops. Charging for idle capacity in this model creates friction that kills adoption.

Traditional API pricing relied on volume tiers—$0.001 per call or $1 per 1,000 requests. This worked when humans made the requests. Now, agentic workflows generate bursts of traffic that are unpredictable and often low-value per transaction. Providers are realizing that the "pay-per-call" model is dying because it doesn't account for the compute cost of the agent's reasoning, not just the network hop. The new standard is usage-based pricing that aligns cost with the actual utility delivered to the agent's goal.

73%
of AI developers prefer usage-based pricing

For your implementation, this means you need to validate that your target users are willing to pay for precision, not access. If you're building an x402-compatible service, your market validation should focus on whether the value of the specific API call justifies the micro-transaction cost. If the cost per call exceeds the perceived value of the data returned, the agent will simply not make the call. Validate this by tracking call success rates against price points, not just total revenue.

402 Endpoint Launch Checklist

Before you push your agentic payment flow to production, verify these core requirements. A misconfigured 402 response breaks the entire agent loop.

The Pay-Per-API 402
  • Response Code: Ensure endpoints return exactly 402 Payment Required when the wallet is empty or the transaction fails. Do not use 401 or 403.
  • Payment Data: The response body must include valid x-payments headers or a JSON payload with the uri (payment URL) and price.
  • Agent Compatibility: Test with an x402-compliant agent. The agent needs to parse the payment instruction and execute the transaction automatically.
  • Idempotency: Verify that retrying a failed payment doesn't double-charge the user or duplicate the API request.
  • Fallback Handling: Define a clear error message for when the payment gateway is unreachable, so the agent knows to retry rather than crash.

Common 402 integration: what to check next

When building agentic payment flows, developers often hit specific edge cases with the 402 status code. Since 402 is reserved for future use in the official HTTP specification, implementation details rely heavily on community standards and libraries like x402 rather than rigid browser defaults.

Here are the most frequent technical hurdles and how to resolve them.