DEV Community

Yaqing2023
Yaqing2023

Posted on

What is the x402 Payment Protocol? A Complete Guide

The x402 payment protocol is an emerging standard for HTTP-native payments that enables machines — particularly AI agents — to pay for services directly over the web without human intervention.

The Origin: HTTP 402 Payment Required

The x402 protocol gets its name from HTTP status code 402 Payment Required. This status code was reserved in the original HTTP spec for "future use" in digital payments — and that future is now.

When a client requests a paid resource, the server responds with:

HTTP/1.1 402 Payment Required
X-Payment-Address: 0x...
X-Payment-Amount: 0.99
X-Payment-Currency: USDC
X-Payment-Chain: base
Enter fullscreen mode Exit fullscreen mode

The client then signs a payment transaction and resends the request with payment proof.

How x402 Works

1. Client → Server: GET /api/generate-video
2. Server → Client: 402 Payment Required (price: $0.99 USDC)
3. Client: Signs USDC transfer
4. Client → Server: GET /api/generate-video + X-Payment-Proof header
5. Server: Verifies payment on-chain
6. Server → Client: 200 OK + video result
Enter fullscreen mode Exit fullscreen mode

The entire flow happens in seconds, with no invoices, no checkout pages, no human approval.

Why x402 Matters for AI Agents

AI agents cannot:

  • Open bank accounts
  • Get credit cards
  • Complete KYC verification

But they CAN:

  • Hold crypto wallet keys
  • Sign transactions
  • Verify on-chain payments

x402 bridges HTTP (how AI agents communicate) with crypto (how AI agents pay).

x402 vs Traditional Payment APIs

Feature Stripe/PayPal x402 Protocol
Human identity required Yes No
KYC needed Yes No
Settlement time 2-3 days Seconds
Micropayments High fees Low/zero fees
Machine-native No Yes
Global by default Limited Yes

Who is Building on x402?

  • Google AP2 - x402 extension for agent payments
  • Coinbase Agentic Wallets - Based around x402
  • MoltsPay - Open-source x402 implementation for AI agents

Getting Started with x402

The easiest way to implement x402 is with MoltsPay:

npm install moltspay
Enter fullscreen mode Exit fullscreen mode

As a service provider:

npx moltspay init --chain base
npx moltspay start ./my-service
Enter fullscreen mode Exit fullscreen mode

As a client/payer:

import { MoltsPay } from "moltspay";

const client = new MoltsPay({ chain: "base" });
const result = await client.pay(
  "https://example.com/api",
  "generate-video",
  { prompt: "a cat dancing" }
);
Enter fullscreen mode Exit fullscreen mode

x402 Protocol Resources

  • x402 Spec: Built on HTTP semantics
  • MoltsPay (x402 for AI): https://moltspay.com
  • Coinbase x402 Facilitator: Production-ready settlement

The x402 payment protocol is becoming the standard for machine-to-machine payments. As AI agents handle more transactions, x402 provides the HTTP-native payment layer they need.

Top comments (0)