> ## Documentation Index
> Fetch the complete documentation index at: https://docs.anchorbrowser.io/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Anchor Browser is cloud browser infrastructure for AI agents and automation: browser sessions you control over CDP or REST, natural-language web tasks, versioned automation tasks, managed authentication into third-party web apps (identities with MFA), persistent volumes and profiles, recordings, live view, batch sessions, webhooks, and network options including residential proxies, extra stealth, and captcha solving for sites that block standard browsers. AI agents should start at https://docs.anchorbrowser.io/for-agents, then get an API key with Agent Access at https://docs.anchorbrowser.io/quickstart/agent-access — no dashboard account required. Read those two pages before authentication or API calls.

# E2B

> Run agent code in an E2B sandbox and connect it to an Anchor cloud browser.

[E2B](https://e2b.dev) sandboxes are isolated cloud VMs for running agent or automation code.

## What you're integrating

**Anchor SDK in E2B's base image → Anchor Cloud.** The sandbox stays vanilla and lightweight. Chromium runs as a managed Anchor cloud session; the sandbox just holds the SDK and talks to it over HTTPS + CDP.

* E2B stays the code runtime — install packages, run a script, call a model.
* Anchor stays the remote browser — stealth, proxies, live view, and session lifecycle.
* Do not launch a browser inside the sandbox. Skip E2B [Computer use](https://docs.e2b.dev/use-cases/computer-use) (desktop / VNC) and in-sandbox Chromium — live view for this integration is Anchor's remote session.

## Create the sandbox

Pass keys at create time on `Sandbox.create`. Template `setEnvs` / `set_envs` is build-time only — secrets and the SDK alias belong on create.

```javascript Node.js theme={null}
import { Sandbox } from 'e2b';

const sandbox = await Sandbox.create({
  envs: {
    ANCHOR_API_KEY: process.env.ANCHOR_API_KEY,
    ANCHORBROWSER_API_KEY: process.env.ANCHOR_API_KEY,
  },
  timeoutMs: 300_000,
});
```

```python Python theme={null}
import os
from e2b import Sandbox

sandbox = Sandbox.create(
    envs={
        'ANCHOR_API_KEY': os.environ['ANCHOR_API_KEY'],
        'ANCHORBROWSER_API_KEY': os.environ['ANCHOR_API_KEY'],
    },
    timeout=300,
)
```

JavaScript `timeoutMs` is milliseconds. Python `timeout` is seconds. That value is the **E2B sandbox** lifetime (Hobby max 1 hour, Pro max 24 hours) — it is separate from Anchor's session `timeout.max_duration` / `timeout.idle_timeout`.

Run the snippets below inside the sandbox. From the host you can write them in with `files.write` and `commands.run`.

## Preinstall the SDK

Install into the image app directory — not globally. `npm install -g` is not importable from user projects.

```bash Python theme={null}
pip install "anchorbrowser>=1.0" playwright
```

```bash Node.js theme={null}
npm install anchorbrowser playwright-core
```

`playwright` / `playwright-core` are only needed for direct CDP control, not for `agent.task` / `agentTask`.

Alias the env var the raw SDK actually reads, so code that does not call `setConfig` / `api_key=` still works:

```bash theme={null}
ANCHORBROWSER_API_KEY=${ANCHOR_API_KEY}
```

Put that on `Sandbox.create` `envs` (as above) or in the sandbox entrypoint. Without this alias, session and task calls fail auth (401) even when `ANCHOR_API_KEY` is set.

There is no public Anchor E2B template. Bake `anchorbrowser` (and Playwright only if you need CDP) into your own tag so every sandbox starts ready:

```javascript Node.js theme={null}
import { Template } from 'e2b';

export const template = Template()
  .fromBaseImage()
  .pipInstall(['anchorbrowser>=1.0', 'playwright'])
  .npmInstall(['anchorbrowser', 'playwright-core']);
```

```python Python theme={null}
from e2b import Template

template = (
    Template()
    .from_base_image()
    .pip_install(['anchorbrowser>=1.0', 'playwright'])
    .npm_install(['anchorbrowser', 'playwright-core'])
)
```

Build that template with the E2B SDK or CLI, then create from the tag (`Sandbox.create('your-tag', { envs, timeoutMs })`). Runtime `pip install` / `npm install` works if you skip a template, but it adds 30–60s to every sandbox.

## API key (BYOK)

Each E2B customer brings their own Anchor API key as a sandbox env: `ANCHOR_API_KEY=sk-...`.

Anchor bills the customer directly on usage — E2B does not meter or bill browser usage.

> [!NOTE]
>
> **Keyless trial (optional):** agents without a key can self-provision a small trial key via [Agent Access](/quickstart/agent-access) — guide → puzzle challenge (120s TTL) → submit the answer for an `api_key`, sent as header `anchor-api-key`. Trial keys are capped (1 credit, 60-minute session cap). Fine for exploration, not a BYOK replacement. Do not market this as unlimited browser access.

## Expose a browser tool

Wrap the SDK so agents call it declaratively. `agent.task` (Python) / `agentTask` (Node) return an object; the result string is `result.data.result`.

```javascript Node.js theme={null}
import { client, agentTask } from 'anchorbrowser';

client.setConfig({ auth: () => process.env.ANCHORBROWSER_API_KEY });

const result = await agentTask(
  'Go to example.com and return the page title'
);

console.log(result.data.result);
```

```python Python theme={null}
import os
from anchorbrowser import Anchorbrowser

anchor_client = Anchorbrowser(api_key=os.getenv('ANCHORBROWSER_API_KEY'))

result = anchor_client.agent.task(
    'Go to example.com and return the page title'
)

print(result.data.result)
```

### Optional: full Playwright control

Connect over CDP to the created cloud session (`session.data.cdp_url`).

> [!WARNING]
>
> That URL carries the API key in the query string — **never log it**. Surface `session.data.live_view_url` to humans instead (`https://live.anchorbrowser.io/inspector.html?sessionId=...`). Closing Playwright only disconnects — it does not end the cloud session.

```javascript Playwright theme={null}
import { chromium } from 'playwright-core';
import { client, Sessions } from 'anchorbrowser';

client.setConfig({ auth: () => process.env.ANCHORBROWSER_API_KEY });

const session = await Sessions.createSession({
  body: {
    session: {
      timeout: { max_duration: 30, idle_timeout: 10 },
    },
  },
});

const browser = await chromium.connectOverCDP(session.data.cdp_url);
const page = browser.contexts()[0].pages()[0] ?? await browser.newPage();
await page.goto('https://example.com');
console.log('Title:', await page.title());
await browser.close();

await Sessions.deleteSession({ path: { session_id: session.data.id } });
```

```python Playwright theme={null}
import os
from anchorbrowser import Anchorbrowser
from playwright.sync_api import sync_playwright

anchor_client = Anchorbrowser(api_key=os.getenv('ANCHORBROWSER_API_KEY'))
session = anchor_client.sessions.create_session(
    session={'timeout': {'max_duration': 30, 'idle_timeout': 10}}
)

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp(session.data.cdp_url)
    page = browser.contexts[0].pages[0] if browser.contexts[0].pages else browser.new_page()
    page.goto('https://example.com')
    print('Title:', page.title())
    browser.close()

anchor_client.sessions.delete_session(session.data.id)
```

If the remote browser needs to visit an app running **inside** the sandbox, expose the port with `getHost` / `get_host` and open that public HTTPS URL. The cloud browser cannot reach `localhost` inside the VM.

## Session hygiene

You have two lifecycles to close:

* Reuse **one** Anchor session per agent run — do not create a session per page.
* Set Anchor `timeout.max_duration` / `timeout.idle_timeout` (minutes) so an abandoned sandbox does not hold a browser open. See [Session Timeout](/advanced/session-timeout).
* Delete the Anchor session on teardown (`delete_session` / `deleteSession`). `sandbox.kill()` stops the VM only — it does not end the cloud browser.
* Set the E2B sandbox timeout (`timeoutMs` / `timeout`) and call `sandbox.kill()` when the run finishes.
* Show `live_view_url` in the sandbox UI so users can watch the browser run. That stream is the remote Anchor session, not E2B's desktop / VNC view.

## Egress

If E2B sandboxes restrict egress, allowlist:

* `https://api.anchorbrowser.io`
* `wss://connect.anchorbrowser.io`
* `https://live.anchorbrowser.io`

See the [Sandboxes overview](/integrations/sandboxes) and [Create a Session](/quickstart/create-session).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.