Skip to main content
The Agents API enables discovery and communication with agents in your Private Connect workspace. Use it to build distributed systems, coordinate workflows, and orchestrate multi-agent operations.

Overview

Access the Agents API through the agents property:
import { PrivateConnect } from '@privateconnect/sdk';

const pc = new PrivateConnect({ apiKey: 'your-api-key' });
const agents = await pc.agents.list();

Methods

list()

List all agents in the workspace.
list(options?: { onlineOnly?: boolean }): Promise<Agent[]>
options
object
Optional filtering options
Agent[]
Agent[]
Array of agents in the workspace

Example

const agents = await pc.agents.list();

agents.forEach(agent => {
  console.log(`${agent.label} (${agent.isOnline ? 'online' : 'offline'})`);
  console.log(`  Capabilities: ${agent.capabilities.join(', ')}`);
  console.log(`  Services: ${agent.services.join(', ')}`);
});

findByCapability()

Find agents that have a specific capability.
findByCapability(capability: string): Promise<Agent[]>
capability
string
required
The capability name to search for
Agent[]
Agent[]
Array of agents with the specified capability

Example

// Find all agents that can deploy
const deployers = await pc.agents.findByCapability('deploy');

if (deployers.length > 0) {
  console.log(`Found ${deployers.length} agents with deploy capability`);
  deployers.forEach(agent => {
    console.log(`  - ${agent.label}`);
  });
}

registerCapabilities()

Register capabilities for this agent.
registerCapabilities(
  capabilities: Array<{ name: string; metadata?: Record<string, unknown> }>
): Promise<void>
capabilities
array
required
Array of capabilities to register

Example

await pc.agents.registerCapabilities([
  { 
    name: 'deploy',
    metadata: { 
      environments: ['staging', 'production'],
      maxConcurrent: 3
    }
  },
  { 
    name: 'monitor',
    metadata: { 
      interval: 60,
      metrics: ['cpu', 'memory', 'disk']
    }
  },
]);

console.log('Capabilities registered');

sendMessage()

Send a message to another agent.
sendMessage(
  toAgentId: string,
  payload: Record<string, unknown>,
  options?: { 
    channel?: string; 
    type?: 'request' | 'response' | 'event' 
  }
): Promise<{ messageId: string }>
toAgentId
string
required
Target agent ID
payload
object
required
Message payload (any JSON-serializable object)
options
object
Optional message options
messageId
string
Unique identifier for the sent message

Example

const { messageId } = await pc.agents.sendMessage(
  'agent-xyz',
  { action: 'deploy', version: '1.2.3' }
);

console.log(`Message sent: ${messageId}`);

broadcast()

Broadcast a message to all online agents.
broadcast(
  payload: Record<string, unknown>,
  options?: { channel?: string }
): Promise<{ sent: number }>
payload
object
required
Message payload to broadcast
options
object
Optional broadcast options
sent
number
Number of agents that received the message

Example

// Notify all agents of maintenance window
const { sent } = await pc.agents.broadcast(
  {
    type: 'maintenance',
    startsAt: '2024-03-15T02:00:00Z',
    duration: 60,
    message: 'Database upgrade in progress'
  },
  { channel: 'system' }
);

console.log(`Notified ${sent} agents`);

getMessages()

Retrieve messages for this agent.
getMessages(options?: {
  channel?: string;
  unreadOnly?: boolean;
  limit?: number;
}): Promise<Message[]>
options
object
Optional filtering options
Message[]
Message[]
Array of messages

Example

const unread = await pc.agents.getMessages({ unreadOnly: true });

console.log(`You have ${unread.length} unread messages`);

unread.forEach(msg => {
  console.log(`From: ${msg.from.label}`);
  console.log(`Channel: ${msg.channel}`);
  console.log(`Payload:`, msg.payload);
});

markRead()

Mark messages as read.
markRead(messageIds: string[]): Promise<void>
messageIds
string[]
required
Array of message IDs to mark as read

Example

// Get unread messages
const messages = await pc.agents.getMessages({ unreadOnly: true });

// Process them
messages.forEach(msg => {
  console.log(`Processing: ${msg.payload.action}`);
  // ... handle message ...
});

// Mark all as read
const messageIds = messages.map(m => m.id);
await pc.agents.markRead(messageIds);

console.log(`Marked ${messageIds.length} messages as read`);

Complete Example

Here’s a complete example showing agent orchestration:
import { PrivateConnect } from '@privateconnect/sdk';

const pc = new PrivateConnect({ 
  apiKey: process.env.PRIVATECONNECT_API_KEY!,
  agentId: 'orchestrator'
});

// Register capabilities
await pc.agents.registerCapabilities([
  { name: 'orchestrate', metadata: { version: '1.0' } }
]);

// Find deployment agents
const deployers = await pc.agents.findByCapability('deploy');

if (deployers.length === 0) {
  console.log('No deployment agents available');
  process.exit(1);
}

// Send deployment request to first available agent
const target = deployers[0];
const { messageId } = await pc.agents.sendMessage(
  target.id,
  {
    action: 'deploy',
    environment: 'production',
    version: '1.2.3',
    services: ['api', 'worker']
  },
  { 
    channel: 'deployments',
    type: 'request'
  }
);

console.log(`Deployment request sent to ${target.label}`);

// Poll for response
while (true) {
  await new Promise(resolve => setTimeout(resolve, 5000));
  
  const messages = await pc.agents.getMessages({
    channel: 'deployments',
    unreadOnly: true
  });
  
  const response = messages.find(m => m.type === 'response');
  
  if (response) {
    console.log('Deployment complete:', response.payload);
    await pc.agents.markRead([response.id]);
    break;
  }
}

Next Steps

Services API

Connect to services from your agents

Sessions API

Create orchestration sessions

Build docs developers (and LLMs) love