This page is a quick start using our official SDK. If you want a quick start using raw code with Playwright, click here.For complete documentation and advanced usage examples, visit our package repositories:
Set up the Anchor Browser client with your API key:
Copy
Ask AI
import { AnchorClient } from "anchorbrowser";const anchorClient = new AnchorClient({ apiKey: process.env.ANCHOR_API_KEY,});
4
AI Agent Browser Automation
Use AI agents to automate browser tasks with natural language commands:
Copy
Ask AI
// Simple navigation taskconst result = await anchorClient.agent.task( "go to news.ycombinator.com and get the title of the first story");console.log("Task result:", result);// Task with execution step monitoringconst executionStepLogs = [];const navigationResult = await anchorClient.agent.task( "go to news.ycombinator.com and get the title of the first story", { taskOptions: { onAgentStep: (executionStep) => { console.log("Agent step:", executionStep); }, }, });
5
Structured Data Extraction
Extract structured data from webpages using schemas:
Copy
Ask AI
import { z } from "zod";// Define your data schemaconst extractionSchema = z.object({ title: z.string(), description: z.string(), price: z.string().optional(),});// Extract structured data from product pageconst structuredResult = await anchorClient.agent.task( "Extract the product title, description, and price from this Amazon product page", { taskOptions: { outputSchema: z.toJSONSchema(extractionSchema), url: "https://www.amazon.com/dp/B0D7D9N7X3", }, });// Validate the resultconst validatedData = extractionSchema.safeParse(structuredResult.result);if (validatedData.success) { console.log("Product title:", validatedData.data.title); console.log("Description:", validatedData.data.description); console.log("Price:", validatedData.data.price);}
6
Browser Session Management
Create and manage browser sessions with full Playwright integration:
Copy
Ask AI
// Method 1: Browser task with session controlconst browserTask = await anchorClient.agent.browserTask( "go to github.com/trending and find the most popular JavaScript repository");console.log("Session ID:", browserTask.sessionId);// Access the Playwright browser instanceconst playwrightBrowser = browserTask.playwrightBrowser;const page = playwrightBrowser.contexts()[0].pages()[0];// Direct Playwright manipulationawait page.goto("https://stackoverflow.com/");console.log("Current URL:", page.url());// Wait for task completionconst taskResult = await browserTask.taskResultPromise;console.log("Final result:", taskResult);// Method 2: Create session and connectconst sessionResponse = await anchorClient.sessions.create();const sessionId = sessionResponse.data.id;const browser = await anchorClient.browser.connect(sessionId);const context = browser.contexts()[0];const newPage = context.pages()[0];await newPage.goto("https://reddit.com/r/programming");// Method 3: Create standalone browserconst standaloneBrowser = await anchorClient.browser.create();const standalonePage = standaloneBrowser.contexts()[0].pages()[0];await standalonePage.goto("https://httpbin.org/ip");
7
Screenshots and Tools
Capture screenshots and use additional tools:
Copy
Ask AI
// Capture screenshot from a specific sessionconst screenshot = await anchorClient.tools.screenshotWebpage({ sessionId: browserTask.sessionId,});// Convert to blob and saveconst screenshotBlob = await screenshot.blob();console.log("Screenshot type:", screenshotBlob.type); // image/png// You can also save the screenshotconst buffer = await screenshotBlob.arrayBuffer();// Save buffer to file system as needed
8
Advanced Configuration
Configure browser sessions with proxies, timeouts, and other options: