Skip to content

s402Your AI agent needs to pay for things.

The chain-agnostic HTTP 402 protocol for AI agents. Superset of x402 and MPP — same price or cheaper where they overlap, six payment schemes where they can't. TypeScript, Python, Go.

Abstract visualization of digital payment flowing through a network

See It in Action

An AI agent hits a paid API. The server says "pay me." The agent pays and gets the data. Three HTTP requests, zero human intervention.

typescript
import {
  extractRequirementsFromResponse,
  encodePaymentPayload,
  S402_HEADERS,
} from 's402';

async function agentFetch(url: string, buildPayment: PaymentBuilder) {
  const res = await fetch(url);

  if (res.status !== 402) return res;

  // 1. Decode what the server wants
  const requirements = extractRequirementsFromResponse(res);
  if (!requirements) throw new Error('Invalid 402 response');

  // 2. Build and sign a payment (you bring the chain SDK)
  const payment = await buildPayment(requirements);

  // 3. Retry with payment attached
  return fetch(url, {
    headers: { [S402_HEADERS.PAYMENT]: encodePaymentPayload(payment) },
  });
}

s402 defines the wire format — what gets sent over HTTP. You bring your own chain SDK for the how (PTB builders, signers, RPC calls).

Start Where You Are

🤖 I'm building an AI agent Need to pay for APIs, tools, or inference. Start with the Quick Start to wire a client in 5 minutes, then read Which Scheme Do I Need? to pick between Exact, Upto, and Prepaid.

🏗 I run a paid API Want agents to pay you directly — no billing dashboards, no API keys. See the server tutorial for a 402 endpoint in 20 lines, then Fee Ownership & Trust for who pays gas.

🦊 I build a wallet or SDK Need to speak s402 from a client library. Start with the Wire Format Spec and the 161-vector conformance suite. TypeScript, Python, Go adapters exist today.

⚙️ I operate a facilitator Running verify/settle infra for a chain or scheme. Read the Architecture page for the facilitator contract, then Security Model for invariants you must preserve.

🔎 I'm auditing or researching Evaluating s402 for production or academic work. Start with the Whitepaper, Threat Model, and the three-way comparison against x402 and MPP.

🔁 I'm migrating from another protocol Already running x402 or MPP. See Migrating from x402 (one-line middleware swap) or Migrating from MPP (coexistence pattern, no rip-and-replace).

Python

python
import httpx
from s402 import decode_payment_required, encode_payment_payload, S402_HEADERS

url = "https://api.example.com/premium-data"
res = httpx.get(url)
if res.status_code == 402:
    requirements = decode_payment_required(res.headers[S402_HEADERS["PAYMENT_REQUIRED"]])
    payment = build_payment(requirements)  # you bring the chain SDK
    res = httpx.get(url, headers={S402_HEADERS["PAYMENT"]: encode_payment_payload(payment)})
bash
pip install s402   # Python — zero dependencies
npm install s402   # TypeScript — zero dependencies

v0.5.0 · Six payment schemes · 831 tests · 161-vector conformance suite · Apache-2.0 · npm

Released under the Apache 2.0 License.