メインコンテンツへスキップ

インストール

npm install @agentwallex/sdk

クライアントの初期化

import { AgentWallex } from "@agentwallex/sdk";

const aw = new AgentWallex({
  apiKey: process.env.AGENTWALLEX_API_KEY!,
  environment: "sandbox",  // "sandbox" | "production"
  baseUrl: undefined,       // Custom endpoint (optional)
  timeout: 30000,           // Request timeout in ms (default: 30000)
});
パラメータデフォルト説明
apiKeystring必須APIキー(awx_...
environment"sandbox" | "production""sandbox"ターゲット環境
baseUrlstringカスタムベースURL(環境設定をオーバーライド)
timeoutnumber30000リクエストタイムアウト(ミリ秒)

エージェント

aw.agents.create(params)

MPCセキュアなウォレットを持つ新しいエージェントを作成します。
const agent = await aw.agents.create({
  name: "my-agent",
  chain: "eip155:8453",
  policies: {
    maxTransactionAmount: "100",
    dailyLimit: "1000",
    allowedAddresses: ["0x..."],
    allowedTokens: ["USDC"],
    requireHumanApproval: false,
  },
  metadata: { team: "growth" },
});
戻り値:
フィールド説明
idstring一意のエージェント識別子
namestringエージェントの表示名
wallet.addressstringオンチェーンウォレットアドレス
wallet.chainstringCAIP-2チェーン識別子
statusstring"active"または"frozen"
createdAtstringISO 8601タイムスタンプ

aw.agents.get(agentId)

IDでエージェントを取得します。
const agent = await aw.agents.get("agent_abc123");

aw.agents.list(params?)

アカウントのすべてのエージェントを一覧表示します。
const { agents, pagination } = await aw.agents.list({
  limit: 20,
  offset: 0,
  status: "active",
});

aw.agents.freeze(agentId)

エージェントのウォレットを即座に凍結し、すべてのトランザクションを防止します。
await aw.agents.freeze("agent_abc123");

aw.agents.unfreeze(agentId)

凍結後にエージェントのウォレットを再有効化します(10分間のクールダウンが必要です)。
await aw.agents.unfreeze("agent_abc123");

支払い

aw.payments.send(params)

直接オンチェーン決済を送信します。
const tx = await aw.payments.send({
  agentId: "agent_abc123",
  to: "0xRecipientAddress",
  amount: "50.00",
  token: "USDC",
  memo: "Payment for API access",
});
戻り値:
フィールド説明
idstringトランザクションID
hashstringオンチェーントランザクションハッシュ
statusstring"pending""confirmed""failed"
amountstring送信された金額
tokenstringトークンシンボル
feestring支払われたネットワークガス手数料

aw.payments.get(transactionId)

トランザクションの詳細とステータスを取得します。
const tx = await aw.payments.get("tx_xyz789");

aw.payments.list(params)

エージェントのトランザクションを一覧表示します。
const { transactions } = await aw.payments.list({
  agentId: "agent_abc123",
  limit: 50,
  status: "confirmed",
});

x402マイクロペイメント

aw.x402.createSession(params)

支出予算を持つx402支払いセッションを作成します。
const session = await aw.x402.createSession({
  agentId: "agent_abc123",
  budgetLimit: "100.00",
  chain: "eip155:84532",
  ttlSeconds: 3600,
  allowedUrls: ["https://paid-api.example.com/v1/data"],
});

aw.x402.check(params)

ターゲットURLがx402支払い交渉をサポートしているか確認します。
const info = await aw.x402.check({
  url: "https://paid-api.example.com/v1/data",
});

aw.x402.pay(params)

1回のx402支払いを交渉して完了します。
const result = await aw.x402.pay({
  agentId: "agent_abc123",
  targetUrl: "https://paid-api.example.com/v1/data",
  sessionId: session.id,     // optional
  chain: "eip155:84532",     // optional
});

aw.x402.sessionPay(sessionId, params)

既存のセッション予算を使用して支払います。
await aw.x402.sessionPay(session.id, {
  targetUrl: "https://paid-api.example.com/v1/data",
});

aw.x402.httpInterceptor(params)

x402 v2チャレンジを自動的に処理するHTTP fetchラッパーを作成します:
const fetchWithPayment = aw.x402.httpInterceptor({
  agentId: "agent_abc123",
  chain: "eip155:84532",
});

// Automatically handles 402 -> pay -> retry flow
const response = await fetchWithPayment("https://paid-api.example.com/v1/data");
const data = await response.json();
インターセプターの動作:
  1. 402レスポンスのPAYMENT-REQUIREDヘッダーを読み取ります
  2. AgentWallex経由で支払いを送信します
  3. PAYMENT-SIGNATUREヘッダー付きでリクエストをリトライします

ポリシー

aw.policies.update(agentId, policies)

エージェントの支出ポリシーを更新します。
await aw.policies.update("agent_abc123", {
  maxTransactionAmount: "200",
  dailyLimit: "2000",
  allowedAddresses: ["0xAddr1", "0xAddr2"],
  velocityLimit: { maxCount: 100, windowSeconds: 3600 },
});

aw.policies.get(agentId)

エージェントの現在のポリシーを取得します。
const policies = await aw.policies.get("agent_abc123");

aw.policies.applyTemplate(agentId, templateName)

エージェントにポリシーテンプレートを適用します。
await aw.policies.applyTemplate("agent_abc123", "conservative");

aw.policies.createTemplate(name, rules)

再利用可能なポリシーテンプレートを作成します。
await aw.policies.createTemplate("my-template", {
  maxTransactionAmount: "200",
  dailyLimit: "2000",
  allowedTokens: ["USDC"],
  velocityLimit: { maxCount: 50, windowSeconds: 3600 },
});

Webhook

aw.webhooks.create(params)

Webhookエンドポイントを登録します。
const webhook = await aw.webhooks.create({
  url: "https://your-app.com/webhooks/agentwallex",
  events: [
    "payment.completed",
    "payment.failed",
    "agent.frozen",
    "policy.violated",
  ],
  secret: "whsec_your_signing_secret",
});

エラーハンドリング

SDKは型付きエラークラスを提供します:
import {
  AgentWallexError,
  PolicyViolationError,
} from "@agentwallex/sdk";

try {
  await aw.payments.send({ ... });
} catch (error) {
  if (error instanceof PolicyViolationError) {
    console.log(`Policy violated: ${error.rule}`);
    console.log(`Details: ${error.message}`);
  } else if (error instanceof AgentWallexError) {
    console.log(`API error: ${error.code} - ${error.message}`);
  }
}
エラークラススローされるタイミング
AgentWallexErrorすべてのAPIエラーの基底クラス
PolicyViolationErrorトランザクションがポリシーエンジンによってブロックされた場合
AuthenticationErrorAPIキーが無効または不足している場合
NotFoundErrorリソースが存在しない場合
RateLimitErrorレート制限を超過した場合

TypeScript型

すべての型がパッケージからエクスポートされています:
import type {
  Agent,
  Payment,
  Transaction,
  Policy,
  X402Session,
  WebhookEvent,
} from "@agentwallex/sdk";