Skip to content

s402Your AI agent needs to pay for things.

Chain-agnostic HTTP 402 payment protocol. Five payment schemes. TypeScript and Python implementations — built for agents that spend money autonomously.

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).

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.2.3 · Five payment schemes · 868 tests across TypeScript and Python · 132-vector conformance suite · Apache-2.0 · npm

Released under the Apache 2.0 License.