Skip to main content
Polling allows you to monitor session progress and retrieve results by periodically checking the session status. This is an alternative to webhooks that’s useful when you can’t receive incoming HTTP requests or prefer a pull-based approach.

Usage

import { SimplexClient, SessionStatusResponse } from 'simplex-ts';

const client = new SimplexClient({
  apiKey: process.env.SIMPLEX_API_KEY!
});

// Start a workflow
const result = await client.workflows.run(workflowId, {
  variables: { url: 'https://example.com' }
});

const sessionId = result.session_id;

// Poll until complete
let status: SessionStatusResponse;
do {
  status = await client.getSessionStatus(sessionId);

  if (status.paused) {
    console.log('Session paused, waiting for resume...');
  } else if (status.in_progress) {
    console.log('Session still running...');
  }

  if (status.in_progress) {
    await new Promise(resolve => setTimeout(resolve, 5000)); // Wait 5 seconds
  }
} while (status.in_progress);

// Session complete
if (status.success) {
  console.log('Session completed successfully!');
  console.log('Files downloaded:', status.file_metadata);
} else {
  console.log('Session failed');
}

Response structure

When polling session status, you’ll receive a response with the following fields:
in_progress
boolean
required
true while the session is running, false when complete
success
boolean | null
required
Session outcome: null while running, true if successful, false if failed
workflow_metadata
object
Metadata set in the workflow definition
session_metadata
object
Custom metadata you provided when starting the session
file_metadata
array
Metadata for files downloaded during the session
scraper_outputs
array
Data collected by scraper actions during the session
structured_output
object | null
Structured output fields from workflow execution. null while the session is in progress, populated at session close.
paused
boolean
true if the session is currently paused (waiting to be resumed), false otherwise
paused_key
string
The pause key identifier if the session is paused, empty string otherwise

Polling with timeout

For production use, always implement a timeout to prevent infinite polling:
import { SimplexClient, SessionStatusResponse } from 'simplex-ts';

async function pollWithTimeout(
  client: SimplexClient,
  sessionId: string,
  timeoutMs: number = 300000,  // 5 minutes default
  intervalMs: number = 5000    // 5 seconds default
): Promise<SessionStatusResponse> {
  const startTime = Date.now();

  while (Date.now() - startTime < timeoutMs) {
    const status = await client.getSessionStatus(sessionId);

    if (!status.in_progress) {
      return status;
    }

    await new Promise(resolve => setTimeout(resolve, intervalMs));
  }

  throw new Error(`Polling timed out after ${timeoutMs}ms`);
}

// Usage
try {
  const status = await pollWithTimeout(client, sessionId, 600000); // 10 min timeout
  console.log('Session result:', status.success);
} catch (error) {
  console.error('Polling failed:', error);
}

Monitoring file downloads

The file_metadata field updates in real-time as file_metadata are downloaded during the session. You can use this to track download progress:
let lastFileCount = 0;

while (true) {
  const status = await client.getSessionStatus(sessionId);

  // Check for new file_metadata
  const currentFileCount = status.file_metadata?.length ?? 0;
  if (currentFileCount > lastFileCount) {
    const newFiles = status.file_metadata!.slice(lastFileCount);
    for (const file of newFiles) {
      console.log(`New file downloaded: ${file.filename} (${file.file_size} bytes)`);
    }
    lastFileCount = currentFileCount;
  }

  if (!status.in_progress) {
    break;
  }

  await new Promise(resolve => setTimeout(resolve, 5000));
}