AgentWallex logo
Docs

TypeScript SDK Reference

Complete reference for the @agentwallex/sdk package

Installation

npm install @agentwallex/sdk

Client Initialization

import { AgentWallex } from "@agentwallex/sdk";
 
const aw = new AgentWallex({
  apiKey: string;          // Your API key
  environment?: "sandbox" | "production";  // Default: "sandbox"
  baseUrl?: string;        // Custom endpoint (optional)
  timeout?: number;        // Request timeout in ms (default: 30000)
});

Agents

aw.agents.create(params)

Create a new agent with an MPC-secured wallet.

const agent = await aw.agents.create({
  name: "my-agent",
  chain: "ethereum" | "polygon" | "base" | "arbitrum",
  policies?: {
    maxTransactionAmount?: string;
    dailyLimit?: string;
    allowedAddresses?: string[];
    allowedTokens?: string[];
    requireHumanApproval?: boolean;
    humanApprovalThreshold?: string;
  },
  metadata?: Record<string, string>;
});

Returns:

FieldTypeDescription
idstringUnique agent identifier
namestringAgent display name
wallet.addressstringOn-chain wallet address
wallet.chainstringBlockchain network
statusstring"active" or "frozen"
createdAtstringISO 8601 timestamp

aw.agents.get(agentId)

Retrieve an agent by ID.

const agent = await aw.agents.get("agent_abc123");

aw.agents.list(params?)

List all agents for your account.

const { agents, pagination } = await aw.agents.list({
  limit: 20,
  offset: 0,
  status: "active",
});

aw.agents.freeze(agentId)

Immediately freeze an agent's wallet, preventing all transactions.

await aw.agents.freeze("agent_abc123");

aw.agents.unfreeze(agentId)

Re-enable an agent's wallet after a freeze.

await aw.agents.unfreeze("agent_abc123");

Payments

aw.payments.send(params)

Send a direct on-chain payment.

const tx = await aw.payments.send({
  agentId: "agent_abc123",
  to: "0xRecipientAddress",
  amount: "50.00",
  token: "USDC",
  memo?: "Payment for API access",
});

Returns:

FieldTypeDescription
idstringTransaction ID
hashstringOn-chain transaction hash
statusstring"pending", "confirmed", "failed"
amountstringAmount sent
tokenstringToken symbol
feestringNetwork gas fee paid

aw.payments.get(transactionId)

Get transaction details and status.

const tx = await aw.payments.get("tx_xyz789");

aw.payments.list(params)

List transactions for an agent.

const { transactions } = await aw.payments.list({
  agentId: "agent_abc123",
  limit: 50,
  status: "confirmed",
});

x402 Micropayments

aw.x402.createSession(params)

Create an x402 payment session (maps to POST /api/v1/x402/sessions).

const session = await aw.x402.createSession({
  agentId: "agent_abc123",      // -> agent_id
  budgetLimit: "100.00",         // -> budget_limit
  chain: "eip155:84532",
  ttlSeconds: 3600,              // -> ttl_seconds
  allowedUrls: ["https://paid-api.example.com/v1/data"],
});

aw.x402.check(params)

Check whether a target URL supports x402 payment negotiation.

const info = await aw.x402.check({
  url: "https://paid-api.example.com/v1/data",
});

aw.x402.pay(params)

Negotiate and complete one x402 payment attempt.

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)

Pay with an existing session budget.

await aw.x402.sessionPay(session.id, {
  targetUrl: "https://paid-api.example.com/v1/data",
});

aw.x402.httpInterceptor(params)

Create an interceptor that automatically handles x402 v2 challenges:

  • reads PAYMENT-REQUIRED
  • submits payment via AgentWallex
  • retries with PAYMENT-SIGNATURE
const fetchWithPayment = aw.x402.httpInterceptor({
  agentId: "agent_abc123",
  chain: "eip155:84532",
});

Policies

aw.policies.update(agentId, policies)

Update an agent's spending policies.

await aw.policies.update("agent_abc123", {
  maxTransactionAmount: "200",
  dailyLimit: "2000",
  allowedAddresses: ["0xAddr1", "0xAddr2"],
  velocityLimit: { maxCount: 100, windowSeconds: 3600 },
});

aw.policies.get(agentId)

Get current policies for an agent.

const policies = await aw.policies.get("agent_abc123");

Webhooks

aw.webhooks.create(params)

Register a webhook endpoint for payment events.

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",
});

Webhook Event Types

EventDescription
payment.completedTransaction confirmed on-chain
payment.failedTransaction failed or reverted
payment.pendingTransaction submitted, awaiting confirmation
agent.frozenAgent wallet was frozen
agent.unfrozenAgent wallet was unfrozen
policy.violatedTransaction rejected by policy engine
x402.session.expiredx402 session budget or TTL exceeded

Error Handling

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}`);
  }
}

TypeScript Types

All types are exported from the package:

import type {
  Agent,
  Payment,
  Transaction,
  Policy,
  X402Session,
  WebhookEvent,
} from "@agentwallex/sdk";