← Clawy / クイックスタート
API キー不要・登録不要。エンドポイントにアクセスすると、サーバーは価格付きで 402 を返し、クライアントは Base 上で 1 回限りの USDC 認可に署名して再試行します。以下が、動作する 1 つのファイルとしての全体です。
npm i viem/v1/chat/auto でテスト —— コスト最適化ティアで $0.04 USDC/回。動いたら ENDPOINT を /v1/chat/opus($0.15、Claude Opus 4.8)や /v1/chat/chatgpt($0.10、GPT-5.5)に切り替え。
PAYMENT-REQUIRED ヘッダー(base64 JSON)に入れて 402 を返す。transferWithAuthorization に署名する。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);
fetch ラッパー)を参照。Clawy は標準の x402 v2 サーバーなので、準拠クライアントなら何でも動作します。
ディスカバリーファイルにより、自律クライアントは自力で Clawy を発見し、価格を確認し、呼び出せます:
/llms.txt — LLM クライアント向けの 1 ページ要約/openapi.json — エンドポイントごとの支払い情報付き OpenAPI 3.1 仕様/.well-known/x402.json — x402 Bazaar マニフェスト(入出力スキーマ)/.well-known/agent-services.json — agent-services カタログ/health — ライブ状態+現在の料金x_clawy.provider / x_clawy.delivery を公開し、どのモデルが処理したか常に分かります。支払いは Coinbase ファシリテーター経由で Base メインネットの USDC で決済されます。チャット出力はモデル生成であり正確性は保証されません —— 依拠する内容は必ず検証してください。