TypeScript SDK

The official TypeScript SDK for AutopayOS. Works with Node.js 18+ and modern browsers.

Installation

Bash
npm install @autopayos/sdk
# or
pnpm add @autopayos/sdk
# or
yarn add @autopayos/sdk

Quick start

TypeScript
import { AutopayosClient } from '@autopayos/sdk';

const client = new AutopayosClient({
  baseUrl: 'https://api.autopayos.com',
  apiKey: 'ak_live_your_api_key',
});

// Request permission
const permission = await client.requestPermission({
  agentDid: 'did:key:z6Mk...',
  principalDid: 'did:key:z6Mp...',
  amount: 50.00,
  currency: 'USD',
});

if (permission.allowed) {
  console.log('Intent approved!');
}

Configuration

Constructor options

TypeScript
interface ClientOptions {
  baseUrl: string;        // API base URL
  apiKey?: string;        // API key for authentication
  agentDid?: string;      // Default agent DID
  timeout?: number;       // Request timeout in ms (default: 30000)
  retries?: number;       // Number of retries (default: 3)
}

const client = new AutopayosClient({
  baseUrl: 'https://api.autopayos.com',
  apiKey: process.env.AUTOPAYOS_API_KEY,
  timeout: 10000,
  retries: 2,
});

Environment-based configuration

TypeScript
const client = new AutopayosClient({
  baseUrl: process.env.NODE_ENV === 'production'
    ? 'https://api.autopayos.com'
    : 'https://sandbox-api.autopayos.com',
  apiKey: process.env.AUTOPAYOS_API_KEY,
});

Core methods

requestPermission

Request permission for an agent to make a purchase.

TypeScript
interface PermissionRequest {
  agentDid: string;
  principalDid: string;
  amount: number;
  currency: string;
  merchantDomain?: string;
  merchantMcc?: string;
  idempotencyKey?: string;
  expiresIn?: number;
}

interface PermissionResponse {
  allowed: boolean;
  intentVc?: VerifiableCredential;
  attestation?: PolicyAttestation;
  reasonCodes?: string[];
  hpRequired?: boolean;
  hpChallenge?: HpChallenge;
}

const result = await client.requestPermission({
  agentDid: 'did:key:z6Mk...',
  principalDid: 'did:key:z6Mp...',
  amount: 100.00,
  currency: 'USD',
  merchantDomain: 'amazon.com',
});

verifyCart

Verify a cart against an intent and policy.

TypeScript
interface CartVerificationRequest {
  intentVc: VerifiableCredential;
  cartVc: VerifiableCredential;
  hpProof?: HpProof;
  idempotencyKey?: string;
}

interface CartVerificationResponse {
  allowed: boolean;
  approvalToken?: string;
  approvalTokenExpiresAt?: string;
  railDecision?: RailDecision;
  reasonCodes?: string[];
}

const result = await client.verifyCart({
  intentVc: permission.intentVc,
  cartVc: merchantCartVc,
});

executePayment

Execute a payment with an approval token.

TypeScript
interface PaymentExecutionRequest {
  approvalToken: string;
  userAuthorization?: string;
}

interface PaymentExecutionResponse {
  pmVc: VerifiableCredential;
  railResponse: RailResponse;
}

const result = await client.executePayment({
  approvalToken: verification.approvalToken,
});

completePurchase

Complete an entire purchase flow in one call.

TypeScript
const result = await client.completePurchase(
  'did:key:z6MkAgent...',   // agentDid
  'did:key:z6MkUser...',    // principalDid
  100.00,                    // amount
  'USD',                     // currency
  'amazon.com',              // merchantDomain
  merchantCartVc             // cartVc
);

console.log('Payment mandate:', result.pmVc);

Policy management

getCurrentPolicy

TypeScript
const policy = await client.getCurrentPolicy();
console.log('Amount cap:', policy.rules.spend.amount_cap);

updatePolicy

TypeScript
await client.updatePolicy(policyId, {
  rules: {
    ...existingRules,
    spend: {
      amount_cap: 200.00,
    },
  },
});

createPolicy

TypeScript
const policy = await client.createPolicy({
  name: 'Shopping Agent Policy',
  rules: {
    version: '2.0',
    spend: {
      amount_cap: 100.00,
      currency: 'USD',
    },
  },
});

simulatePolicy

Test a policy without creating an intent.

TypeScript
const result = await client.simulatePolicy({
  policyId: 'pol_abc123',
  request: {
    amount: 150.00,
    currency: 'USD',
    merchantDomain: 'amazon.com',
  },
});

console.log('Would be allowed:', result.allowed);
console.log('Violations:', result.reasonCodes);

Agent management

registerAgent

TypeScript
const agent = await client.registerAgent({
  name: 'Shopping Assistant',
  description: 'AI-powered shopping agent',
  capabilities: ['browse', 'purchase'],
  publicKey: myPublicKey,
});

console.log('Agent DID:', agent.did);

getAgent

TypeScript
const agent = await client.getAgent(agentDid);
console.log('Status:', agent.status);
console.log('Linked policy:', agent.linkedPolicyId);

getAgents

TypeScript
const agents = await client.getAgents({
  status: 'ACTIVE',
  limit: 50,
});

for (const agent of agents.data) {
  console.log(`${agent.displayName}: ${agent.did}`);
}

revokeAgent

TypeScript
await client.revokeAgent(agentDid, {
  reason: 'Security concern',
});

updateAgentVelocity

TypeScript
await client.updateAgentVelocity(agentDid, {
  maxTransactions: 20,
  maxAmount: 1000.00,
  timeWindow: '24h',
});

Evidence

getEvidence

TypeScript
const evidence = await client.getEvidence({
  last: 50,
  eventTypes: ['PAYMENT_AUTHORIZED'],
  from: '2025-12-01',
});

console.log('Events:', evidence.events.length);
console.log('Chain valid:', evidence.chainValid);

getEvidenceBundle

TypeScript
const bundle = await client.getEvidenceBundle(transactionId);

console.log('Transaction:', bundle.transactionId);
console.log('Events:', bundle.events.length);

verifyEvidenceChain

TypeScript
const result = await client.verifyEvidenceChain({
  fromSeq: 1000,
  toSeq: 1100,
});

if (!result.valid) {
  console.error('Chain broken at:', result.brokenAt);
}

Human Presence

listHpCredentials

TypeScript
const credentials = await client.listHpCredentials(agentDid);

for (const cred of credentials) {
  console.log(`${cred.deviceName}: ${cred.credentialId}`);
}

revokeHpCredential

TypeScript
await client.revokeHpCredential(agentDid, credentialId);

demoHpProof

For testing only:

TypeScript
const hp = await client.demoHpProof({
  agentDid: 'did:key:z6Mk...',
  freshnessSeconds: 300,
});

Demo helpers

demoCart

TypeScript
const cart = await client.demoCart({
  payerDid: 'did:key:z6MkUser...',
  domain: 'test-merchant.autopayos.com',
  amount: 50.00,
  currency: 'USD',
  items: [
    { name: 'Test Product', price: 50.00, quantity: 1 }
  ],
});

Error handling

AutopayosError

TypeScript
import { AutopayosError } from '@autopayos/sdk';

try {
  const permission = await client.requestPermission({ ... });
} catch (error) {
  if (error instanceof AutopayosError) {
    console.error('Code:', error.code);
    console.error('Message:', error.message);
    console.error('Details:', error.details);
    
    switch (error.code) {
      case 'AMOUNT_OVER_CAP':
        // Handle amount limit
        break;
      case 'MERCHANT_NOT_ALLOWED':
        // Handle merchant restriction
        break;
      case 'RATE_LIMITED':
        // Handle rate limiting
        break;
    }
  }
}

Retry logic

The SDK automatically retries on network errors:

TypeScript
const client = new AutopayosClient({
  baseUrl: 'https://api.autopayos.com',
  apiKey: 'ak_live_...',
  retries: 3,  // Retry up to 3 times
});

TypeScript types

VerifiableCredential

TypeScript
interface VerifiableCredential {
  '@context': string[];
  type: string[];
  issuer: string;
  issuanceDate: string;
  expirationDate?: string;
  credentialSubject: Record<string, any>;
  proof?: Proof;
}

PolicyV1

TypeScript
interface PolicyV1 {
  version: '1.0' | '2.0';
  spend: {
    amount_cap: number;
    currency: string;
    usage?: 'single' | 'recurring';
    categories?: string[];
  };
  merchant?: {
    allow_list?: string[];
    deny_list?: string[];
  };
  context?: {
    presence?: 'required' | 'optional';
    presence_threshold?: number;
    time_window?: TimeWindow;
  };
  risk?: {
    velocity_limit?: VelocityLimit;
    anomaly_flags?: string[];
  };
  rails?: {
    allow: string[];
    priorities?: string[];
    fail_over?: boolean;
  };
}

RailDecision

TypeScript
interface RailDecision {
  rail: 'STRIPE' | 'VISA_IC' | 'STRIPE_SPT';
  reason: string;
  merchantConnected?: boolean;
  expiresAt?: string;
}

A2A Client

For A2A protocol communication:

TypeScript
import { A2AClient } from '@autopayos/a2a-client';

const a2a = new A2AClient({
  endpoint: 'https://api.autopayos.com/a2a/gateway',
  agentDid: 'did:key:z6Mk...',
  privateKey: myPrivateKey,
});

const result = await a2a.send({
  type: 'IntentMandate',
  to: 'did:key:z6MkAutoPayOS...',
  body: {
    agentDid: 'did:key:z6Mk...',
    principalDid: 'did:key:z6Mp...',
    request: {
      maxAmount: 100.00,
      currency: 'USD',
    },
  },
});

Best practices

1. Use environment variables

TypeScript
const client = new AutopayosClient({
  baseUrl: process.env.AUTOPAYOS_URL!,
  apiKey: process.env.AUTOPAYOS_API_KEY!,
});

2. Handle all error cases

TypeScript
const permission = await client.requestPermission({ ... });

if (!permission.allowed) {
  for (const code of permission.reasonCodes || []) {
    // Handle each reason
  }
  return;
}

3. Use idempotency keys

TypeScript
const permission = await client.requestPermission({
  ...params,
  idempotencyKey: `intent-${orderId}-${Date.now()}`,
});

4. Log transaction IDs

TypeScript
const payment = await client.executePayment({ approvalToken });
console.log('Mandate ID:', payment.pmVc.credentialSubject.mandateId);

Next steps