This page is a quick start using code. If you want a quick start using an Integration (CrewAI, Make, Langchain, etc.) click here.
1

Fetch API Key

In Anchor UI, copy your API key
2

Install playwright

npm i playwright-core
3

Run sample code

If you don’t have access to code running, consider using the live playgroundThe following example demonstrates how to start the browser and navigate to a website
const { chromium } = require("playwright-core");

(async () => {
    const ANCHOR_API_KEY = process.env.ANCHOR_API_KEY;

    const browser = await chromium.connectOverCDP(`wss://connect.anchorbrowser.io?apiKey=${ANCHOR_API_KEY}`);
    const page = await browser.newPage();

    // Navigate to Anchor Browser's website
    await page.goto("https://anchorbrowser.io");
    console.log("Page title:", await page.title());

    await browser.close();
})().catch(console.error);
4

Alter the browser configuration

Anchor Browser supports different configurations of the browser session (see API reference for all options). Some of the most common configurations are:To use a browser with a specific configuration, first create a browser session with the desired configuration.
const axios = require("axios");

(async () => {
    const ANCHOR_API_KEY = process.env.ANCHOR_API_KEY;
    const browserConfiguration = {
    session: {
        // Proxy configuration
    proxy: {
        active: true,
        type: "anchor_residential",
        country_code: "it"
    },
    // Session lifetime management
        max_duration: 1, // 1 minute
        idle_timeout: 1   // 1 minute
    }
    };

    const response = await axios.post("https://api.anchorbrowser.io/v1/sessions", browserConfiguration, {
    headers: {
        "anchor-api-key": ANCHOR_API_KEY,
        "Content-Type": "application/json",
    },
    });

    const session = response.data.data;
    console.log("Session created:", session.id); // Keep this ID for later use
})().catch(console.error);
5

Session generation and reconnection

You can create sessions and reconnect to them later using the session ID. This is useful for maintaining state across multiple browser sessions.
const { chromium } = require("playwright-core");
const ANCHOR_API_KEY = process.env.ANCHOR_API_KEY;

(async () => {
const browser = await chromium.connectOverCDP(`wss://connect.anchorbrowser.io?apiKey=${ANCHOR_API_KEY}&sessionId=${sessionId}`);
const page = await browser.newPage();

// Check the IP address
await page.goto("https://www.whatismyip.com/");
await page.waitForTimeout(10000)
console.log(await page.textContent('#region-state'))

// Close browser but session remains active
await browser.close();
})().catch(console.error);