Skip to main content

Overview

A common pattern is to wrap the perform-web-task endpoint inside a Task for async execution. This allows you to run AI-powered web tasks asynchronously and poll for results later.
This pattern is useful when you need to run multiple perform-web-task operations in parallel or when you want to decouple task execution from result retrieval.

Example

The following example creates a task that calls perform-web-task, deploys it, and runs it asynchronously:
import axios from 'axios';

(async () => {
  const asyncPerformWebTask = async ({ name, prompt, url, sessionId }) => {
    const apiKey = process.env.ANCHOR_API_KEY;

    const taskCode = `export default async function run() {
      const res = await fetch('https://api.anchorbrowser.io/v1/tools/perform-web-task' + (process.env.ANCHOR_SESSION_ID ? '?sessionId=' + process.env.ANCHOR_SESSION_ID : ''), {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'anchor-api-key': process.env.ANCHOR_API_KEY },
        body: JSON.stringify({ prompt: process.env.ANCHOR_PROMPT, url: process.env.ANCHOR_URL }),
      });
      const data = await res.json();
      return { success: true, result: data.data?.result || data.data || data };
    }`;

    // Create the task
    const createRes = await axios.post('https://api.anchorbrowser.io/v1/task', 
      { name, language: 'typescript' }, 
      { headers: { 'anchor-api-key': apiKey } }
    );
    const taskId = createRes.data.data.id;

    // Deploy the task with the code
    const deployRes = await axios.post(`https://api.anchorbrowser.io/v1/task/${taskId}/deploy`, {
      code: Buffer.from(taskCode).toString('base64'),
    }, { headers: { 'anchor-api-key': apiKey } });

    // Run the task asynchronously
    const runRes = await axios.post('https://api.anchorbrowser.io/v1/task/run', {
      taskId,
      version: deployRes.data.data.version,
      async: true,
      inputs: { 
        ANCHOR_API_KEY: apiKey, 
        ANCHOR_PROMPT: prompt, 
        ...(sessionId && { ANCHOR_SESSION_ID: sessionId }), 
        ...(url && { ANCHOR_URL: url }) 
      },
    }, { headers: { 'anchor-api-key': apiKey } });

    return runRes.data.data;
  };

  const getTaskResult = async (taskId, executionResultId) => {
    const res = await axios.get(`https://api.anchorbrowser.io/v1/task/${taskId}/executions/${executionResultId}`, {
      headers: { 'anchor-api-key': process.env.ANCHOR_API_KEY },
    });
    return res.data;
  };

  // Start the async task
  const taskData = await asyncPerformWebTask({
    name: "get-first-paragraph",
    prompt: "Get the first paragraph of the page",
    url: "https://docs.anchorbrowser.io",
  });

  console.log("Task started:", taskData);

  // Wait for the task to complete (adjust timeout as needed)
  setTimeout(async () => {
    const taskResult = await getTaskResult(taskData.taskId, taskData.executionResultId);
    console.log("Task Result:\n", taskResult);
  }, 90000);
})();
Async task executions have a maximum duration of 3 hours. Tasks that exceed this limit will be automatically cancelled.