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);
from anchorbrowser import Anchorbrowser
import os

anchor_client = Anchorbrowser(api_key=os.environ.get("ANCHORBROWSER_API_KEY"))

# Start async task
response = anchor_client.tools.perform_web_task(
    prompt='Extract the main heading and first paragraph from the page',
    url='https://docs.anchorbrowser.io',
    async_=True
)

print('Workflow ID:', response.data.workflow_id)
print('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);
}
status_response = anchor_client.tools.get_perform_web_task_status(workflow_id)

# Status can be: 'RUNNING', 'COMPLETED', or 'FAILED'
if status_response['data']['status'] == 'RUNNING':
    print('Still running...')
elif status_response['data']['status'] == 'COMPLETED':
    print('Result:', status_response['data']['result'])
elif status_response['data']['status'] == 'FAILED':
    print('Error:', status_response['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);
})();
from anchorbrowser import Anchorbrowser
import os
import time
import requests

anchor_client = Anchorbrowser(api_key=os.environ.get("ANCHORBROWSER_API_KEY"))

# Start multiple async tasks in parallel
responses = [
    anchor_client.tools.perform_web_task(
        prompt='Extract the main heading',
        url='https://docs.anchorbrowser.io',
        async_=True
    ),
    anchor_client.tools.perform_web_task(
        prompt='Get the first paragraph',
        url='https://example.com',
        async_=True
    )
]

workflow_ids = [response.data.workflow_id for response in responses]
print('Started', len(workflow_ids), 'tasks')

# Poll all tasks
results = []
for workflow_id in workflow_ids:
    status = 'RUNNING'
    while status == 'RUNNING':
        time.sleep(2)

        status_response = anchor_client.tools.get_perform_web_task_status(workflow_id)
        status = status_response['data']['status']

        if status == 'COMPLETED':
            results.append(status_response['data']['result'])
            break
        elif status == 'FAILED':
            raise Exception(status_response['data']['error'])

print('All tasks completed:', results)