This page contains practical examples for the Asteroid Odyssey TypeScript SDK.

Basic Agent Execution

import { AsteroidClient, executeAgent, waitForExecutionResult } from 'asteroid-odyssey';

async function basicExample() {
  const client = AsteroidClient('YOUR_API_KEY');
  
  const executionId = await executeAgent(client, 'YOUR_AGENT_ID', {
    dynamic_data: {
      DATA: "First name: John, Last name: Smith"
    },
    agent_profile_id: 'YOUR_AGENT_PROFILE_ID'
  });
  
  const result = await waitForExecutionResult(client, executionId);
  console.log('Result:', result);
}

basicExample().catch(console.error);

Checking Status While Running

import { AsteroidClient, executeAgent, getExecutionStatus } from 'asteroid-odyssey';

async function monitorExecution() {
  const client = AsteroidClient('YOUR_API_KEY');
  
  const executionId = await executeAgent(client, 'YOUR_AGENT_ID', {
    dynamic_data: {
      DATA: "First name: John, Last name: Smith"
    },
    agent_profile_id: 'YOUR_AGENT_PROFILE_ID'
  });
  
  console.log('Execution started:', executionId);
  
  // Check status periodically
  const checkStatus = async () => {
    const status = await getExecutionStatus(client, executionId);
    console.log(`Status: ${status.status}`);
    
    // Continue checking if status is not final
    // Possible statuses: 'starting' | 'running' | 'paused' | 'awaiting_confirmation' | 'completed' | 'failed' | 'cancelled'
    const finalStatuses = ['completed', 'failed', 'cancelled'];
    if (!finalStatuses.includes(status.status)) {
      setTimeout(checkStatus, 2000); // Check again in 2 seconds
    } else {
      console.log('Execution finished with final status:', status.status);
    }
  };
  
  checkStatus();
}

monitorExecution().catch(console.error);

Complex Form Data

import { AsteroidClient, executeAgent, waitForExecutionResult } from 'asteroid-odyssey';

async function complexFormExample() {
  const client = AsteroidClient('YOUR_API_KEY');
  
  const formData = {
    personalInfo: {
      firstName: "John",
      lastName: "Smith",
      email: "john.smith@example.com"
    },
    preferences: {
      newsletter: true,
      notifications: false
    },
    address: {
      street: "123 Main St",
      city: "Anytown",
      zipCode: "12345"
    }
  };
  
  const executionId = await executeAgent(client, 'YOUR_AGENT_ID', {
    dynamic_data: {
      DATA: JSON.stringify(formData)
    },
    agent_profile_id: 'YOUR_AGENT_PROFILE_ID'
  });
  
  const result = await waitForExecutionResult(client, executionId);
  console.log('Form submission result:', result);
}

Error Handling with Retries

import { AsteroidClient, executeAgent, waitForExecutionResult } from 'asteroid-odyssey';

async function robustExecution() {
  const client = AsteroidClient('YOUR_API_KEY');
  const maxRetries = 3;
  let attempt = 0;
  
  while (attempt < maxRetries) {
    try {
      const executionId = await executeAgent(client, 'YOUR_AGENT_ID', {
        DATA: "First name: John, Last name: Smith"
      });
      
      const result = await waitForExecutionResult(client, executionId);
      console.log('Success on attempt', attempt + 1);
      return result;
      
    } catch (error) {
      attempt++;
      console.log(`Attempt ${attempt} failed:`, error.message);
      
      if (attempt >= maxRetries) {
        throw new Error(`Failed after ${maxRetries} attempts`);
      }
      
      // Wait before retrying
      await new Promise(resolve => setTimeout(resolve, 1000 * attempt));
    }
  }
}
These examples cover the most common patterns you’ll need when working with the Asteroid Odyssey TypeScript SDK.