API Resources
The SDK provides typed access to all Computer Agents API resources. Each resource manager is available as a property on the client.
import { ComputerAgentsClient } from 'computer-agents';
const client = new ComputerAgentsClient({
apiKey: process.env.COMPUTER_AGENTS_API_KEY
});
// Access resources
client.threads // Thread management
client.environments // Environment management
client.agents // Agent configuration
client.files // File operations
client.schedules // Scheduled tasks
client.budget // Budget status
client.billing // Usage records
client.git // Git operationsThreads
Threads represent persistent conversation contexts. Use them for multi-turn interactions with agents.
Create a Thread
const thread = await client.threads.create({
environmentId: 'env_xxx'
});
console.log(thread.id); // 'thread_abc123'Send a Message
const result = await client.threads.sendMessage(thread.id, {
content: 'Create a Python calculator',
onEvent: (event) => {
console.log(event.type);
}
});
console.log(result.content); // Agent response
console.log(result.run); // Run details (tokens, status)List Threads
const { data: threads } = await client.threads.list();
threads.forEach(t => {
console.log(`${t.id}: ${t.messageCount} messages`);
});Get Thread Details
const thread = await client.threads.get('thread_xxx');
console.log(thread.status); // 'active' | 'completed' | 'cancelled'Get Thread Messages
const { data: messages } = await client.threads.getMessages('thread_xxx');
messages.forEach(m => {
console.log(`[${m.role}]: ${m.content}`);
});Delete a Thread
await client.threads.delete('thread_xxx');Environments
Environments are isolated execution containers with their own file systems and configurations.
List Environments
const environments = await client.environments.list();
environments.forEach(env => {
console.log(`${env.name} (${env.id}) - ${env.isDefault ? 'default' : ''}`);
});Create an Environment
const env = await client.environments.create({
name: 'my-project',
description: 'Project workspace',
internetAccess: true,
isDefault: false,
environmentVariables: [
{ key: 'NODE_ENV', value: 'development' }
],
secrets: [
{ key: 'API_KEY', value: 'secret-value' }
]
});Get an Environment
const env = await client.environments.get('env_xxx');
console.log(env.name, env.internetAccess);Update an Environment
const updated = await client.environments.update('env_xxx', {
name: 'renamed-project',
internetAccess: false
});Delete an Environment
await client.environments.delete('env_xxx');Files
Manage files within environment workspaces.
List Files
const files = await client.files.listFiles('env_xxx');
files.forEach(f => {
const icon = f.type === 'directory' ? '📁' : '📄';
console.log(`${icon} ${f.path} ${f.size ? `(${f.size} bytes)` : ''}`);
});Upload a File
const result = await client.files.uploadFile({
environmentId: 'env_xxx',
filename: 'app.py',
path: 'src', // Optional subdirectory
content: 'print("Hello, World!")'
});
console.log(`Uploaded: ${result.path}`);Download a File
// As string
const content = await client.files.getFile('env_xxx', 'src/app.py');
console.log(content);
// As Buffer (for binary files)
const buffer = await client.files.downloadFile('env_xxx', 'image.png');Move/Rename a File
await client.files.moveFile({
environmentId: 'env_xxx',
sourcePath: 'old-name.py',
destPath: 'new-name.py'
});Delete a File
await client.files.deleteFile('env_xxx', 'src/app.py');Agents
Configure agent behaviors and settings.
List Agents
const agents = await client.agents.list();
agents.forEach(agent => {
console.log(`${agent.name} (${agent.model})`);
});Create an Agent
const agent = await client.agents.create({
name: 'Code Assistant',
model: 'gpt-4o',
instructions: 'You are a helpful coding assistant. Write clean, tested code.',
reasoningEffort: 'medium'
});Get an Agent
const agent = await client.agents.get('agent_xxx');
console.log(agent.instructions);Update an Agent
const updated = await client.agents.update('agent_xxx', {
instructions: 'Updated instructions for the agent.',
model: 'gpt-4o-mini'
});Delete an Agent
await client.agents.delete('agent_xxx');Schedules
Automate task execution on a schedule.
List Schedules
const schedules = await client.schedules.list();
schedules.forEach(s => {
console.log(`${s.name}: ${s.cronExpression} (${s.enabled ? 'enabled' : 'disabled'})`);
});Create a Schedule
const schedule = await client.schedules.create({
name: 'Daily Report',
agentId: 'agent_xxx',
agentName: 'Reporter',
task: 'Generate a daily status report and save to reports/',
scheduleType: 'recurring',
cronExpression: '0 9 * * *', // Daily at 9 AM
timezone: 'America/New_York',
enabled: true
});Update a Schedule
await client.schedules.update('schedule_xxx', {
enabled: false // Pause the schedule
});Delete a Schedule
await client.schedules.delete('schedule_xxx');Budget & Billing
Track usage and manage budgets.
Get Budget Status
const status = await client.budget.getStatus();
console.log(`Balance: $${(status.balance / 100).toFixed(2)}`);
console.log(`Spent: $${(status.spent / 100).toFixed(2)}`);
console.log(`Remaining: $${(status.remaining / 100).toFixed(2)}`);Check If Can Execute
const { canExecute, reason } = await client.budget.canExecute();
if (!canExecute) {
console.error('Cannot execute:', reason);
}Get Billing Records
const { records } = await client.budget.getRecords({ limit: 10 });
records.forEach(r => {
console.log(`${r.type}: $${(r.amount / 100).toFixed(2)} - ${r.description}`);
});Get Usage Statistics
const stats = await client.billing.getStats({ period: 'month' });
console.log(`Total cost: $${(stats.totalCost / 100).toFixed(2)}`);
console.log(`Total tokens: ${stats.totalTokens}`);
console.log(`Total runs: ${stats.totalRuns}`);Git Operations
Perform git operations on environment workspaces.
Get Diff
const diff = await client.git.diff('env_xxx');
diff.diffs.forEach(d => {
console.log(`${d.path}: +${d.additions} -${d.deletions}`);
});Commit Changes
const result = await client.git.commit('env_xxx', {
message: 'Add user authentication feature',
author: {
name: 'AI Agent',
email: 'agent@computer-agents.com'
}
});
console.log(`Committed: ${result.commit.sha}`);Push to Remote
const result = await client.git.push('env_xxx', {
remote: 'origin',
branch: 'main'
});
console.log(`Pushed ${result.push.commits} commits`);Health & Metrics
Check API status and performance.
Health Check
const health = await client.health();
console.log(`Status: ${health.status}`);
console.log(`Uptime: ${health.uptime}s`);Get Metrics
const metrics = await client.metrics();
console.log(`Total executions: ${metrics.totalExecutions}`);
console.log(`Success rate: ${(metrics.successfulExecutions / metrics.totalExecutions * 100).toFixed(1)}%`);Next Steps
- Streaming & Events - Handle real-time execution events
- Advanced Patterns - Production patterns and best practices
Last updated on