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:
1

Fetch API Key

In Anchor UI, copy your API key
2

Install the SDK

npm install anchorbrowser
3

Initialize the SDK

Set up the Anchor Browser client with your API key:
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:
// Simple navigation task
const 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 monitoring
const 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:
import { z } from "zod";

// Define your data schema
const extractionSchema = z.object({
  title: z.string(),
  description: z.string(),
  price: z.string().optional(),
});

// Extract structured data from product page
const 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 result
const 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:
// Method 1: Browser task with session control
const 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 instance
const playwrightBrowser = browserTask.playwrightBrowser;
const page = playwrightBrowser.contexts()[0].pages()[0];

// Direct Playwright manipulation
await page.goto("https://stackoverflow.com/");
console.log("Current URL:", page.url());

// Wait for task completion
const taskResult = await browserTask.taskResultPromise;
console.log("Final result:", taskResult);

// Method 2: Create session and connect
const 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 browser
const 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:
// Capture screenshot from a specific session
const screenshot = await anchorClient.tools.screenshotWebpage({
  sessionId: browserTask.sessionId,
});

// Convert to blob and save
const screenshotBlob = await screenshot.blob();
console.log("Screenshot type:", screenshotBlob.type); // image/png

// You can also save the screenshot
const buffer = await screenshotBlob.arrayBuffer();
// Save buffer to file system as needed
8

Advanced Configuration

Configure browser sessions with proxies, timeouts, and other options:
// Create session with advanced configuration
const sessionConfig = {
  session: {
    recording: false, // Disable session recording
    proxy: {
      active: true,
      type: "anchor_residential",
      country_code: "us",
    },
    max_duration: 5, // 5 minutes
    idle_timeout: 1, // 1 minute
  },
};

const configuredSession = await anchorClient.sessions.create(sessionConfig);
const configuredBrowser = await anchorClient.browser.connect(
  configuredSession.data.id
);

// Use the configured browser to check IP location
const page = configuredBrowser.contexts()[0].pages()[0];
await page.goto("https://httpbin.org/ip");
const ipInfo = await page.textContent("pre");
console.log("IP Info:", ipInfo);

Key SDK Benefits

The Anchor Browser SDK provides several advantages over direct API usage:
  • AI Agent Integration: Use natural language to automate complex browser tasks
  • Structured Data Extraction: Define schemas and extract data in a predictable format
  • Seamless Playwright Integration: Full access to Playwright’s powerful browser automation capabilities
  • Session Management: Easy creation and management of persistent browser sessions
  • Built-in Tools: Screenshot capture, proxy management, and more
  • Type Safety: Full TypeScript support with proper type definitions

Next Steps