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

Fetch API Key

In Anchor UI, copy your API key
2

Install playwright

npm i playwright-core
3

Create a session via API

const axios = require("axios");

(async () => {
    const session = await axios.post("https://api.anchorbrowser.io/v1/sessions", {}, {
        headers: {
            "anchor-api-key": process.env.ANCHOR_API_KEY,
            "Content-Type": "application/json"
        }
    });
    const cdp_url = session.data.data.cdp_url;
    console.log("Session's CDP_URL for later use\n", cdp_url);
})().catch(console.error);
4

Run sample code

const { chromium } = require("playwright-core");

(async () => {
    // Connect to the session
    const browser = await chromium.connectOverCDP(cdp_url);
    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);
5

Advanced 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 browserConfiguration = {
        session: {
            "recording": { "active": false }, // Default is true
            // Proxy configuration
            proxy: {
                active: true,
                type: "anchor_residential",
                country_code: "it"
            },
            // Session lifetime management
            "timeout": {
                "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": process.env.ANCHOR_API_KEY,
            "Content-Type": "application/json",
        },
    });

    const session = response.data.data;
    const cdp_url = session.cdp_url;
    console.log("Session's CDP_URL for later use\n", cdp_url);  // Keep this cdp_url for next step
})().catch(console.error);
6

Session reconnection

Reconnect to an existing session using the session CDP Url.Validate the session created in the previous step is still active by checking the IP address.
const { chromium } = require("playwright-core");

(async () => {
    const browser = await chromium.connectOverCDP(cdp_url);
    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);