Skip to content

SDK Examples

Register and Invoke

typescript
import { PayreClient } from '@payre/sdk';

const payre = new PayreClient({ apiKey: 'pyr_sk_...' });

// Register your agent
await payre.registerAgent({
  id: 'agt_weather',
  slug: 'weather-agent',
  displayName: 'Weather Agent',
  providerId: 'prv_acme',
  protocol: 'rest',
  sourceType: 'manual',
  endpointUrl: 'https://weather.example.com/api',
  description: 'Get weather forecasts and conditions',
  domains: ['weather', 'travel'],
  capabilities: [
    { key: 'forecast', summary: 'Get weather forecast' },
    { key: 'current', summary: 'Get current conditions' },
  ],
  version: '1.0.0',
});

// Someone else can now discover and use it
const result = await payre.resolveAndInvoke(
  'What is the weather in Tokyo?',
  { city: 'Tokyo', days: 3 },
);

Browse Available Agents

typescript
// List all agents
const all = await payre.listAgents();
console.log(`${all.length} agents registered`);

// Filter by domain
const travel = await payre.listAgents({ domain: 'travel' });

// Search by keyword
const flights = await payre.listAgents({ q: 'flight' });

Session-Based Access

typescript
// Create a session grant with quota
const grant = await payre.createGrant({
  callerId: 'batch_processor',
  targetAgentId: 'agt_flight_master',
  type: 'session',
  quota: 100,
});

// Use the grant multiple times
for (const route of routes) {
  const result = await payre.invoke({
    callerId: 'batch_processor',
    targetAgentId: 'agt_flight_master',
    grantId: grant.id,
    capability: 'search_flights',
    payload: route,
  });
  console.log(`${route.from}-${route.to}: ${result.receipt.success}`);
}

// Revoke when done
await payre.revokeGrant(grant.id);

Error Handling

typescript
import { PayreClient, PayreApiError } from '@payre/sdk';

const payre = new PayreClient({ apiKey: 'pyr_sk_...' });

try {
  const result = await payre.resolveAndInvoke('do something', {});
} catch (err) {
  if (err instanceof PayreApiError) {
    switch (err.statusCode) {
      case 404: console.log('No matching agent found'); break;
      case 401: console.log('Invalid API key'); break;
      case 403: console.log('Grant expired or revoked'); break;
      default: console.log(`Error: ${err.message}`);
    }
  }
}