← Clawy  /  快速开始

通过 x402 调用付费 AI API — 仅需 5 分钟

无需 API 密钥、无需注册。你请求一个端点,服务器返回 402 和价格,你的客户端在 Base 上签署一次性 USDC 授权并重试。下面是完整的单文件可运行示例。

Live OpenAI-compatible USDC on Base EIP-3009

你需要什么

低成本起步。先用 /v1/chat/auto 测试 —— 成本优化层,$0.04 USDC/次。跑通后再把 ENDPOINT 换成 /v1/chat/opus($0.15,Claude Opus 4.8)或 /v1/chat/chatgpt($0.10,GPT-5.5)。

流程

1
把你的消息 POST 到端点(带支付)→ 服务器返回 402,价格在 PAYMENT-REQUIRED 头中(base64 JSON)。
2
为确切的 USDC 金额签署 EIP-3009 transferWithAuthorization,付款给要求中的地址。
3
X-PAYMENT 头携带已签名负载重试同一个 POST → 200 OK,返回 OpenAI 格式的 chat.completion

可复制粘贴的客户端

保存为 pay-and-call.mjs,然后运行 X402_PRIVATE_KEY=0x... node pay-and-call.mjs

// pay-and-call.mjs — pay Clawy in USDC over x402 and read the answer.
// Setup:  npm i viem
import { privateKeyToAccount } from "viem/accounts";
import { randomBytes } from "crypto";

const GATEWAY  = "https://clawy.uk";
const ENDPOINT = "/v1/chat/auto";  // cheapest tier: $0.04 USDC/call
const MESSAGE  = "Reply with exactly: HELLO";
const USDC     = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"; // USDC on Base
const CHAIN_ID = 8453;                                          // Base mainnet

const account = privateKeyToAccount(process.env.X402_PRIVATE_KEY);
const body = JSON.stringify({ messages: [{ role: "user", content: MESSAGE }] });
const headers = { "Content-Type": "application/json" };

// 1) Unpaid request -> 402 with the price in the PAYMENT-REQUIRED header
const r1 = await fetch(GATEWAY + ENDPOINT, { method: "POST", headers, body });
if (r1.status !== 402) throw new Error("expected 402, got " + r1.status);
const reqs = JSON.parse(Buffer.from(r1.headers.get("payment-required"), "base64").toString());
const a = reqs.accepts[0];   // { scheme:"exact", network, amount, payTo, ... }
console.log("price:", a.amount, "(6-dp USDC) ->", a.payTo);

// 2) Sign an EIP-3009 transferWithAuthorization for the exact amount
const nonce = "0x" + randomBytes(32).toString("hex");
const validBefore = BigInt(Math.floor(Date.now() / 1000) + 300); // valid 5 min
const signature = await account.signTypedData({
  domain: { name: "USD Coin", version: "2", chainId: CHAIN_ID, verifyingContract: USDC },
  types: { TransferWithAuthorization: [
    { name: "from", type: "address" }, { name: "to", type: "address" },
    { name: "value", type: "uint256" }, { name: "validAfter", type: "uint256" },
    { name: "validBefore", type: "uint256" }, { name: "nonce", type: "bytes32" },
  ] },
  primaryType: "TransferWithAuthorization",
  message: { from: account.address, to: a.payTo, value: BigInt(a.amount),
             validAfter: 0n, validBefore, nonce },
});

// 3) Build the X-PAYMENT header and retry -> 200 + OpenAI-shape response
const xPayment = Buffer.from(JSON.stringify({
  x402Version: 2, scheme: "exact", network: a.network,
  payload: { signature, authorization: {
    from: account.address, to: a.payTo, value: a.amount,
    validAfter: "0", validBefore: validBefore.toString(), nonce } },
})).toString("base64");

const r2 = await fetch(GATEWAY + ENDPOINT, {
  method: "POST", headers: { ...headers, "X-PAYMENT": xPayment }, body,
});
const out = await r2.json();
console.log(out.choices[0].message.content);  // -> HELLO
console.log("served by:", out.x_clawy?.provider);

这是最简可行路径。生产环境请加上余额与错误处理 —— 更完整的参考(测试网支持、余额检查、dry-run)在仓库中的 scripts/x402-client.mjs

不想自己手写?现成的 x402 客户端已经为你封装了这套 402 → 支付 → 重试 循环 —— 见 x402.org 上的客户端库(例如带支付的 fetch 封装)。Clawy 是标准的 x402 v2 服务器,任何合规客户端都能用。

面向 AI 智能体(无需人工)

发现文件让自主客户端自行找到 Clawy、查询价格并调用:

诚实提示。后端是 OpenAI 与 Anthropic 的官方计费 API;每个响应都暴露 x_clawy.provider / x_clawy.delivery,让你始终知道是哪个模型处理了请求。支付通过 Coinbase facilitator 在 Base 主网以 USDC 结算。聊天输出由模型生成,不保证正确 —— 凡是你要依赖的内容请自行核实。