Overview
The Tasks API enables you to create, version, and execute reusable browser automation code in your Anchor Browser sessions. Tasks allow you to:
- Write once, run anywhere: Create reusable automation scripts that can be executed across multiple sessions
- Execution tracking: Monitor task execution history, performance, and results
- Input: Pass dynamic inputs to your tasks for flexible execution
- Output: Return exactly what you need: status, messages, and domain data tailored to your use case.
Tasks are executed in a secure sandbox environment and can access the full Anchor Browser API for automation capabilities.
Creating Your First Task
Writing Task Code
For reliable execution, follow these guidelines:
- Write your code in TypeScript.
- Export a single default async function.
- In that function, return whatever your workflow requires as output (e.g., status, messages, domain data).
Tasks can receive values as inputs. All input names must be prefixed with ANCHOR_
Basic Task Example
import AnchorClient from 'anchorbrowser';
// Initialize the Anchor client with your API key
const anchorClient = new AnchorClient({
apiKey: process.env.ANCHOR_API_KEY,
});
// Export the main function as the default export
export default async function run() {
// Create a new browser instance
const browser = await anchorClient.browser.create();
const page = browser.contexts()[0].pages()[0];
// Access input values
const targetUrl = process.env.ANCHOR_TARGET_URL;
const maxPages = parseInt(process.env.ANCHOR_MAX_PAGES || '10');
// Implement your automation logic
await page.goto(targetUrl);
console.log(`Scraping up to ${maxPages} pages from ${targetUrl}`);
// Always close the browser when done
await browser.close();
// Return a result object with success status and message
return {
success: true,
message: 'Task completed successfully'
};
}
Using the API
Create a new task using the AnchorBrowser API:
curl -X POST https://api.anchorbrowser.io/v1/task \
-H "anchor-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "web-scraper",
"language": "typescript",
"description": "A task to scrape product information from e-commerce sites",
"code": "Y29uc3QgYW5jaG9yID0gcmVxdWlyZSgnYW5jaG9yYnJvd3NlcicpOwoKYXN5bmMgZnVuY3Rpb24gcnVuKCkgewogIGNvbnN0IHNlc3Npb24gPSBhd2FpdCBhbmNob3IuY3JlYXRlU2Vzc2lvbigpOwogIGF3YWl0IHNlc3Npb24uZ29UbygnaHR0cHM6Ly9leGFtcGxlLmNvbScpOwogIGNvbnN0IHRpdGxlID0gYXdhaXQgc2Vzc2lvbi5nZXRUaXRsZSgpOwogIGNvbnNvbGUubG9nKHRpdGxlKTsKICBhd2FpdCBzZXNzaW9uLmNsb3NlKCk7Cn0KcnVuKCk7"
}'
Response:
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"name": "web-scraper",
"teamId": "team-123",
"description": "A task to scrape product information from e-commerce sites",
"latestVersion": "draft",
"code": "Y29uc3QgYW5jaG9yID0gcmVxdWlyZSgnYW5jaG9yYnJvd3NlcicpOwoKYXN5bmMgZnVuY3Rpb24gcnVuKCkgewogIGNvbnN0IHNlc3Npb24gPSBhd2FpdCBhbmNob3IuY3JlYXRlU2Vzc2lvbigpOwogIGF3YWl0IHNlc3Npb24uZ29UbygnaHR0cHM6Ly9leGFtcGxlLmNvbScpOwogIGNvbnN0IHRpdGxlID0gYXdhaXQgc2Vzc2lvbi5nZXRUaXRsZSgpOwogIGNvbnNvbGUubG9nKHRpdGxlKTsKICBhd2FpdCBzZXNzaW9uLmNsb3NlKCk7Cn0KcnVuKCk7",
"language": "typescript",
"deleted": false,
"createdAt": "2024-01-01T00:00:00.000Z",
"updatedAt": "2024-01-01T00:00:00.000Z"
}
2. Run the Task
Run draft / deploed version with the relevant inputs:
curl -X POST https://api.anchorbrowser.io/v1/task/run \
-H "anchor-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"taskId": "550e8400-e29b-41d4-a716-446655440000",
"version": "draft",
"inputs": {
"ANCHOR_TARGET_URL": "https://example.com",
"ANCHOR_MAX_PAGES": "10"
}
}'
3. Deploy to Anchor Production
Once tested, deploy the task to create a new published version:
curl -X POST https://api.anchorbrowser.io/v1/task/550e8400-e29b-41d4-a716-446655440000/deploy \
-H "anchor-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"code": "Y29uc3QgYW5jaG9yID0gcmVxdWlyZSgnYW5jaG9yYnJvd3NlcicpOwoKYXN5bmMgZnVuY3Rpb24gcnVuKCkgewogIGNvbnN0IHNlc3Npb24gPSBhd2FpdCBhbmNob3IuY3JlYXRlU2Vzc2lvbigpOwogIGF3YWl0IHNlc3Npb24uZ29UbygnaHR0cHM6Ly9leGFtcGxlLmNvbScpOwogIGNvbnN0IHRpdGxlID0gYXdhaXQgc2Vzc2lvbi5nZXRUaXRsZSgpOwogIGNvbnNvbGUubG9nKHRpdGxlKTsKICBhd2FpdCBzZXNzaW9uLmNsb3NlKCk7Cn0KcnVuKCk7"
}'
Support
For additional help with the Tasks API: