Overview
A common pattern is to wrap theperform-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 callsperform-web-task, deploys it, and runs it asynchronously:
Copy
Ask AI
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.
Show How It Works
Show How It Works
How It Works
- Create a Task: The
asyncPerformWebTaskfunction creates a new task that wraps theperform-web-taskAPI call - Deploy the Task: The task code is base64-encoded and deployed to Anchor
- Run Asynchronously: The task is executed with
async: true, returning immediately with task and execution IDs - Poll for Results: Use the
getTaskResultfunction to check the execution status and retrieve results
Parameters
TheasyncPerformWebTask function accepts the following parameters:| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Unique name for the task |
prompt | string | Yes | The prompt describing what the AI should do |
url | string | No | Starting URL for the task |
sessionId | string | No | Existing session ID to use instead of creating a new one |

