Skip to main content

Basic Usage

To run perform-web-task asynchronously, set the async parameter to true in your request. The API will return immediately with a workflow_id that you can use to poll for results.
import Anchorbrowser from 'anchorbrowser';

const anchorClient = new Anchorbrowser({
apiKey: process.env.ANCHORBROWSER_API_KEY
});

// Start async task
const response = await anchorClient.tools.performWebTask({
prompt: 'Extract the main heading and first paragraph from the page',
url: 'https://docs.anchorbrowser.io',
async: true
});

console.log('Workflow ID:', response.data.workflow_id);
console.log('Status:', response.data.status);

Polling for Results

After starting an async task, you’ll receive a workflow_id in the response. Use this ID to poll the status endpoint until the task completes.

Status Endpoint

const statusResponse = await anchorClient.tools.getPerformWebTaskStatus(workflowId);

// Status can be: 'RUNNING', 'COMPLETED', or 'FAILED'
if (statusResponse.data.status === 'RUNNING') {
    console.log('Still running...');
} else if (statusResponse.data.status === 'COMPLETED') {
    console.log('Result:', statusResponse.data.result);
} else if (statusResponse.data.status === 'FAILED') {
    console.error('Error:', statusResponse.data.error);
}

Response Statuses

  • RUNNING: The workflow is currently executing
  • COMPLETED: The workflow has completed. The result field contains the task output as a string.
The status will be COMPLETED even if the agent fails to complete the task, since the workflow execution itself succeeded. Always check the result field to verify whether the agent completed the task successfully or encountered an error.
  • FAILED: The workflow has failed. The error field contains the error message

Complete Example

import Anchorbrowser from 'anchorbrowser';

(async () => {
  const anchorClient = new Anchorbrowser({
    apiKey: process.env.ANCHORBROWSER_API_KEY
  });

  // Start multiple async tasks in parallel
  const responses = await Promise.all([
    anchorClient.tools.performWebTask({
      prompt: 'Extract the main heading',
      url: 'https://docs.anchorbrowser.io',
      async: true
    }),
    anchorClient.tools.performWebTask({
      prompt: 'Get the first paragraph',
      url: 'https://example.com',
      async: true
    })
  ]);

  const workflowIds = responses.map(response => response.data.workflow_id);
  console.log('Started', workflowIds.length, 'tasks');

  // Poll all tasks
  const results = await Promise.all(
    workflowIds.map(async (wokflowId) => {
      let status = 'RUNNING';
      while (status === 'RUNNING') {
        await new Promise(resolve => setTimeout(resolve, 2000));
        const statusResponse = await anchorClient.tools.getPerformWebTaskStatus(wokflowId);
        status = statusResponse.data.status;

        if (status === 'COMPLETED') {
          return statusResponse.data.result;
        } else if (status === 'FAILED') {
          throw new Error(statusResponse.data.error);
        }
      }
    })
  );

  console.log('All tasks completed:', results);
})();