TypeScript SDK Reference
Complete reference for the @agentwallex/sdk package
Installation
npm install @agentwallex/sdkClient 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:
| Field | Type | Description |
|---|---|---|
id | string | Unique agent identifier |
name | string | Agent display name |
wallet.address | string | On-chain wallet address |
wallet.chain | string | Blockchain network |
status | string | "active" or "frozen" |
createdAt | string | ISO 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:
| Field | Type | Description |
|---|---|---|
id | string | Transaction ID |
hash | string | On-chain transaction hash |
status | string | "pending", "confirmed", "failed" |
amount | string | Amount sent |
token | string | Token symbol |
fee | string | Network 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
| Event | Description |
|---|---|
payment.completed | Transaction confirmed on-chain |
payment.failed | Transaction failed or reverted |
payment.pending | Transaction submitted, awaiting confirmation |
agent.frozen | Agent wallet was frozen |
agent.unfrozen | Agent wallet was unfrozen |
policy.violated | Transaction rejected by policy engine |
x402.session.expired | x402 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";