From 44e377346b52d4caf04ac260771ef492d70fa9df Mon Sep 17 00:00:00 2001 From: Tomas Beran Date: Tue, 17 Mar 2026 01:24:07 +0100 Subject: [PATCH 01/16] docs: Add app preview example section --- docs.json | 6 + docs/examples/app-preview.mdx | 283 ++++++++++++++++++++++++++++++++++ 2 files changed, 289 insertions(+) create mode 100644 docs/examples/app-preview.mdx diff --git a/docs.json b/docs.json index 41ff2fea..84825ac0 100644 --- a/docs.json +++ b/docs.json @@ -50,6 +50,12 @@ "docs/use-cases/ci-cd" ] }, + { + "group": "Examples", + "pages": [ + "docs/examples/app-preview" + ] + }, { "group": "Agents in sandbox", "pages": [ diff --git a/docs/examples/app-preview.mdx b/docs/examples/app-preview.mdx new file mode 100644 index 00000000..952b12a1 --- /dev/null +++ b/docs/examples/app-preview.mdx @@ -0,0 +1,283 @@ +--- +title: "App preview with Kernel" +description: "Deploy a web app in an E2B sandbox and use a Kernel cloud browser to screenshot every route and generate a preview report." +icon: "camera" +--- + +Deploy a web application inside an E2B sandbox, get a public URL, then use a [Kernel](https://www.kernel.computer/) cloud browser to visit every route, capture screenshots, and produce a structured preview report — all without running a browser locally. + +## Architecture + +This example combines two services: + +1. **E2B Sandbox** — a secure cloud environment where the web app runs. E2B exposes the app at a public HTTPS URL via `sandbox.get_host(port)`. +2. **Kernel Cloud Browser** — a remote Chromium instance controlled through Playwright's CDP protocol. It navigates the public URL, takes screenshots, and collects page metadata. + +The orchestrator script on your machine creates the sandbox, deploys the app, and then runs a browsing script _inside_ the sandbox that connects to a Kernel browser and screenshots each route. + +## Prerequisites + +- An [E2B API key](https://e2b.dev/dashboard?tab=keys) +- A [Kernel API key](https://www.kernel.computer/) +- Python 3.10+ + +```bash +pip install e2b-code-interpreter +``` + +Set both keys in your environment: + +```bash .env +E2B_API_KEY=e2b_*** +KERNEL_API_KEY=kernel_*** +``` + +## How it works + + + +Start an E2B sandbox using the `kernel-browser` template, which comes with the Kernel SDK and Playwright client pre-installed. No local browser binary is needed — Kernel provides the browser remotely. + +```python +from e2b_code_interpreter import Sandbox + +sandbox = Sandbox.create( + "kernel-browser", + envs={"KERNEL_API_KEY": os.environ["KERNEL_API_KEY"]}, + timeout=300, +) +``` + + + +Write a FastAPI application into the sandbox and start it as a background process on port 8000. + +```python +sandbox.files.write("/home/user/app.py", FASTAPI_APP) +sandbox.commands.run( + "pip install --break-system-packages fastapi uvicorn", + timeout=60, +) +sandbox.commands.run( + "uvicorn app:app --host 0.0.0.0 --port 8000", + background=True, + cwd="/home/user", +) +``` + + + +E2B exposes any sandbox port as a public HTTPS endpoint. + +```python +host = sandbox.get_host(8000) +app_url = f"https://{host}" +``` + + + +A script running inside the sandbox creates a Kernel cloud browser, connects via Playwright CDP, and visits each route to take screenshots. + +```python +from kernel import Kernel +from playwright.sync_api import sync_playwright + +kernel = Kernel() +kb = kernel.browsers.create() + +with sync_playwright() as pw: + browser = pw.chromium.connect_over_cdp(kb.cdp_ws_url) + page = browser.new_page() + page.set_viewport_size({"width": 1280, "height": 720}) + + page.goto(app_url, wait_until="networkidle") + page.screenshot(path="/home/user/screenshots/home.png") +``` + + + +After visiting every route, the script writes a JSON report with page titles and screenshot paths. The orchestrator reads it back from the sandbox. + +```python +import json + +report = json.loads( + sandbox.files.read("/home/user/preview_report.json") +) +for entry in report: + print(f"Route: {entry['route']} — Title: {entry['title']}") +``` + + + +## Full example + +```python app_preview.py +""" +App Preview — E2B + Kernel + +Spins up a web app inside an E2B sandbox, exposes it at a public URL, +then uses a Kernel cloud browser to navigate the app, take screenshots +of each route, and generate a visual preview report. +""" + +import os +import time +import json + +from e2b_code_interpreter import Sandbox + +# A sample FastAPI app to deploy inside the sandbox +FASTAPI_APP = ''' +from fastapi import FastAPI +from fastapi.responses import HTMLResponse + +app = FastAPI() + +@app.get("/", response_class=HTMLResponse) +def home(): + return """ + + + My App +

Welcome to My App

+ + """ + +@app.get("/about", response_class=HTMLResponse) +def about(): + return """ + + + About +

About

E2B + Kernel integration demo.

+ + """ + +@app.get("/api/status") +def status(): + return {"status": "ok", "sandbox": "e2b", "browser": "kernel"} +''' + +# Script that runs inside the sandbox to browse the app with Kernel +BROWSE_SCRIPT = ''' +import sys +import json +from kernel import Kernel +from playwright.sync_api import sync_playwright + +app_url = sys.argv[1] +routes = ["/", "/about"] + +kernel = Kernel() +kb = kernel.browsers.create() + +results = [] + +with sync_playwright() as pw: + browser = pw.chromium.connect_over_cdp(kb.cdp_ws_url) + page = browser.new_page() + page.set_viewport_size({"width": 1280, "height": 720}) + + for route in routes: + url = f"{app_url}{route}" + page.goto(url, wait_until="networkidle", timeout=15000) + + # Take screenshot + safe_name = route.strip("/").replace("/", "_") or "home" + screenshot_path = f"/home/user/screenshots/{safe_name}.png" + page.screenshot(path=screenshot_path) + + # Collect page info + results.append({ + "route": route, + "title": page.title(), + "screenshot": screenshot_path, + }) + + browser.close() + +with open("/home/user/preview_report.json", "w") as f: + json.dump(results, f) +''' + + +def main(): + sandbox = Sandbox.create( + "kernel-browser", + envs={"KERNEL_API_KEY": os.environ["KERNEL_API_KEY"]}, + timeout=300, + ) + + try: + # Deploy and start the FastAPI app + sandbox.files.write("/home/user/app.py", FASTAPI_APP) + sandbox.commands.run("mkdir -p /home/user/screenshots", timeout=5) + sandbox.commands.run( + "pip install --break-system-packages fastapi uvicorn", + timeout=60, + ) + sandbox.commands.run( + "uvicorn app:app --host 0.0.0.0 --port 8000", + background=True, + cwd="/home/user", + ) + time.sleep(3) + + # Get the public URL + host = sandbox.get_host(8000) + app_url = f"https://{host}" + print(f"App is live at: {app_url}") + + # Use Kernel browser to preview the app + sandbox.files.write("/home/user/browse.py", BROWSE_SCRIPT) + result = sandbox.commands.run( + f'python3 /home/user/browse.py "{app_url}"', + timeout=120, + ) + + # Read the preview report + report = json.loads( + sandbox.files.read("/home/user/preview_report.json") + ) + for entry in report: + print(f"Route: {entry['route']} — Title: {entry['title']}") + + finally: + sandbox.kill() + + +if __name__ == "__main__": + main() +``` + +## Key concepts + +| Concept | Detail | +|---|---| +| **E2B template** | `kernel-browser` — pre-built with the Kernel SDK and Playwright client | +| **Public URL** | `sandbox.get_host(port)` returns an HTTPS hostname | +| **Background process** | `sandbox.commands.run(..., background=True)` starts the web server without blocking | +| **Kernel browser** | `kernel.browsers.create()` spins up a remote Chromium; connect via `kb.cdp_ws_url` | +| **CDP connection** | Playwright's `connect_over_cdp()` drives the remote browser with full API access | + +## Adapting this example + +- **Your own app** — replace `FASTAPI_APP` with any web framework (Next.js, Flask, Express). Adjust the install commands and start script accordingly. +- **More routes** — add paths to the `routes` list in `BROWSE_SCRIPT` to screenshot additional pages. +- **Visual regression** — compare screenshots across deploys to catch UI regressions automatically. +- **CI integration** — run this as a post-deploy step to generate preview links and thumbnails for pull requests. + +## Related guides + + + + Create, manage, and control sandbox lifecycle + + + Run terminal commands inside the sandbox + + + Build AI agents that control virtual desktops + + From 0e007e9ec2ce1af7ca8340bc8d1f5ca14f6fc40e Mon Sep 17 00:00:00 2001 From: Tomas Beran Date: Tue, 17 Mar 2026 01:50:08 +0100 Subject: [PATCH 02/16] fix: Use e2b base SDK instead of e2b-code-interpreter --- docs/examples/app-preview.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/examples/app-preview.mdx b/docs/examples/app-preview.mdx index 952b12a1..2da47ce0 100644 --- a/docs/examples/app-preview.mdx +++ b/docs/examples/app-preview.mdx @@ -22,7 +22,7 @@ The orchestrator script on your machine creates the sandbox, deploys the app, an - Python 3.10+ ```bash -pip install e2b-code-interpreter +pip install e2b ``` Set both keys in your environment: @@ -39,7 +39,7 @@ KERNEL_API_KEY=kernel_*** Start an E2B sandbox using the `kernel-browser` template, which comes with the Kernel SDK and Playwright client pre-installed. No local browser binary is needed — Kernel provides the browser remotely. ```python -from e2b_code_interpreter import Sandbox +from e2b import Sandbox sandbox = Sandbox.create( "kernel-browser", @@ -125,7 +125,7 @@ import os import time import json -from e2b_code_interpreter import Sandbox +from e2b import Sandbox # A sample FastAPI app to deploy inside the sandbox FASTAPI_APP = ''' From fcb34cee1b0ef5281bd0c8976c15e5a4074c7850 Mon Sep 17 00:00:00 2001 From: Tomas Beran Date: Tue, 17 Mar 2026 15:01:55 +0100 Subject: [PATCH 03/16] docs: Wrap full example in accordion component --- docs/examples/app-preview.mdx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/examples/app-preview.mdx b/docs/examples/app-preview.mdx index 2da47ce0..084c59a8 100644 --- a/docs/examples/app-preview.mdx +++ b/docs/examples/app-preview.mdx @@ -112,6 +112,7 @@ for entry in report: ## Full example + ```python app_preview.py """ App Preview — E2B + Kernel @@ -250,6 +251,7 @@ def main(): if __name__ == "__main__": main() ``` + ## Key concepts From f8e84015f741b996f708869567639b3a07a72adc Mon Sep 17 00:00:00 2001 From: Tomas Beran Date: Tue, 17 Mar 2026 15:31:47 +0100 Subject: [PATCH 04/16] docs: Use expandable code block instead of accordion for full example --- docs/examples/app-preview.mdx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/examples/app-preview.mdx b/docs/examples/app-preview.mdx index 084c59a8..6d6e170b 100644 --- a/docs/examples/app-preview.mdx +++ b/docs/examples/app-preview.mdx @@ -112,8 +112,7 @@ for entry in report: ## Full example - -```python app_preview.py +```python app_preview.py expandable """ App Preview — E2B + Kernel @@ -251,7 +250,6 @@ def main(): if __name__ == "__main__": main() ``` - ## Key concepts From 4fab1f9bd4af06310bd1abe2700834a937ba188a Mon Sep 17 00:00:00 2001 From: Tomas Beran Date: Tue, 17 Mar 2026 15:36:06 +0100 Subject: [PATCH 05/16] docs: Move app preview to use-cases/browser-use --- docs.json | 9 ++------- .../app-preview.mdx => use-cases/browser-use.mdx} | 4 ++-- 2 files changed, 4 insertions(+), 9 deletions(-) rename docs/{examples/app-preview.mdx => use-cases/browser-use.mdx} (99%) diff --git a/docs.json b/docs.json index 84825ac0..b65bcd30 100644 --- a/docs.json +++ b/docs.json @@ -47,13 +47,8 @@ "pages": [ "docs/use-cases/coding-agents", "docs/use-cases/computer-use", - "docs/use-cases/ci-cd" - ] - }, - { - "group": "Examples", - "pages": [ - "docs/examples/app-preview" + "docs/use-cases/ci-cd", + "docs/use-cases/browser-use" ] }, { diff --git a/docs/examples/app-preview.mdx b/docs/use-cases/browser-use.mdx similarity index 99% rename from docs/examples/app-preview.mdx rename to docs/use-cases/browser-use.mdx index 6d6e170b..b8c3b14c 100644 --- a/docs/examples/app-preview.mdx +++ b/docs/use-cases/browser-use.mdx @@ -1,7 +1,7 @@ --- -title: "App preview with Kernel" +title: "Browser use" description: "Deploy a web app in an E2B sandbox and use a Kernel cloud browser to screenshot every route and generate a preview report." -icon: "camera" +icon: "browser" --- Deploy a web application inside an E2B sandbox, get a public URL, then use a [Kernel](https://www.kernel.computer/) cloud browser to visit every route, capture screenshots, and produce a structured preview report — all without running a browser locally. From 0bf90407b6b005e4466aac8864ffd5033b0a2dde Mon Sep 17 00:00:00 2001 From: Tomas Beran Date: Tue, 17 Mar 2026 16:18:47 +0100 Subject: [PATCH 06/16] docs: Use globe icon for browser use page --- docs/use-cases/browser-use.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/use-cases/browser-use.mdx b/docs/use-cases/browser-use.mdx index b8c3b14c..bd84fb96 100644 --- a/docs/use-cases/browser-use.mdx +++ b/docs/use-cases/browser-use.mdx @@ -1,7 +1,7 @@ --- title: "Browser use" description: "Deploy a web app in an E2B sandbox and use a Kernel cloud browser to screenshot every route and generate a preview report." -icon: "browser" +icon: "globe" --- Deploy a web application inside an E2B sandbox, get a public URL, then use a [Kernel](https://www.kernel.computer/) cloud browser to visit every route, capture screenshots, and produce a structured preview report — all without running a browser locally. From 47c28f843c337cd07615362e2e7c67b90e34fd11 Mon Sep 17 00:00:00 2001 From: Tomas Beran Date: Tue, 17 Mar 2026 19:16:49 +0100 Subject: [PATCH 07/16] docs: Add agent browser use case and reorganize browser use section --- docs.json | 9 +- docs/use-cases/agent-browser.mdx | 235 +++++++++++++++++++++++++++++++ docs/use-cases/browser-use.mdx | 2 +- 3 files changed, 244 insertions(+), 2 deletions(-) create mode 100644 docs/use-cases/agent-browser.mdx diff --git a/docs.json b/docs.json index b65bcd30..ae3c8c76 100644 --- a/docs.json +++ b/docs.json @@ -48,7 +48,14 @@ "docs/use-cases/coding-agents", "docs/use-cases/computer-use", "docs/use-cases/ci-cd", - "docs/use-cases/browser-use" + { + "group": "Browser use", + "icon": "globe", + "pages": [ + "docs/use-cases/browser-use", + "docs/use-cases/agent-browser" + ] + } ] }, { diff --git a/docs/use-cases/agent-browser.mdx b/docs/use-cases/agent-browser.mdx new file mode 100644 index 00000000..cc889810 --- /dev/null +++ b/docs/use-cases/agent-browser.mdx @@ -0,0 +1,235 @@ +--- +title: "Agent remote browser" +description: "Run an autonomous AI agent inside an E2B sandbox that browses the web using a Kernel cloud browser and the Browser Use framework." +icon: "robot" +--- + +Run an AI agent inside an E2B sandbox that autonomously controls a [Kernel](https://www.kernel.computer/) cloud browser. The agent decides what to click, type, and navigate — you just give it a task. + +This builds on the [remote browser](/docs/use-cases/browser-use) pattern by adding the [Browser Use](https://docs.browser-use.com/) framework, which turns an LLM into a browser-controlling agent. + +## Architecture + +1. **E2B Sandbox** — isolated environment where the agent code runs. Pre-installed with Kernel SDK, Playwright, and Browser Use. +2. **Kernel Cloud Browser** — remote Chromium instance the agent controls via CDP. +3. **Browser Use** — agent framework that connects an LLM to Playwright. The LLM sees screenshots and decides actions (click, type, scroll, navigate). + +The orchestrator creates the sandbox and kicks off the agent. The agent runs autonomously inside the sandbox — it creates a Kernel browser, connects Browser Use, and executes the task. + +## Prerequisites + +- An [E2B API key](https://e2b.dev/dashboard?tab=keys) +- A [Kernel API key](https://www.kernel.computer/) +- An LLM API key (Anthropic, OpenAI, or other [supported model](https://docs.browser-use.com/customize/supported-models)) +- Python 3.10+ + +```bash +pip install e2b-code-interpreter +``` + +Set your keys in the environment: + +```bash .env +E2B_API_KEY=e2b_*** +KERNEL_API_KEY=kernel_*** +ANTHROPIC_API_KEY=sk-ant-*** +``` + +## How it works + + + +Start an E2B sandbox using the `kernel-agent-browser` template, which comes with Kernel SDK, Playwright, and Browser Use pre-installed. Pass the API keys the agent will need. + +```python +from e2b_code_interpreter import Sandbox + +sandbox = Sandbox.create( + "kernel-agent-browser", + envs={ + "KERNEL_API_KEY": os.environ["KERNEL_API_KEY"], + "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"], + }, + timeout=300, +) +``` + + + +The agent script creates a Kernel browser, connects Browser Use to it, and runs a task autonomously. + +```python +AGENT_SCRIPT = ''' +import asyncio +from kernel import Kernel +from browser_use import Agent, Browser, ChatAnthropic + +async def main(): + kernel = Kernel() + kb = kernel.browsers.create() + + browser = Browser(cdp_url=kb.cdp_ws_url) + + agent = Agent( + task="Go to Hacker News, find the top 3 AI stories, and summarize them", + llm=ChatAnthropic(model="claude-sonnet-4-20250514"), + browser=browser, + ) + result = await agent.run() + print(result) + +asyncio.run(main()) +''' + +sandbox.files.write("/home/user/agent_task.py", AGENT_SCRIPT) +``` + + + +Execute the agent inside the sandbox. The agent will autonomously browse, click, type, and navigate to complete the task. + +```python +result = sandbox.commands.run( + "python3 /home/user/agent_task.py", + timeout=180, +) +print(result.stdout) +``` + + + +## Full example + +```python agent_browser.py expandable +""" +Agent Remote Browser — E2B + Kernel + Browser Use + +Spins up an E2B sandbox with Browser Use framework and Kernel cloud browser. +An AI agent autonomously browses the web to complete a research task. +""" + +import os + +from e2b_code_interpreter import Sandbox + +AGENT_SCRIPT = ''' +import asyncio +from kernel import Kernel +from browser_use import Agent, Browser, ChatAnthropic + +async def main(): + # Create a Kernel cloud browser + kernel = Kernel() + kb = kernel.browsers.create() + print(f"Kernel browser created: {kb.id}") + + # Connect Browser Use to the Kernel browser via CDP + browser = Browser(cdp_url=kb.cdp_ws_url) + + # Create an AI agent that autonomously browses + agent = Agent( + task=""" + Go to https://news.ycombinator.com and find the top 3 stories + that are about AI or machine learning. For each story: + 1. Note the title and point count + 2. Click through to the comments page + 3. Read the top comment + + Return a summary of your findings. + """, + llm=ChatAnthropic(model="claude-sonnet-4-20250514"), + browser=browser, + max_actions_per_step=4, + ) + + result = await agent.run() + print("\\n" + "=" * 60) + print("AGENT RESULT:") + print("=" * 60) + print(result) + +asyncio.run(main()) +''' + + +def main(): + sandbox = Sandbox.create( + "kernel-agent-browser", + envs={ + "KERNEL_API_KEY": os.environ["KERNEL_API_KEY"], + "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"], + }, + timeout=300, + ) + + try: + sandbox.files.write("/home/user/agent_task.py", AGENT_SCRIPT) + + result = sandbox.commands.run( + "python3 /home/user/agent_task.py", + timeout=180, + ) + + if result.exit_code != 0: + print(f"Agent failed: {result.stderr}") + else: + print(result.stdout) + + finally: + sandbox.kill() + + +if __name__ == "__main__": + main() +``` + +## Key concepts + +| Concept | Detail | +|---|---| +| **E2B template** | `kernel-agent-browser` — pre-built with Kernel SDK, Playwright, and Browser Use | +| **Kernel browser** | `kernel.browsers.create()` spins up a remote Chromium; connect via `kb.cdp_ws_url` | +| **Browser Use** | `Browser(cdp_url=...)` connects the agent framework to Kernel's CDP endpoint | +| **LLM choice** | Browser Use supports `ChatAnthropic`, `ChatOpenAI`, `ChatGoogle`, and more | +| **Autonomous agent** | The LLM sees the page (via screenshots) and decides what actions to take | + +## Choosing an LLM + +Browser Use supports multiple LLM providers. Import the one you need: + +```python +# Anthropic (recommended) +from browser_use import ChatAnthropic +llm = ChatAnthropic(model="claude-sonnet-4-20250514") + +# OpenAI +from browser_use import ChatOpenAI +llm = ChatOpenAI(model="gpt-4o") + +# Google +from browser_use import ChatGoogle +llm = ChatGoogle(model="gemini-2.5-flash") +``` + +Pass the corresponding API key in the sandbox `envs`. + +## Adapting this example + +- **Different tasks** — change the `task` string to any web research, form filling, or data extraction task. +- **Custom actions** — Browser Use supports [custom actions](https://docs.browser-use.com/customize/custom-actions) to extend agent capabilities. +- **Vision control** — set `use_vision="auto"` on the Agent to let it decide when to use screenshots vs DOM. +- **Multiple agents** — run several agents in parallel, each with their own Kernel browser, for concurrent research. + +## Related guides + + + + Programmatic browser automation with Playwright + Kernel + + + Build AI agents that control virtual desktops + + + Create, manage, and control sandbox lifecycle + + diff --git a/docs/use-cases/browser-use.mdx b/docs/use-cases/browser-use.mdx index bd84fb96..015cd9fc 100644 --- a/docs/use-cases/browser-use.mdx +++ b/docs/use-cases/browser-use.mdx @@ -1,5 +1,5 @@ --- -title: "Browser use" +title: "Remote browser" description: "Deploy a web app in an E2B sandbox and use a Kernel cloud browser to screenshot every route and generate a preview report." icon: "globe" --- From 61451bcf8e06faed5e8f8ebfe85e540db59cdd98 Mon Sep 17 00:00:00 2001 From: Tomas Beran Date: Tue, 24 Mar 2026 21:40:11 +0100 Subject: [PATCH 08/16] docs: Merge browser pages into single Kernel integration page Combines browser-use.mdx and agent-browser.mdx into one page at docs/integrations/kernel.mdx. Adds new Integrations section to sidebar. --- docs.json | 16 +- docs/integrations/kernel.mdx | 497 +++++++++++++++++++++++++++++++ docs/use-cases/agent-browser.mdx | 235 --------------- docs/use-cases/browser-use.mdx | 283 ------------------ 4 files changed, 504 insertions(+), 527 deletions(-) create mode 100644 docs/integrations/kernel.mdx delete mode 100644 docs/use-cases/agent-browser.mdx delete mode 100644 docs/use-cases/browser-use.mdx diff --git a/docs.json b/docs.json index ae3c8c76..c8b96be1 100644 --- a/docs.json +++ b/docs.json @@ -47,15 +47,13 @@ "pages": [ "docs/use-cases/coding-agents", "docs/use-cases/computer-use", - "docs/use-cases/ci-cd", - { - "group": "Browser use", - "icon": "globe", - "pages": [ - "docs/use-cases/browser-use", - "docs/use-cases/agent-browser" - ] - } + "docs/use-cases/ci-cd" + ] + }, + { + "group": "Integrations", + "pages": [ + "docs/integrations/kernel" ] }, { diff --git a/docs/integrations/kernel.mdx b/docs/integrations/kernel.mdx new file mode 100644 index 00000000..6e398ac8 --- /dev/null +++ b/docs/integrations/kernel.mdx @@ -0,0 +1,497 @@ +--- +title: "Kernel" +description: "Use Kernel cloud browsers with E2B sandboxes for web scraping, app previews, and autonomous browser agents." +icon: "globe" +--- + +[Kernel](https://www.kernel.computer/) provides cloud browsers — remote Chromium instances you can control programmatically via [CDP](https://chromedevtools.github.io/devtools-protocol/). Combined with E2B sandboxes, your agents can browse the web without running a browser locally. + +E2B provides pre-built sandbox templates with the Kernel SDK and Playwright already installed: + +| Template | Includes | Use case | +|---|---|---| +| `kernel-browser` | Kernel SDK, Playwright | Programmatic browsing, screenshots, scraping | +| `kernel-agent-browser` | Kernel SDK, Playwright, Browser Use | Autonomous AI agent browsing | + +## Prerequisites + +- An [E2B API key](https://e2b.dev/dashboard?tab=keys) +- A [Kernel API key](https://www.kernel.computer/) +- Python 3.10+ + +```bash +pip install e2b +``` + +Set your keys in the environment: + +```bash .env +E2B_API_KEY=e2b_*** +KERNEL_API_KEY=kernel_*** +``` + +## App preview with Kernel + +Deploy a web app inside a sandbox, get a public URL, then use a Kernel browser to visit every route, capture screenshots, and produce a preview report. + + + +Start an E2B sandbox using the `kernel-browser` template. No local browser binary is needed — Kernel provides the browser remotely. + +```python +from e2b import Sandbox + +sandbox = Sandbox.create( + "kernel-browser", + envs={"KERNEL_API_KEY": os.environ["KERNEL_API_KEY"]}, + timeout=300, +) +``` + + + +Write a FastAPI application into the sandbox and start it as a background process on port 8000. + +```python +sandbox.files.write("/home/user/app.py", FASTAPI_APP) +sandbox.commands.run( + "pip install --break-system-packages fastapi uvicorn", + timeout=60, +) +sandbox.commands.run( + "uvicorn app:app --host 0.0.0.0 --port 8000", + background=True, + cwd="/home/user", +) +``` + + + +E2B exposes any sandbox port as a public HTTPS endpoint. + +```python +host = sandbox.get_host(8000) +app_url = f"https://{host}" +``` + + + +A script running inside the sandbox creates a Kernel cloud browser, connects via Playwright CDP, and visits each route to take screenshots. + +```python +from kernel import Kernel +from playwright.sync_api import sync_playwright + +kernel = Kernel() +kb = kernel.browsers.create() + +with sync_playwright() as pw: + browser = pw.chromium.connect_over_cdp(kb.cdp_ws_url) + page = browser.new_page() + page.set_viewport_size({"width": 1280, "height": 720}) + + page.goto(app_url, wait_until="networkidle") + page.screenshot(path="/home/user/screenshots/home.png") +``` + + + +After visiting every route, the script writes a JSON report with page titles and screenshot paths. The orchestrator reads it back from the sandbox. + +```python +import json + +report = json.loads( + sandbox.files.read("/home/user/preview_report.json") +) +for entry in report: + print(f"Route: {entry['route']} — Title: {entry['title']}") +``` + + + +### Full example + +```python app_preview.py expandable +""" +App Preview — E2B + Kernel + +Spins up a web app inside an E2B sandbox, exposes it at a public URL, +then uses a Kernel cloud browser to navigate the app, take screenshots +of each route, and generate a visual preview report. +""" + +import os +import time +import json + +from e2b import Sandbox + +# A sample FastAPI app to deploy inside the sandbox +FASTAPI_APP = ''' +from fastapi import FastAPI +from fastapi.responses import HTMLResponse + +app = FastAPI() + +@app.get("/", response_class=HTMLResponse) +def home(): + return """ + + + My App +

Welcome to My App

+ + """ + +@app.get("/about", response_class=HTMLResponse) +def about(): + return """ + + + About +

About

E2B + Kernel integration demo.

+ + """ + +@app.get("/api/status") +def status(): + return {"status": "ok", "sandbox": "e2b", "browser": "kernel"} +''' + +# Script that runs inside the sandbox to browse the app with Kernel +BROWSE_SCRIPT = ''' +import sys +import json +from kernel import Kernel +from playwright.sync_api import sync_playwright + +app_url = sys.argv[1] +routes = ["/", "/about"] + +kernel = Kernel() +kb = kernel.browsers.create() + +results = [] + +with sync_playwright() as pw: + browser = pw.chromium.connect_over_cdp(kb.cdp_ws_url) + page = browser.new_page() + page.set_viewport_size({"width": 1280, "height": 720}) + + for route in routes: + url = f"{app_url}{route}" + page.goto(url, wait_until="networkidle", timeout=15000) + + # Take screenshot + safe_name = route.strip("/").replace("/", "_") or "home" + screenshot_path = f"/home/user/screenshots/{safe_name}.png" + page.screenshot(path=screenshot_path) + + # Collect page info + results.append({ + "route": route, + "title": page.title(), + "screenshot": screenshot_path, + }) + + browser.close() + +with open("/home/user/preview_report.json", "w") as f: + json.dump(results, f) +''' + + +def main(): + sandbox = Sandbox.create( + "kernel-browser", + envs={"KERNEL_API_KEY": os.environ["KERNEL_API_KEY"]}, + timeout=300, + ) + + try: + # Deploy and start the FastAPI app + sandbox.files.write("/home/user/app.py", FASTAPI_APP) + sandbox.commands.run("mkdir -p /home/user/screenshots", timeout=5) + sandbox.commands.run( + "pip install --break-system-packages fastapi uvicorn", + timeout=60, + ) + sandbox.commands.run( + "uvicorn app:app --host 0.0.0.0 --port 8000", + background=True, + cwd="/home/user", + ) + time.sleep(3) + + # Get the public URL + host = sandbox.get_host(8000) + app_url = f"https://{host}" + print(f"App is live at: {app_url}") + + # Use Kernel browser to preview the app + sandbox.files.write("/home/user/browse.py", BROWSE_SCRIPT) + result = sandbox.commands.run( + f'python3 /home/user/browse.py "{app_url}"', + timeout=120, + ) + + # Read the preview report + report = json.loads( + sandbox.files.read("/home/user/preview_report.json") + ) + for entry in report: + print(f"Route: {entry['route']} — Title: {entry['title']}") + + finally: + sandbox.kill() + + +if __name__ == "__main__": + main() +``` + +## Agent browser with Browser Use + +Add the [Browser Use](https://docs.browser-use.com/) framework to give an LLM autonomous control over a Kernel browser. The agent sees the page via screenshots and decides what to click, type, and navigate — you just give it a task. + +This requires an additional LLM API key (Anthropic, OpenAI, or other [supported model](https://docs.browser-use.com/customize/supported-models)): + +```bash .env +ANTHROPIC_API_KEY=sk-ant-*** +``` + + + +Start an E2B sandbox using the `kernel-agent-browser` template, which includes Browser Use on top of the Kernel SDK and Playwright. + +```python +from e2b import Sandbox + +sandbox = Sandbox.create( + "kernel-agent-browser", + envs={ + "KERNEL_API_KEY": os.environ["KERNEL_API_KEY"], + "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"], + }, + timeout=300, +) +``` + + + +The agent script creates a Kernel browser, connects Browser Use to it, and runs a task autonomously. + +```python +AGENT_SCRIPT = ''' +import asyncio +from kernel import Kernel +from browser_use import Agent, Browser, ChatAnthropic + +async def main(): + kernel = Kernel() + kb = kernel.browsers.create() + + browser = Browser(cdp_url=kb.cdp_ws_url) + + agent = Agent( + task="Go to Hacker News, find the top 3 AI stories, and summarize them", + llm=ChatAnthropic(model="claude-sonnet-4-20250514"), + browser=browser, + ) + result = await agent.run() + print(result) + +asyncio.run(main()) +''' + +sandbox.files.write("/home/user/agent_task.py", AGENT_SCRIPT) +``` + + + +Execute the agent inside the sandbox. The agent will autonomously browse, click, type, and navigate to complete the task. + +```python +result = sandbox.commands.run( + "python3 /home/user/agent_task.py", + timeout=180, +) +print(result.stdout) +``` + + + +### Choosing an LLM + +Browser Use supports multiple LLM providers. Import the one you need and pass the corresponding API key in the sandbox `envs`. + +```python +# Anthropic (recommended) +from browser_use import ChatAnthropic +llm = ChatAnthropic(model="claude-sonnet-4-20250514") + +# OpenAI +from browser_use import ChatOpenAI +llm = ChatOpenAI(model="gpt-4o") + +# Google +from browser_use import ChatGoogle +llm = ChatGoogle(model="gemini-2.5-flash") +``` + +### Full example + +```python agent_browser.py expandable +""" +Agent Remote Browser — E2B + Kernel + Browser Use + +Spins up an E2B sandbox with Browser Use framework and Kernel cloud browser. +An AI agent autonomously browses the web to complete a research task. +""" + +import os + +from e2b import Sandbox + +AGENT_SCRIPT = ''' +import asyncio +from kernel import Kernel +from browser_use import Agent, Browser, ChatAnthropic + +async def main(): + # Create a Kernel cloud browser + kernel = Kernel() + kb = kernel.browsers.create() + print(f"Kernel browser created: {kb.id}") + + # Connect Browser Use to the Kernel browser via CDP + browser = Browser(cdp_url=kb.cdp_ws_url) + + # Create an AI agent that autonomously browses + agent = Agent( + task=""" + Go to https://news.ycombinator.com and find the top 3 stories + that are about AI or machine learning. For each story: + 1. Note the title and point count + 2. Click through to the comments page + 3. Read the top comment + + Return a summary of your findings. + """, + llm=ChatAnthropic(model="claude-sonnet-4-20250514"), + browser=browser, + max_actions_per_step=4, + ) + + result = await agent.run() + print("\\n" + "=" * 60) + print("AGENT RESULT:") + print("=" * 60) + print(result) + +asyncio.run(main()) +''' + + +def main(): + sandbox = Sandbox.create( + "kernel-agent-browser", + envs={ + "KERNEL_API_KEY": os.environ["KERNEL_API_KEY"], + "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"], + }, + timeout=300, + ) + + try: + sandbox.files.write("/home/user/agent_task.py", AGENT_SCRIPT) + + result = sandbox.commands.run( + "python3 /home/user/agent_task.py", + timeout=180, + ) + + if result.exit_code != 0: + print(f"Agent failed: {result.stderr}") + else: + print(result.stdout) + + finally: + sandbox.kill() + + +if __name__ == "__main__": + main() +``` + +## Kernel MCP tools + +The [Kernel MCP Server](https://mcp.onkernel.com/mcp) exposes 10 tools over the Model Context Protocol. Connect any MCP-compatible client — no installation needed. + +Install for CLI tools: + +```bash +kernel mcp install --target # Supports Cursor, Claude Desktop, VS Code, etc. +``` + +Or use stdio transport: + +```bash +npx -y mcp-remote https://mcp.onkernel.com/mcp +``` + +Authentication uses OAuth 2.0 via Clerk. + +| Tool | Description | +|---|---| +| `manage_browsers` | Create, list, get, and delete browser sessions (headless/stealth modes, profiles, proxies, viewports, extensions, SSH) | +| `manage_profiles` | Set up, list, and delete browser profiles for persisting cookies and logins | +| `manage_browser_pools` | Create and manage pools of pre-warmed browsers | +| `manage_proxies` | Create and manage proxy configurations (datacenter, ISP, residential, mobile, custom) | +| `manage_extensions` | List and delete uploaded browser extensions | +| `manage_apps` | List apps, invoke actions, get deployments, and retrieve invocation results | +| `computer_action` | Mouse, keyboard, and screenshot controls for browser sessions | +| `execute_playwright_code` | Execute Playwright/TypeScript code with automatic video replay and cleanup | +| `exec_command` | Run shell commands inside a browser VM | +| `search_docs` | Search Kernel platform documentation | + +## Deploying Kernel skills + +An agent running inside the sandbox can use the Kernel CLI to create, deploy, and invoke browser automation skills — turning any one-off workflow into a persistent, reusable automation. + +```python +# Agent deploys a reusable browser automation skill +sandbox.commands.run("kernel create --name price-checker --language python --template browser-use", cwd="/home/user") +# ... agent writes the automation code ... +sandbox.commands.run("kernel deploy main.py --env-file .env", cwd="/home/user/price-checker") +# Later, invoke the deployed skill +sandbox.commands.run('kernel invoke price-checker check --payload \'{"url": "https://example.com"}\'') +``` + +## Key concepts + +| Concept | Detail | +|---|---| +| **E2B templates** | `kernel-browser` and `kernel-agent-browser` — pre-built with the Kernel SDK and Playwright | +| **Public URL** | `sandbox.get_host(port)` returns an HTTPS hostname for any sandbox port | +| **Background process** | `sandbox.commands.run(..., background=True)` starts a web server without blocking | +| **Kernel browser** | `kernel.browsers.create()` spins up a remote Chromium; connect via `kb.cdp_ws_url` | +| **CDP connection** | Playwright's `connect_over_cdp()` drives the remote browser with full API access | +| **Browser Use** | Agent framework that connects an LLM to Playwright for autonomous browsing | +| **Kernel MCP Server** | Hosted at `https://mcp.onkernel.com/mcp` — 10 MCP tools for browser management and automation | +| **Kernel Skills** | `kernel create` + `kernel deploy` packages browser automations as persistent, invokable skills | + +## Related guides + + + + Create, manage, and control sandbox lifecycle + + + Run terminal commands inside the sandbox + + + Build AI agents that control virtual desktops + + diff --git a/docs/use-cases/agent-browser.mdx b/docs/use-cases/agent-browser.mdx deleted file mode 100644 index cc889810..00000000 --- a/docs/use-cases/agent-browser.mdx +++ /dev/null @@ -1,235 +0,0 @@ ---- -title: "Agent remote browser" -description: "Run an autonomous AI agent inside an E2B sandbox that browses the web using a Kernel cloud browser and the Browser Use framework." -icon: "robot" ---- - -Run an AI agent inside an E2B sandbox that autonomously controls a [Kernel](https://www.kernel.computer/) cloud browser. The agent decides what to click, type, and navigate — you just give it a task. - -This builds on the [remote browser](/docs/use-cases/browser-use) pattern by adding the [Browser Use](https://docs.browser-use.com/) framework, which turns an LLM into a browser-controlling agent. - -## Architecture - -1. **E2B Sandbox** — isolated environment where the agent code runs. Pre-installed with Kernel SDK, Playwright, and Browser Use. -2. **Kernel Cloud Browser** — remote Chromium instance the agent controls via CDP. -3. **Browser Use** — agent framework that connects an LLM to Playwright. The LLM sees screenshots and decides actions (click, type, scroll, navigate). - -The orchestrator creates the sandbox and kicks off the agent. The agent runs autonomously inside the sandbox — it creates a Kernel browser, connects Browser Use, and executes the task. - -## Prerequisites - -- An [E2B API key](https://e2b.dev/dashboard?tab=keys) -- A [Kernel API key](https://www.kernel.computer/) -- An LLM API key (Anthropic, OpenAI, or other [supported model](https://docs.browser-use.com/customize/supported-models)) -- Python 3.10+ - -```bash -pip install e2b-code-interpreter -``` - -Set your keys in the environment: - -```bash .env -E2B_API_KEY=e2b_*** -KERNEL_API_KEY=kernel_*** -ANTHROPIC_API_KEY=sk-ant-*** -``` - -## How it works - - - -Start an E2B sandbox using the `kernel-agent-browser` template, which comes with Kernel SDK, Playwright, and Browser Use pre-installed. Pass the API keys the agent will need. - -```python -from e2b_code_interpreter import Sandbox - -sandbox = Sandbox.create( - "kernel-agent-browser", - envs={ - "KERNEL_API_KEY": os.environ["KERNEL_API_KEY"], - "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"], - }, - timeout=300, -) -``` - - - -The agent script creates a Kernel browser, connects Browser Use to it, and runs a task autonomously. - -```python -AGENT_SCRIPT = ''' -import asyncio -from kernel import Kernel -from browser_use import Agent, Browser, ChatAnthropic - -async def main(): - kernel = Kernel() - kb = kernel.browsers.create() - - browser = Browser(cdp_url=kb.cdp_ws_url) - - agent = Agent( - task="Go to Hacker News, find the top 3 AI stories, and summarize them", - llm=ChatAnthropic(model="claude-sonnet-4-20250514"), - browser=browser, - ) - result = await agent.run() - print(result) - -asyncio.run(main()) -''' - -sandbox.files.write("/home/user/agent_task.py", AGENT_SCRIPT) -``` - - - -Execute the agent inside the sandbox. The agent will autonomously browse, click, type, and navigate to complete the task. - -```python -result = sandbox.commands.run( - "python3 /home/user/agent_task.py", - timeout=180, -) -print(result.stdout) -``` - - - -## Full example - -```python agent_browser.py expandable -""" -Agent Remote Browser — E2B + Kernel + Browser Use - -Spins up an E2B sandbox with Browser Use framework and Kernel cloud browser. -An AI agent autonomously browses the web to complete a research task. -""" - -import os - -from e2b_code_interpreter import Sandbox - -AGENT_SCRIPT = ''' -import asyncio -from kernel import Kernel -from browser_use import Agent, Browser, ChatAnthropic - -async def main(): - # Create a Kernel cloud browser - kernel = Kernel() - kb = kernel.browsers.create() - print(f"Kernel browser created: {kb.id}") - - # Connect Browser Use to the Kernel browser via CDP - browser = Browser(cdp_url=kb.cdp_ws_url) - - # Create an AI agent that autonomously browses - agent = Agent( - task=""" - Go to https://news.ycombinator.com and find the top 3 stories - that are about AI or machine learning. For each story: - 1. Note the title and point count - 2. Click through to the comments page - 3. Read the top comment - - Return a summary of your findings. - """, - llm=ChatAnthropic(model="claude-sonnet-4-20250514"), - browser=browser, - max_actions_per_step=4, - ) - - result = await agent.run() - print("\\n" + "=" * 60) - print("AGENT RESULT:") - print("=" * 60) - print(result) - -asyncio.run(main()) -''' - - -def main(): - sandbox = Sandbox.create( - "kernel-agent-browser", - envs={ - "KERNEL_API_KEY": os.environ["KERNEL_API_KEY"], - "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"], - }, - timeout=300, - ) - - try: - sandbox.files.write("/home/user/agent_task.py", AGENT_SCRIPT) - - result = sandbox.commands.run( - "python3 /home/user/agent_task.py", - timeout=180, - ) - - if result.exit_code != 0: - print(f"Agent failed: {result.stderr}") - else: - print(result.stdout) - - finally: - sandbox.kill() - - -if __name__ == "__main__": - main() -``` - -## Key concepts - -| Concept | Detail | -|---|---| -| **E2B template** | `kernel-agent-browser` — pre-built with Kernel SDK, Playwright, and Browser Use | -| **Kernel browser** | `kernel.browsers.create()` spins up a remote Chromium; connect via `kb.cdp_ws_url` | -| **Browser Use** | `Browser(cdp_url=...)` connects the agent framework to Kernel's CDP endpoint | -| **LLM choice** | Browser Use supports `ChatAnthropic`, `ChatOpenAI`, `ChatGoogle`, and more | -| **Autonomous agent** | The LLM sees the page (via screenshots) and decides what actions to take | - -## Choosing an LLM - -Browser Use supports multiple LLM providers. Import the one you need: - -```python -# Anthropic (recommended) -from browser_use import ChatAnthropic -llm = ChatAnthropic(model="claude-sonnet-4-20250514") - -# OpenAI -from browser_use import ChatOpenAI -llm = ChatOpenAI(model="gpt-4o") - -# Google -from browser_use import ChatGoogle -llm = ChatGoogle(model="gemini-2.5-flash") -``` - -Pass the corresponding API key in the sandbox `envs`. - -## Adapting this example - -- **Different tasks** — change the `task` string to any web research, form filling, or data extraction task. -- **Custom actions** — Browser Use supports [custom actions](https://docs.browser-use.com/customize/custom-actions) to extend agent capabilities. -- **Vision control** — set `use_vision="auto"` on the Agent to let it decide when to use screenshots vs DOM. -- **Multiple agents** — run several agents in parallel, each with their own Kernel browser, for concurrent research. - -## Related guides - - - - Programmatic browser automation with Playwright + Kernel - - - Build AI agents that control virtual desktops - - - Create, manage, and control sandbox lifecycle - - diff --git a/docs/use-cases/browser-use.mdx b/docs/use-cases/browser-use.mdx deleted file mode 100644 index 015cd9fc..00000000 --- a/docs/use-cases/browser-use.mdx +++ /dev/null @@ -1,283 +0,0 @@ ---- -title: "Remote browser" -description: "Deploy a web app in an E2B sandbox and use a Kernel cloud browser to screenshot every route and generate a preview report." -icon: "globe" ---- - -Deploy a web application inside an E2B sandbox, get a public URL, then use a [Kernel](https://www.kernel.computer/) cloud browser to visit every route, capture screenshots, and produce a structured preview report — all without running a browser locally. - -## Architecture - -This example combines two services: - -1. **E2B Sandbox** — a secure cloud environment where the web app runs. E2B exposes the app at a public HTTPS URL via `sandbox.get_host(port)`. -2. **Kernel Cloud Browser** — a remote Chromium instance controlled through Playwright's CDP protocol. It navigates the public URL, takes screenshots, and collects page metadata. - -The orchestrator script on your machine creates the sandbox, deploys the app, and then runs a browsing script _inside_ the sandbox that connects to a Kernel browser and screenshots each route. - -## Prerequisites - -- An [E2B API key](https://e2b.dev/dashboard?tab=keys) -- A [Kernel API key](https://www.kernel.computer/) -- Python 3.10+ - -```bash -pip install e2b -``` - -Set both keys in your environment: - -```bash .env -E2B_API_KEY=e2b_*** -KERNEL_API_KEY=kernel_*** -``` - -## How it works - - - -Start an E2B sandbox using the `kernel-browser` template, which comes with the Kernel SDK and Playwright client pre-installed. No local browser binary is needed — Kernel provides the browser remotely. - -```python -from e2b import Sandbox - -sandbox = Sandbox.create( - "kernel-browser", - envs={"KERNEL_API_KEY": os.environ["KERNEL_API_KEY"]}, - timeout=300, -) -``` - - - -Write a FastAPI application into the sandbox and start it as a background process on port 8000. - -```python -sandbox.files.write("/home/user/app.py", FASTAPI_APP) -sandbox.commands.run( - "pip install --break-system-packages fastapi uvicorn", - timeout=60, -) -sandbox.commands.run( - "uvicorn app:app --host 0.0.0.0 --port 8000", - background=True, - cwd="/home/user", -) -``` - - - -E2B exposes any sandbox port as a public HTTPS endpoint. - -```python -host = sandbox.get_host(8000) -app_url = f"https://{host}" -``` - - - -A script running inside the sandbox creates a Kernel cloud browser, connects via Playwright CDP, and visits each route to take screenshots. - -```python -from kernel import Kernel -from playwright.sync_api import sync_playwright - -kernel = Kernel() -kb = kernel.browsers.create() - -with sync_playwright() as pw: - browser = pw.chromium.connect_over_cdp(kb.cdp_ws_url) - page = browser.new_page() - page.set_viewport_size({"width": 1280, "height": 720}) - - page.goto(app_url, wait_until="networkidle") - page.screenshot(path="/home/user/screenshots/home.png") -``` - - - -After visiting every route, the script writes a JSON report with page titles and screenshot paths. The orchestrator reads it back from the sandbox. - -```python -import json - -report = json.loads( - sandbox.files.read("/home/user/preview_report.json") -) -for entry in report: - print(f"Route: {entry['route']} — Title: {entry['title']}") -``` - - - -## Full example - -```python app_preview.py expandable -""" -App Preview — E2B + Kernel - -Spins up a web app inside an E2B sandbox, exposes it at a public URL, -then uses a Kernel cloud browser to navigate the app, take screenshots -of each route, and generate a visual preview report. -""" - -import os -import time -import json - -from e2b import Sandbox - -# A sample FastAPI app to deploy inside the sandbox -FASTAPI_APP = ''' -from fastapi import FastAPI -from fastapi.responses import HTMLResponse - -app = FastAPI() - -@app.get("/", response_class=HTMLResponse) -def home(): - return """ - - - My App -

Welcome to My App

- - """ - -@app.get("/about", response_class=HTMLResponse) -def about(): - return """ - - - About -

About

E2B + Kernel integration demo.

- - """ - -@app.get("/api/status") -def status(): - return {"status": "ok", "sandbox": "e2b", "browser": "kernel"} -''' - -# Script that runs inside the sandbox to browse the app with Kernel -BROWSE_SCRIPT = ''' -import sys -import json -from kernel import Kernel -from playwright.sync_api import sync_playwright - -app_url = sys.argv[1] -routes = ["/", "/about"] - -kernel = Kernel() -kb = kernel.browsers.create() - -results = [] - -with sync_playwright() as pw: - browser = pw.chromium.connect_over_cdp(kb.cdp_ws_url) - page = browser.new_page() - page.set_viewport_size({"width": 1280, "height": 720}) - - for route in routes: - url = f"{app_url}{route}" - page.goto(url, wait_until="networkidle", timeout=15000) - - # Take screenshot - safe_name = route.strip("/").replace("/", "_") or "home" - screenshot_path = f"/home/user/screenshots/{safe_name}.png" - page.screenshot(path=screenshot_path) - - # Collect page info - results.append({ - "route": route, - "title": page.title(), - "screenshot": screenshot_path, - }) - - browser.close() - -with open("/home/user/preview_report.json", "w") as f: - json.dump(results, f) -''' - - -def main(): - sandbox = Sandbox.create( - "kernel-browser", - envs={"KERNEL_API_KEY": os.environ["KERNEL_API_KEY"]}, - timeout=300, - ) - - try: - # Deploy and start the FastAPI app - sandbox.files.write("/home/user/app.py", FASTAPI_APP) - sandbox.commands.run("mkdir -p /home/user/screenshots", timeout=5) - sandbox.commands.run( - "pip install --break-system-packages fastapi uvicorn", - timeout=60, - ) - sandbox.commands.run( - "uvicorn app:app --host 0.0.0.0 --port 8000", - background=True, - cwd="/home/user", - ) - time.sleep(3) - - # Get the public URL - host = sandbox.get_host(8000) - app_url = f"https://{host}" - print(f"App is live at: {app_url}") - - # Use Kernel browser to preview the app - sandbox.files.write("/home/user/browse.py", BROWSE_SCRIPT) - result = sandbox.commands.run( - f'python3 /home/user/browse.py "{app_url}"', - timeout=120, - ) - - # Read the preview report - report = json.loads( - sandbox.files.read("/home/user/preview_report.json") - ) - for entry in report: - print(f"Route: {entry['route']} — Title: {entry['title']}") - - finally: - sandbox.kill() - - -if __name__ == "__main__": - main() -``` - -## Key concepts - -| Concept | Detail | -|---|---| -| **E2B template** | `kernel-browser` — pre-built with the Kernel SDK and Playwright client | -| **Public URL** | `sandbox.get_host(port)` returns an HTTPS hostname | -| **Background process** | `sandbox.commands.run(..., background=True)` starts the web server without blocking | -| **Kernel browser** | `kernel.browsers.create()` spins up a remote Chromium; connect via `kb.cdp_ws_url` | -| **CDP connection** | Playwright's `connect_over_cdp()` drives the remote browser with full API access | - -## Adapting this example - -- **Your own app** — replace `FASTAPI_APP` with any web framework (Next.js, Flask, Express). Adjust the install commands and start script accordingly. -- **More routes** — add paths to the `routes` list in `BROWSE_SCRIPT` to screenshot additional pages. -- **Visual regression** — compare screenshots across deploys to catch UI regressions automatically. -- **CI integration** — run this as a post-deploy step to generate preview links and thumbnails for pull requests. - -## Related guides - - - - Create, manage, and control sandbox lifecycle - - - Run terminal commands inside the sandbox - - - Build AI agents that control virtual desktops - - From 6d2af4f11eef05b51214bf74dc20cb8db347bd4e Mon Sep 17 00:00:00 2001 From: Tomas Beran Date: Tue, 24 Mar 2026 21:52:02 +0100 Subject: [PATCH 09/16] docs: Move Kernel page to use-cases/remote-browser --- docs.json | 9 ++------- .../kernel.mdx => use-cases/remote-browser.mdx} | 2 +- 2 files changed, 3 insertions(+), 8 deletions(-) rename docs/{integrations/kernel.mdx => use-cases/remote-browser.mdx} (99%) diff --git a/docs.json b/docs.json index c8b96be1..a4ee2fe0 100644 --- a/docs.json +++ b/docs.json @@ -47,13 +47,8 @@ "pages": [ "docs/use-cases/coding-agents", "docs/use-cases/computer-use", - "docs/use-cases/ci-cd" - ] - }, - { - "group": "Integrations", - "pages": [ - "docs/integrations/kernel" + "docs/use-cases/ci-cd", + "docs/use-cases/remote-browser" ] }, { diff --git a/docs/integrations/kernel.mdx b/docs/use-cases/remote-browser.mdx similarity index 99% rename from docs/integrations/kernel.mdx rename to docs/use-cases/remote-browser.mdx index 6e398ac8..55ca08a4 100644 --- a/docs/integrations/kernel.mdx +++ b/docs/use-cases/remote-browser.mdx @@ -1,5 +1,5 @@ --- -title: "Kernel" +title: "Remote browser" description: "Use Kernel cloud browsers with E2B sandboxes for web scraping, app previews, and autonomous browser agents." icon: "globe" --- From b3fd603f7ef2be60ba7a30ae13831d586a99a04b Mon Sep 17 00:00:00 2001 From: Tomas Beran Date: Tue, 24 Mar 2026 21:59:01 +0100 Subject: [PATCH 10/16] docs: Improve remote browser page flow and structure Lead with why (no sandbox compute wasted, zero install, CAPTCHA handling, parallel browsing), show two approaches upfront, then streamlined walkthroughs. --- docs/use-cases/remote-browser.mdx | 103 ++++++++++++------------------ 1 file changed, 41 insertions(+), 62 deletions(-) diff --git a/docs/use-cases/remote-browser.mdx b/docs/use-cases/remote-browser.mdx index 55ca08a4..ef6a2543 100644 --- a/docs/use-cases/remote-browser.mdx +++ b/docs/use-cases/remote-browser.mdx @@ -4,14 +4,27 @@ description: "Use Kernel cloud browsers with E2B sandboxes for web scraping, app icon: "globe" --- -[Kernel](https://www.kernel.computer/) provides cloud browsers — remote Chromium instances you can control programmatically via [CDP](https://chromedevtools.github.io/devtools-protocol/). Combined with E2B sandboxes, your agents can browse the web without running a browser locally. +[Kernel](https://www.kernel.computer/) provides cloud browsers — remote Chromium instances your agents can control via [CDP](https://chromedevtools.github.io/devtools-protocol/) (Chrome DevTools Protocol). -E2B provides pre-built sandbox templates with the Kernel SDK and Playwright already installed: +## Why use a remote browser? -| Template | Includes | Use case | +Running a browser inside the sandbox works, but offloading it to Kernel has real advantages: + +- **No sandbox compute wasted on Chrome** — Chromium is resource-hungry. Kernel runs it on dedicated infrastructure so your sandbox CPU and memory stay available for your agent's actual work. +- **Zero installation** — no need to install Chrome, Chromium, or Playwright binaries in your sandbox template. Kernel provides the browser remotely. +- **Built-in CAPTCHA and bot detection handling** — Kernel browsers support stealth mode, residential proxies, and browser profiles that persist cookies and sessions across runs. +- **Parallel browsing** — spin up multiple Kernel browsers simultaneously without multiplying sandbox costs. + +## Two approaches + +E2B provides pre-built sandbox templates with the Kernel SDK already installed: + +| Template | What's included | Best for | |---|---|---| -| `kernel-browser` | Kernel SDK, Playwright | Programmatic browsing, screenshots, scraping | -| `kernel-agent-browser` | Kernel SDK, Playwright, Browser Use | Autonomous AI agent browsing | +| `kernel-browser` | Kernel SDK, Playwright | Programmatic browsing — screenshots, scraping, app previews | +| `kernel-agent-browser` | Kernel SDK, Playwright, Browser Use | Autonomous AI agents that browse on their own | + +Both templates connect to Kernel browsers over CDP. The difference is whether *you* write the browsing logic or an *LLM agent* decides what to do. ## Prerequisites @@ -30,15 +43,18 @@ E2B_API_KEY=e2b_*** KERNEL_API_KEY=kernel_*** ``` -## App preview with Kernel +## Programmatic browsing + +Use this approach when you know exactly what pages to visit and what to do on them — taking screenshots, scraping data, or generating previews. -Deploy a web app inside a sandbox, get a public URL, then use a Kernel browser to visit every route, capture screenshots, and produce a preview report. +This example deploys a web app inside a sandbox, gets a public URL, then uses a Kernel browser to screenshot every route. - -Start an E2B sandbox using the `kernel-browser` template. No local browser binary is needed — Kernel provides the browser remotely. + +Start a sandbox with the `kernel-browser` template, write your web app into it, and start the server. ```python +import os from e2b import Sandbox sandbox = Sandbox.create( @@ -46,13 +62,7 @@ sandbox = Sandbox.create( envs={"KERNEL_API_KEY": os.environ["KERNEL_API_KEY"]}, timeout=300, ) -``` - - - -Write a FastAPI application into the sandbox and start it as a background process on port 8000. -```python sandbox.files.write("/home/user/app.py", FASTAPI_APP) sandbox.commands.run( "pip install --break-system-packages fastapi uvicorn", @@ -66,19 +76,14 @@ sandbox.commands.run( ``` - -E2B exposes any sandbox port as a public HTTPS endpoint. + +E2B exposes any sandbox port as a public HTTPS endpoint. A script inside the sandbox creates a Kernel browser, connects via Playwright CDP, and screenshots each route. ```python host = sandbox.get_host(8000) app_url = f"https://{host}" -``` - - -A script running inside the sandbox creates a Kernel cloud browser, connects via Playwright CDP, and visits each route to take screenshots. - -```python +# This runs inside the sandbox from kernel import Kernel from playwright.sync_api import sync_playwright @@ -95,8 +100,8 @@ with sync_playwright() as pw: ``` - -After visiting every route, the script writes a JSON report with page titles and screenshot paths. The orchestrator reads it back from the sandbox. + +After visiting every route, read the report back from the sandbox. ```python import json @@ -251,9 +256,9 @@ if __name__ == "__main__": main() ``` -## Agent browser with Browser Use +## Autonomous agent browsing -Add the [Browser Use](https://docs.browser-use.com/) framework to give an LLM autonomous control over a Kernel browser. The agent sees the page via screenshots and decides what to click, type, and navigate — you just give it a task. +Use this approach when you want an LLM to decide what to click, type, and navigate. The [Browser Use](https://docs.browser-use.com/) framework connects an LLM to the Kernel browser — the agent sees the page via screenshots and autonomously completes tasks. This requires an additional LLM API key (Anthropic, OpenAI, or other [supported model](https://docs.browser-use.com/customize/supported-models)): @@ -263,7 +268,7 @@ ANTHROPIC_API_KEY=sk-ant-*** -Start an E2B sandbox using the `kernel-agent-browser` template, which includes Browser Use on top of the Kernel SDK and Playwright. +Use the `kernel-agent-browser` template, which includes Browser Use on top of the Kernel SDK and Playwright. ```python from e2b import Sandbox @@ -279,8 +284,8 @@ sandbox = Sandbox.create( ``` - -The agent script creates a Kernel browser, connects Browser Use to it, and runs a task autonomously. + +The agent script creates a Kernel browser, connects Browser Use, and runs a task autonomously. You just describe what you want done. ```python AGENT_SCRIPT = ''' @@ -291,7 +296,6 @@ from browser_use import Agent, Browser, ChatAnthropic async def main(): kernel = Kernel() kb = kernel.browsers.create() - browser = Browser(cdp_url=kb.cdp_ws_url) agent = Agent( @@ -306,17 +310,7 @@ asyncio.run(main()) ''' sandbox.files.write("/home/user/agent_task.py", AGENT_SCRIPT) -``` - - - -Execute the agent inside the sandbox. The agent will autonomously browse, click, type, and navigate to complete the task. - -```python -result = sandbox.commands.run( - "python3 /home/user/agent_task.py", - timeout=180, -) +result = sandbox.commands.run("python3 /home/user/agent_task.py", timeout=180) print(result.stdout) ``` @@ -427,9 +421,7 @@ if __name__ == "__main__": ## Kernel MCP tools -The [Kernel MCP Server](https://mcp.onkernel.com/mcp) exposes 10 tools over the Model Context Protocol. Connect any MCP-compatible client — no installation needed. - -Install for CLI tools: +The [Kernel MCP Server](https://mcp.onkernel.com/mcp) exposes 10 tools over the Model Context Protocol, letting MCP-compatible clients manage browsers without any SDK installation. ```bash kernel mcp install --target # Supports Cursor, Claude Desktop, VS Code, etc. @@ -458,30 +450,17 @@ Authentication uses OAuth 2.0 via Clerk. ## Deploying Kernel skills -An agent running inside the sandbox can use the Kernel CLI to create, deploy, and invoke browser automation skills — turning any one-off workflow into a persistent, reusable automation. +An agent running inside the sandbox can use the Kernel CLI to package any browser workflow as a persistent, reusable automation. ```python -# Agent deploys a reusable browser automation skill +# Create a new skill from a template sandbox.commands.run("kernel create --name price-checker --language python --template browser-use", cwd="/home/user") -# ... agent writes the automation code ... +# Deploy it sandbox.commands.run("kernel deploy main.py --env-file .env", cwd="/home/user/price-checker") -# Later, invoke the deployed skill +# Invoke it later sandbox.commands.run('kernel invoke price-checker check --payload \'{"url": "https://example.com"}\'') ``` -## Key concepts - -| Concept | Detail | -|---|---| -| **E2B templates** | `kernel-browser` and `kernel-agent-browser` — pre-built with the Kernel SDK and Playwright | -| **Public URL** | `sandbox.get_host(port)` returns an HTTPS hostname for any sandbox port | -| **Background process** | `sandbox.commands.run(..., background=True)` starts a web server without blocking | -| **Kernel browser** | `kernel.browsers.create()` spins up a remote Chromium; connect via `kb.cdp_ws_url` | -| **CDP connection** | Playwright's `connect_over_cdp()` drives the remote browser with full API access | -| **Browser Use** | Agent framework that connects an LLM to Playwright for autonomous browsing | -| **Kernel MCP Server** | Hosted at `https://mcp.onkernel.com/mcp` — 10 MCP tools for browser management and automation | -| **Kernel Skills** | `kernel create` + `kernel deploy` packages browser automations as persistent, invokable skills | - ## Related guides From aca66d3f1b7d5980a57cd54f6951f986b516750f Mon Sep 17 00:00:00 2001 From: Tomas Beran Date: Tue, 24 Mar 2026 22:03:55 +0100 Subject: [PATCH 11/16] docs: Remove cost framing from remote browser benefits --- docs/use-cases/remote-browser.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/use-cases/remote-browser.mdx b/docs/use-cases/remote-browser.mdx index ef6a2543..51fbaf6b 100644 --- a/docs/use-cases/remote-browser.mdx +++ b/docs/use-cases/remote-browser.mdx @@ -10,10 +10,10 @@ icon: "globe" Running a browser inside the sandbox works, but offloading it to Kernel has real advantages: -- **No sandbox compute wasted on Chrome** — Chromium is resource-hungry. Kernel runs it on dedicated infrastructure so your sandbox CPU and memory stay available for your agent's actual work. +- **Keeps your sandbox lightweight** — Chromium is resource-hungry. Offloading it to Kernel frees your sandbox CPU and memory for your agent's actual work. - **Zero installation** — no need to install Chrome, Chromium, or Playwright binaries in your sandbox template. Kernel provides the browser remotely. - **Built-in CAPTCHA and bot detection handling** — Kernel browsers support stealth mode, residential proxies, and browser profiles that persist cookies and sessions across runs. -- **Parallel browsing** — spin up multiple Kernel browsers simultaneously without multiplying sandbox costs. +- **Parallel browsing** — spin up multiple Kernel browsers simultaneously from a single sandbox. ## Two approaches From 6b8fd28bf43237a570c97902ec6207087f5e7928 Mon Sep 17 00:00:00 2001 From: Tomas Beran Date: Tue, 24 Mar 2026 22:41:27 +0100 Subject: [PATCH 12/16] docs: Improve remote browser page flow and structure --- docs/use-cases/remote-browser.mdx | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/docs/use-cases/remote-browser.mdx b/docs/use-cases/remote-browser.mdx index 51fbaf6b..1eeb8a4c 100644 --- a/docs/use-cases/remote-browser.mdx +++ b/docs/use-cases/remote-browser.mdx @@ -4,28 +4,17 @@ description: "Use Kernel cloud browsers with E2B sandboxes for web scraping, app icon: "globe" --- -[Kernel](https://www.kernel.computer/) provides cloud browsers — remote Chromium instances your agents can control via [CDP](https://chromedevtools.github.io/devtools-protocol/) (Chrome DevTools Protocol). +[Kernel](https://www.kernel.computer/) provides cloud browsers — remote Chromium instances your agents can control through Playwright. Instead of running a browser inside the sandbox, your agent connects to a Kernel browser over the network. -## Why use a remote browser? +This keeps your sandbox lightweight — no Chrome installation, no GPU-hungry rendering. Kernel also handles CAPTCHA and bot detection with stealth mode, residential proxies, and persistent browser profiles. You can spin up multiple browsers simultaneously from a single sandbox. -Running a browser inside the sandbox works, but offloading it to Kernel has real advantages: - -- **Keeps your sandbox lightweight** — Chromium is resource-hungry. Offloading it to Kernel frees your sandbox CPU and memory for your agent's actual work. -- **Zero installation** — no need to install Chrome, Chromium, or Playwright binaries in your sandbox template. Kernel provides the browser remotely. -- **Built-in CAPTCHA and bot detection handling** — Kernel browsers support stealth mode, residential proxies, and browser profiles that persist cookies and sessions across runs. -- **Parallel browsing** — spin up multiple Kernel browsers simultaneously from a single sandbox. - -## Two approaches - -E2B provides pre-built sandbox templates with the Kernel SDK already installed: +E2B provides two pre-built sandbox templates with the Kernel SDK already installed: | Template | What's included | Best for | |---|---|---| | `kernel-browser` | Kernel SDK, Playwright | Programmatic browsing — screenshots, scraping, app previews | | `kernel-agent-browser` | Kernel SDK, Playwright, Browser Use | Autonomous AI agents that browse on their own | -Both templates connect to Kernel browsers over CDP. The difference is whether *you* write the browsing logic or an *LLM agent* decides what to do. - ## Prerequisites - An [E2B API key](https://e2b.dev/dashboard?tab=keys) @@ -77,7 +66,7 @@ sandbox.commands.run( -E2B exposes any sandbox port as a public HTTPS endpoint. A script inside the sandbox creates a Kernel browser, connects via Playwright CDP, and screenshots each route. +E2B exposes any sandbox port as a public HTTPS endpoint. A script inside the sandbox creates a Kernel browser, connects via Playwright, and screenshots each route. ```python host = sandbox.get_host(8000) From e23e7d124bdd9cffedc58c320c4245675c7d68f7 Mon Sep 17 00:00:00 2001 From: Tomas Beran Date: Tue, 24 Mar 2026 22:55:26 +0100 Subject: [PATCH 13/16] docs: Fix template names, split mixed-context code, link MCP docs --- docs/use-cases/remote-browser.mdx | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/use-cases/remote-browser.mdx b/docs/use-cases/remote-browser.mdx index 1eeb8a4c..cdcad8ad 100644 --- a/docs/use-cases/remote-browser.mdx +++ b/docs/use-cases/remote-browser.mdx @@ -65,14 +65,19 @@ sandbox.commands.run( ``` - -E2B exposes any sandbox port as a public HTTPS endpoint. A script inside the sandbox creates a Kernel browser, connects via Playwright, and screenshots each route. + +E2B exposes any sandbox port as a public HTTPS endpoint. Pass this URL to the browsing script that runs inside the sandbox. ```python host = sandbox.get_host(8000) app_url = f"https://{host}" +``` + -# This runs inside the sandbox + +Inside the sandbox, create a Kernel browser and connect via Playwright to screenshot each route. This code runs as a script inside the sandbox — it uses the Kernel SDK and Playwright that come pre-installed in the `kernel-browser` template. + +```python from kernel import Kernel from playwright.sync_api import sync_playwright @@ -410,7 +415,7 @@ if __name__ == "__main__": ## Kernel MCP tools -The [Kernel MCP Server](https://mcp.onkernel.com/mcp) exposes 10 tools over the Model Context Protocol, letting MCP-compatible clients manage browsers without any SDK installation. +The [Kernel MCP Server](https://mcp.onkernel.com/mcp) exposes 10 tools over the [Model Context Protocol](https://modelcontextprotocol.io/) (MCP), letting MCP-compatible clients manage browsers without any SDK installation. ```bash kernel mcp install --target # Supports Cursor, Claude Desktop, VS Code, etc. From 5e36134be44a121de9e34a97ffba7ca9288345c0 Mon Sep 17 00:00:00 2001 From: Tomas Beran Date: Wed, 25 Mar 2026 14:23:38 +0100 Subject: [PATCH 14/16] docs: Rework remote browser page with three focused examples - Add Note distinguishing remote vs local browser (computer use) - Restructure into three card-linked examples: screenshot endpoints, agent data extraction, live browser preview - Fix all examples so Kernel code runs inside the sandbox, not the orchestrator - Add CodeGroup tabs (Python + JS) throughout - Remove MCP tools table, Kernel skills section, and LLM chooser - Add live view example with iframe embedding and read-only mode --- docs/use-cases/remote-browser.mdx | 513 +++++++++++++----------------- 1 file changed, 213 insertions(+), 300 deletions(-) diff --git a/docs/use-cases/remote-browser.mdx b/docs/use-cases/remote-browser.mdx index cdcad8ad..fdde2986 100644 --- a/docs/use-cases/remote-browser.mdx +++ b/docs/use-cases/remote-browser.mdx @@ -1,29 +1,31 @@ --- title: "Remote browser" -description: "Use Kernel cloud browsers with E2B sandboxes for web scraping, app previews, and autonomous browser agents." +description: "Use Kernel cloud browsers with E2B sandboxes for web scraping, screenshots, and autonomous browsing agents." icon: "globe" --- -[Kernel](https://www.kernel.computer/) provides cloud browsers — remote Chromium instances your agents can control through Playwright. Instead of running a browser inside the sandbox, your agent connects to a Kernel browser over the network. + +This guide covers **remote browsers** powered by [Kernel](https://www.kernel.computer/) — cloud Chromium instances your code controls via [CDP](https://chromedevtools.github.io/devtools-protocol/) or the [Kernel SDK](https://www.kernel.sh/docs/sdk/overview). For **local browser automation** using a virtual desktop, see [Computer use](/docs/use-cases/computer-use). + -This keeps your sandbox lightweight — no Chrome installation, no GPU-hungry rendering. Kernel also handles CAPTCHA and bot detection with stealth mode, residential proxies, and persistent browser profiles. You can spin up multiple browsers simultaneously from a single sandbox. +Remote browsers run on Kernel's infrastructure, not inside your sandbox. Your agent connects to them over the network via Playwright or Puppeteer. This keeps sandboxes lightweight and lets you spin up many browsers in parallel. -E2B provides two pre-built sandbox templates with the Kernel SDK already installed: - -| Template | What's included | Best for | -|---|---|---| -| `kernel-browser` | Kernel SDK, Playwright | Programmatic browsing — screenshots, scraping, app previews | -| `kernel-agent-browser` | Kernel SDK, Playwright, Browser Use | Autonomous AI agents that browse on their own | +Kernel handles stealth mode, CAPTCHA solving, residential proxies, and persistent browser profiles out of the box. ## Prerequisites - An [E2B API key](https://e2b.dev/dashboard?tab=keys) - A [Kernel API key](https://www.kernel.computer/) -- Python 3.10+ +- Python 3.10+ / Node.js 18+ -```bash -pip install e2b + +```bash JavaScript & TypeScript +npm i e2b @onkernel/sdk playwright-core +``` +```bash Python +pip install e2b kernel playwright ``` + Set your keys in the environment: @@ -32,17 +34,57 @@ E2B_API_KEY=e2b_*** KERNEL_API_KEY=kernel_*** ``` -## Programmatic browsing +E2B provides a pre-built sandbox template with the Kernel SDK and Playwright already installed: + +| Template | What's included | Best for | +|---|---|---| +| `kernel-browser` | Kernel SDK, Playwright, Browser Use | Screenshots, scraping, app previews, autonomous agents | + +## Examples -Use this approach when you know exactly what pages to visit and what to do on them — taking screenshots, scraping data, or generating previews. +Here are three common patterns for using remote browsers with E2B sandboxes. + + + + Deploy a web app in a sandbox, screenshot every route + + + Let an LLM autonomously browse and extract data + + + Watch the browser in real time via Kernel's live view + + + +--- -This example deploys a web app inside a sandbox, gets a public URL, then uses a Kernel browser to screenshot every route. +## Screenshot app endpoints + +Deploy a web app inside an E2B sandbox, get a public URL, then use a Kernel browser to screenshot every route. - -Start a sandbox with the `kernel-browser` template, write your web app into it, and start the server. + + + +```typescript JavaScript & TypeScript +import { Sandbox } from 'e2b' -```python +const sandbox = await Sandbox.create('kernel-browser', { + envs: { KERNEL_API_KEY: process.env.KERNEL_API_KEY! }, + timeoutMs: 300_000, +}) + +await sandbox.files.write('/home/user/app.py', FASTAPI_APP) +await sandbox.commands.run( + 'pip install --break-system-packages fastapi uvicorn', + { timeoutMs: 60_000 }, +) +await sandbox.commands.run( + 'uvicorn app:app --host 0.0.0.0 --port 8000', + { background: true, cwd: '/home/user' }, +) +``` +```python Python import os from e2b import Sandbox @@ -63,24 +105,25 @@ sandbox.commands.run( cwd="/home/user", ) ``` + - -E2B exposes any sandbox port as a public HTTPS endpoint. Pass this URL to the browsing script that runs inside the sandbox. + +E2B exposes any sandbox port as a public HTTPS endpoint. Write a browsing script into the sandbox that creates a Kernel browser and screenshots each route. -```python -host = sandbox.get_host(8000) -app_url = f"https://{host}" -``` - + +```typescript JavaScript & TypeScript +const host = sandbox.getHost(8000) +const appUrl = `https://${host}` - -Inside the sandbox, create a Kernel browser and connect via Playwright to screenshot each route. This code runs as a script inside the sandbox — it uses the Kernel SDK and Playwright that come pre-installed in the `kernel-browser` template. - -```python +const BROWSE_SCRIPT = ` +import sys from kernel import Kernel from playwright.sync_api import sync_playwright +app_url = "${appUrl}" +routes = ["/", "/about", "/dashboard"] + kernel = Kernel() kb = kernel.browsers.create() @@ -89,186 +132,93 @@ with sync_playwright() as pw: page = browser.new_page() page.set_viewport_size({"width": 1280, "height": 720}) - page.goto(app_url, wait_until="networkidle") - page.screenshot(path="/home/user/screenshots/home.png") -``` - - - -After visiting every route, read the report back from the sandbox. + for route in routes: + page.goto(f"{app_url}{route}", wait_until="networkidle") + name = "home" if route == "/" else route.strip("/") + page.screenshot(path=f"/home/user/{name}.png") + print(f"Captured {route}") -```python -import json + browser.close() +` -report = json.loads( - sandbox.files.read("/home/user/preview_report.json") +await sandbox.files.write('/home/user/browse.py', BROWSE_SCRIPT) +const result = await sandbox.commands.run( + 'python3 /home/user/browse.py', + { timeoutMs: 60_000 }, ) -for entry in report: - print(f"Route: {entry['route']} — Title: {entry['title']}") +console.log(result.stdout) +await sandbox.kill() ``` - - - -### Full example - -```python app_preview.py expandable -""" -App Preview — E2B + Kernel - -Spins up a web app inside an E2B sandbox, exposes it at a public URL, -then uses a Kernel cloud browser to navigate the app, take screenshots -of each route, and generate a visual preview report. -""" - -import os -import time -import json - -from e2b import Sandbox - -# A sample FastAPI app to deploy inside the sandbox -FASTAPI_APP = ''' -from fastapi import FastAPI -from fastapi.responses import HTMLResponse - -app = FastAPI() - -@app.get("/", response_class=HTMLResponse) -def home(): - return """ - - - My App -

Welcome to My App

- - """ - -@app.get("/about", response_class=HTMLResponse) -def about(): - return """ - - - About -

About

E2B + Kernel integration demo.

- - """ - -@app.get("/api/status") -def status(): - return {"status": "ok", "sandbox": "e2b", "browser": "kernel"} -''' +```python Python +host = sandbox.get_host(8000) +app_url = f"https://{host}" -# Script that runs inside the sandbox to browse the app with Kernel -BROWSE_SCRIPT = ''' -import sys -import json +BROWSE_SCRIPT = f''' from kernel import Kernel from playwright.sync_api import sync_playwright -app_url = sys.argv[1] -routes = ["/", "/about"] +app_url = "{app_url}" +routes = ["/", "/about", "/dashboard"] kernel = Kernel() kb = kernel.browsers.create() -results = [] - with sync_playwright() as pw: browser = pw.chromium.connect_over_cdp(kb.cdp_ws_url) page = browser.new_page() - page.set_viewport_size({"width": 1280, "height": 720}) + page.set_viewport_size({{"width": 1280, "height": 720}}) for route in routes: - url = f"{app_url}{route}" - page.goto(url, wait_until="networkidle", timeout=15000) - - # Take screenshot - safe_name = route.strip("/").replace("/", "_") or "home" - screenshot_path = f"/home/user/screenshots/{safe_name}.png" - page.screenshot(path=screenshot_path) - - # Collect page info - results.append({ - "route": route, - "title": page.title(), - "screenshot": screenshot_path, - }) + page.goto(f"{{app_url}}{{route}}", wait_until="networkidle") + name = "home" if route == "/" else route.strip("/") + page.screenshot(path=f"/home/user/{{name}}.png") + print(f"Captured {{route}}") browser.close() - -with open("/home/user/preview_report.json", "w") as f: - json.dump(results, f) ''' - -def main(): - sandbox = Sandbox.create( - "kernel-browser", - envs={"KERNEL_API_KEY": os.environ["KERNEL_API_KEY"]}, - timeout=300, - ) - - try: - # Deploy and start the FastAPI app - sandbox.files.write("/home/user/app.py", FASTAPI_APP) - sandbox.commands.run("mkdir -p /home/user/screenshots", timeout=5) - sandbox.commands.run( - "pip install --break-system-packages fastapi uvicorn", - timeout=60, - ) - sandbox.commands.run( - "uvicorn app:app --host 0.0.0.0 --port 8000", - background=True, - cwd="/home/user", - ) - time.sleep(3) - - # Get the public URL - host = sandbox.get_host(8000) - app_url = f"https://{host}" - print(f"App is live at: {app_url}") - - # Use Kernel browser to preview the app - sandbox.files.write("/home/user/browse.py", BROWSE_SCRIPT) - result = sandbox.commands.run( - f'python3 /home/user/browse.py "{app_url}"', - timeout=120, - ) - - # Read the preview report - report = json.loads( - sandbox.files.read("/home/user/preview_report.json") - ) - for entry in report: - print(f"Route: {entry['route']} — Title: {entry['title']}") - - finally: - sandbox.kill() - - -if __name__ == "__main__": - main() +sandbox.files.write("/home/user/browse.py", BROWSE_SCRIPT) +result = sandbox.commands.run("python3 /home/user/browse.py", timeout=60) +print(result.stdout) +sandbox.kill() ``` + +
+
+ +--- -## Autonomous agent browsing +## Agent data extraction -Use this approach when you want an LLM to decide what to click, type, and navigate. The [Browser Use](https://docs.browser-use.com/) framework connects an LLM to the Kernel browser — the agent sees the page via screenshots and autonomously completes tasks. +Use [Browser Use](https://docs.browser-use.com/) to let an LLM autonomously browse a website and extract data. The agent sees the page via screenshots and decides what to click, type, and navigate. -This requires an additional LLM API key (Anthropic, OpenAI, or other [supported model](https://docs.browser-use.com/customize/supported-models)): +This requires an additional LLM API key: ```bash .env ANTHROPIC_API_KEY=sk-ant-*** ``` - -Use the `kernel-agent-browser` template, which includes Browser Use on top of the Kernel SDK and Playwright. - -```python + + + +```typescript JavaScript & TypeScript +import { Sandbox } from 'e2b' + +const sandbox = await Sandbox.create('kernel-browser', { + envs: { + KERNEL_API_KEY: process.env.KERNEL_API_KEY!, + ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY!, + }, + timeoutMs: 300_000, +}) +``` +```python Python +import os from e2b import Sandbox sandbox = Sandbox.create( - "kernel-agent-browser", + "kernel-browser", envs={ "KERNEL_API_KEY": os.environ["KERNEL_API_KEY"], "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"], @@ -276,13 +226,15 @@ sandbox = Sandbox.create( timeout=300, ) ``` + -The agent script creates a Kernel browser, connects Browser Use, and runs a task autonomously. You just describe what you want done. +The agent script runs inside the sandbox. It creates a Kernel browser, connects Browser Use, and completes the task autonomously. -```python -AGENT_SCRIPT = ''' + +```typescript JavaScript & TypeScript +const AGENT_SCRIPT = ` import asyncio from kernel import Kernel from browser_use import Agent, Browser, ChatAnthropic @@ -293,178 +245,139 @@ async def main(): browser = Browser(cdp_url=kb.cdp_ws_url) agent = Agent( - task="Go to Hacker News, find the top 3 AI stories, and summarize them", - llm=ChatAnthropic(model="claude-sonnet-4-20250514"), + task="Go to https://news.ycombinator.com, find the top 5 stories, and return their titles and point counts as JSON", + llm=ChatAnthropic(model="claude-sonnet-4"), browser=browser, ) result = await agent.run() print(result) asyncio.run(main()) -''' - -sandbox.files.write("/home/user/agent_task.py", AGENT_SCRIPT) -result = sandbox.commands.run("python3 /home/user/agent_task.py", timeout=180) -print(result.stdout) -``` - - - -### Choosing an LLM - -Browser Use supports multiple LLM providers. Import the one you need and pass the corresponding API key in the sandbox `envs`. - -```python -# Anthropic (recommended) -from browser_use import ChatAnthropic -llm = ChatAnthropic(model="claude-sonnet-4-20250514") +` -# OpenAI -from browser_use import ChatOpenAI -llm = ChatOpenAI(model="gpt-4o") - -# Google -from browser_use import ChatGoogle -llm = ChatGoogle(model="gemini-2.5-flash") +await sandbox.files.write('/home/user/agent_task.py', AGENT_SCRIPT) +const result = await sandbox.commands.run( + 'python3 /home/user/agent_task.py', + { timeoutMs: 180_000 }, +) +console.log(result.stdout) +await sandbox.kill() ``` - -### Full example - -```python agent_browser.py expandable -""" -Agent Remote Browser — E2B + Kernel + Browser Use - -Spins up an E2B sandbox with Browser Use framework and Kernel cloud browser. -An AI agent autonomously browses the web to complete a research task. -""" - -import os - -from e2b import Sandbox - +```python Python AGENT_SCRIPT = ''' import asyncio from kernel import Kernel from browser_use import Agent, Browser, ChatAnthropic async def main(): - # Create a Kernel cloud browser kernel = Kernel() kb = kernel.browsers.create() - print(f"Kernel browser created: {kb.id}") - - # Connect Browser Use to the Kernel browser via CDP browser = Browser(cdp_url=kb.cdp_ws_url) - # Create an AI agent that autonomously browses agent = Agent( - task=""" - Go to https://news.ycombinator.com and find the top 3 stories - that are about AI or machine learning. For each story: - 1. Note the title and point count - 2. Click through to the comments page - 3. Read the top comment - - Return a summary of your findings. - """, - llm=ChatAnthropic(model="claude-sonnet-4-20250514"), + task="Go to https://news.ycombinator.com, find the top 5 stories, and return their titles and point counts as JSON", + llm=ChatAnthropic(model="claude-sonnet-4"), browser=browser, - max_actions_per_step=4, ) - result = await agent.run() - print("\\n" + "=" * 60) - print("AGENT RESULT:") - print("=" * 60) print(result) asyncio.run(main()) ''' +sandbox.files.write("/home/user/agent_task.py", AGENT_SCRIPT) +result = sandbox.commands.run("python3 /home/user/agent_task.py", timeout=180) +print(result.stdout) +sandbox.kill() +``` + +
+
-def main(): - sandbox = Sandbox.create( - "kernel-agent-browser", - envs={ - "KERNEL_API_KEY": os.environ["KERNEL_API_KEY"], - "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"], - }, - timeout=300, - ) +--- - try: - sandbox.files.write("/home/user/agent_task.py", AGENT_SCRIPT) +## Live browser preview - result = sandbox.commands.run( - "python3 /home/user/agent_task.py", - timeout=180, - ) +Kernel provides a live view URL for every browser session — you can watch the browser in real time or embed it in your app. This is useful for debugging, demos, or letting users see what the agent is doing. - if result.exit_code != 0: - print(f"Agent failed: {result.stderr}") - else: - print(result.stdout) + +```typescript JavaScript & TypeScript +import { Sandbox } from 'e2b' - finally: - sandbox.kill() +const sandbox = await Sandbox.create('kernel-browser', { + envs: { KERNEL_API_KEY: process.env.KERNEL_API_KEY! }, + timeoutMs: 300_000, +}) +const LIVE_VIEW_SCRIPT = ` +from kernel import Kernel -if __name__ == "__main__": - main() -``` +kernel = Kernel() +browser = kernel.browsers.create() -## Kernel MCP tools +# Print the live view URL — accessible from any browser +print(browser.browser_live_view_url) +` -The [Kernel MCP Server](https://mcp.onkernel.com/mcp) exposes 10 tools over the [Model Context Protocol](https://modelcontextprotocol.io/) (MCP), letting MCP-compatible clients manage browsers without any SDK installation. +await sandbox.files.write('/home/user/live_view.py', LIVE_VIEW_SCRIPT) +const result = await sandbox.commands.run( + 'python3 /home/user/live_view.py', + { timeoutMs: 30_000 }, +) +const liveViewUrl = result.stdout.trim() +console.log('Watch the browser:', liveViewUrl) + +// Embed in your app as an iframe +// -```bash -kernel mcp install --target # Supports Cursor, Claude Desktop, VS Code, etc. +// Read-only mode (no mouse/keyboard interaction) +// liveViewUrl + '?readOnly=true' ``` +```python Python +import os +from e2b import Sandbox + +sandbox = Sandbox.create( + "kernel-browser", + envs={"KERNEL_API_KEY": os.environ["KERNEL_API_KEY"]}, + timeout=300, +) + +LIVE_VIEW_SCRIPT = ''' +from kernel import Kernel -Or use stdio transport: +kernel = Kernel() +browser = kernel.browsers.create() -```bash -npx -y mcp-remote https://mcp.onkernel.com/mcp -``` +# Print the live view URL — accessible from any browser +print(browser.browser_live_view_url) +''' -Authentication uses OAuth 2.0 via Clerk. - -| Tool | Description | -|---|---| -| `manage_browsers` | Create, list, get, and delete browser sessions (headless/stealth modes, profiles, proxies, viewports, extensions, SSH) | -| `manage_profiles` | Set up, list, and delete browser profiles for persisting cookies and logins | -| `manage_browser_pools` | Create and manage pools of pre-warmed browsers | -| `manage_proxies` | Create and manage proxy configurations (datacenter, ISP, residential, mobile, custom) | -| `manage_extensions` | List and delete uploaded browser extensions | -| `manage_apps` | List apps, invoke actions, get deployments, and retrieve invocation results | -| `computer_action` | Mouse, keyboard, and screenshot controls for browser sessions | -| `execute_playwright_code` | Execute Playwright/TypeScript code with automatic video replay and cleanup | -| `exec_command` | Run shell commands inside a browser VM | -| `search_docs` | Search Kernel platform documentation | - -## Deploying Kernel skills - -An agent running inside the sandbox can use the Kernel CLI to package any browser workflow as a persistent, reusable automation. - -```python -# Create a new skill from a template -sandbox.commands.run("kernel create --name price-checker --language python --template browser-use", cwd="/home/user") -# Deploy it -sandbox.commands.run("kernel deploy main.py --env-file .env", cwd="/home/user/price-checker") -# Invoke it later -sandbox.commands.run('kernel invoke price-checker check --payload \'{"url": "https://example.com"}\'') +sandbox.files.write("/home/user/live_view.py", LIVE_VIEW_SCRIPT) +result = sandbox.commands.run("python3 /home/user/live_view.py", timeout=30) +live_view_url = result.stdout.strip() +print("Watch the browser:", live_view_url) + +# Embed in your app as an iframe +# + +# Read-only mode (no mouse/keyboard interaction) +# live_view_url + '?readOnly=true' ``` + + +The live view URL stays active until the browser is deleted or times out. For more details, see the [Kernel live view documentation](https://www.kernel.sh/docs/browsers/live-view). ## Related guides + + Local browser automation with virtual desktops + Create, manage, and control sandbox lifecycle Run terminal commands inside the sandbox - - Build AI agents that control virtual desktops - From c3c2002e69a79bc155ee4762a8013cdf4a70bac2 Mon Sep 17 00:00:00 2001 From: Tomas Beran Date: Thu, 26 Mar 2026 17:07:30 +0100 Subject: [PATCH 15/16] docs: Address PR review comments on remote browser page Remove unnecessary --break-system-packages flag from pip install commands and clarify that Browser Use comes pre-installed in the kernel-browser template. --- docs/use-cases/remote-browser.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/use-cases/remote-browser.mdx b/docs/use-cases/remote-browser.mdx index fdde2986..306bb9f8 100644 --- a/docs/use-cases/remote-browser.mdx +++ b/docs/use-cases/remote-browser.mdx @@ -76,7 +76,7 @@ const sandbox = await Sandbox.create('kernel-browser', { await sandbox.files.write('/home/user/app.py', FASTAPI_APP) await sandbox.commands.run( - 'pip install --break-system-packages fastapi uvicorn', + 'pip install fastapi uvicorn', { timeoutMs: 60_000 }, ) await sandbox.commands.run( @@ -96,7 +96,7 @@ sandbox = Sandbox.create( sandbox.files.write("/home/user/app.py", FASTAPI_APP) sandbox.commands.run( - "pip install --break-system-packages fastapi uvicorn", + "pip install fastapi uvicorn", timeout=60, ) sandbox.commands.run( @@ -230,7 +230,7 @@ sandbox = Sandbox.create( -The agent script runs inside the sandbox. It creates a Kernel browser, connects Browser Use, and completes the task autonomously. +The agent script runs inside the sandbox. Browser Use and its dependencies come pre-installed in the `kernel-browser` template. The script creates a Kernel browser, connects Browser Use, and completes the task autonomously. ```typescript JavaScript & TypeScript From d103b58d5f0d9ba53aea02dd4a071e72fb78efde Mon Sep 17 00:00:00 2001 From: Tomas Beran Date: Thu, 26 Mar 2026 17:46:32 +0100 Subject: [PATCH 16/16] docs: Add full expandable examples and define FASTAPI_APP Add copy-pasteable full examples using expandable CodeGroups for the screenshot and agent data extraction sections. Define the missing FASTAPI_APP constant in the step-by-step code blocks. --- docs/use-cases/remote-browser.mdx | 284 ++++++++++++++++++++++++++++++ 1 file changed, 284 insertions(+) diff --git a/docs/use-cases/remote-browser.mdx b/docs/use-cases/remote-browser.mdx index 306bb9f8..23282b36 100644 --- a/docs/use-cases/remote-browser.mdx +++ b/docs/use-cases/remote-browser.mdx @@ -69,6 +69,26 @@ Deploy a web app inside an E2B sandbox, get a public URL, then use a Kernel brow ```typescript JavaScript & TypeScript import { Sandbox } from 'e2b' +// A minimal FastAPI app with three routes to screenshot +const FASTAPI_APP = ` +from fastapi import FastAPI +from fastapi.responses import HTMLResponse + +app = FastAPI() + +@app.get("/") +def home(): + return HTMLResponse("

Home

Welcome to the app.

") + +@app.get("/about") +def about(): + return HTMLResponse("

About

About this app.

") + +@app.get("/dashboard") +def dashboard(): + return HTMLResponse("

Dashboard

Your dashboard.

") +` + const sandbox = await Sandbox.create('kernel-browser', { envs: { KERNEL_API_KEY: process.env.KERNEL_API_KEY! }, timeoutMs: 300_000, @@ -88,6 +108,26 @@ await sandbox.commands.run( import os from e2b import Sandbox +# A minimal FastAPI app with three routes to screenshot +FASTAPI_APP = """ +from fastapi import FastAPI +from fastapi.responses import HTMLResponse + +app = FastAPI() + +@app.get("/") +def home(): + return HTMLResponse("

Home

Welcome to the app.

") + +@app.get("/about") +def about(): + return HTMLResponse("

About

About this app.

") + +@app.get("/dashboard") +def dashboard(): + return HTMLResponse("

Dashboard

Your dashboard.

") +""" + sandbox = Sandbox.create( "kernel-browser", envs={"KERNEL_API_KEY": os.environ["KERNEL_API_KEY"]}, @@ -186,6 +226,162 @@ sandbox.kill()
+Full example: + + +```typescript JavaScript & TypeScript expandable +import { Sandbox } from 'e2b' + +// A minimal FastAPI app with three routes to screenshot +const FASTAPI_APP = ` +from fastapi import FastAPI +from fastapi.responses import HTMLResponse + +app = FastAPI() + +@app.get("/") +def home(): + return HTMLResponse("

Home

Welcome to the app.

") + +@app.get("/about") +def about(): + return HTMLResponse("

About

About this app.

") + +@app.get("/dashboard") +def dashboard(): + return HTMLResponse("

Dashboard

Your dashboard.

") +` + +// 1. Create the sandbox and start the app +const sandbox = await Sandbox.create('kernel-browser', { + envs: { KERNEL_API_KEY: process.env.KERNEL_API_KEY! }, + timeoutMs: 300_000, +}) + +await sandbox.files.write('/home/user/app.py', FASTAPI_APP) +await sandbox.commands.run( + 'pip install fastapi uvicorn', + { timeoutMs: 60_000 }, +) +await sandbox.commands.run( + 'uvicorn app:app --host 0.0.0.0 --port 8000', + { background: true, cwd: '/home/user' }, +) + +// 2. Screenshot each route with a Kernel browser +const host = sandbox.getHost(8000) +const appUrl = `https://${host}` + +const BROWSE_SCRIPT = ` +import sys +from kernel import Kernel +from playwright.sync_api import sync_playwright + +app_url = "${appUrl}" +routes = ["/", "/about", "/dashboard"] + +kernel = Kernel() +kb = kernel.browsers.create() + +with sync_playwright() as pw: + browser = pw.chromium.connect_over_cdp(kb.cdp_ws_url) + page = browser.new_page() + page.set_viewport_size({"width": 1280, "height": 720}) + + for route in routes: + page.goto(f"{app_url}{route}", wait_until="networkidle") + name = "home" if route == "/" else route.strip("/") + page.screenshot(path=f"/home/user/{name}.png") + print(f"Captured {route}") + + browser.close() +` + +await sandbox.files.write('/home/user/browse.py', BROWSE_SCRIPT) +const result = await sandbox.commands.run( + 'python3 /home/user/browse.py', + { timeoutMs: 60_000 }, +) +console.log(result.stdout) +await sandbox.kill() +``` +```python Python expandable +import os +from e2b import Sandbox + +# A minimal FastAPI app with three routes to screenshot +FASTAPI_APP = """ +from fastapi import FastAPI +from fastapi.responses import HTMLResponse + +app = FastAPI() + +@app.get("/") +def home(): + return HTMLResponse("

Home

Welcome to the app.

") + +@app.get("/about") +def about(): + return HTMLResponse("

About

About this app.

") + +@app.get("/dashboard") +def dashboard(): + return HTMLResponse("

Dashboard

Your dashboard.

") +""" + +# 1. Create the sandbox and start the app +sandbox = Sandbox.create( + "kernel-browser", + envs={"KERNEL_API_KEY": os.environ["KERNEL_API_KEY"]}, + timeout=300, +) + +sandbox.files.write("/home/user/app.py", FASTAPI_APP) +sandbox.commands.run( + "pip install fastapi uvicorn", + timeout=60, +) +sandbox.commands.run( + "uvicorn app:app --host 0.0.0.0 --port 8000", + background=True, + cwd="/home/user", +) + +# 2. Screenshot each route with a Kernel browser +host = sandbox.get_host(8000) +app_url = f"https://{host}" + +BROWSE_SCRIPT = f''' +from kernel import Kernel +from playwright.sync_api import sync_playwright + +app_url = "{app_url}" +routes = ["/", "/about", "/dashboard"] + +kernel = Kernel() +kb = kernel.browsers.create() + +with sync_playwright() as pw: + browser = pw.chromium.connect_over_cdp(kb.cdp_ws_url) + page = browser.new_page() + page.set_viewport_size({{"width": 1280, "height": 720}}) + + for route in routes: + page.goto(f"{{app_url}}{{route}}", wait_until="networkidle") + name = "home" if route == "/" else route.strip("/") + page.screenshot(path=f"/home/user/{{name}}.png") + print(f"Captured {{route}}") + + browser.close() +''' + +sandbox.files.write("/home/user/browse.py", BROWSE_SCRIPT) +result = sandbox.commands.run("python3 /home/user/browse.py", timeout=60) +print(result.stdout) +sandbox.kill() +``` +
+ --- ## Agent data extraction @@ -294,6 +490,94 @@ sandbox.kill() +Full example: + + +```typescript JavaScript & TypeScript expandable +import { Sandbox } from 'e2b' + +// 1. Create the sandbox with API keys +const sandbox = await Sandbox.create('kernel-browser', { + envs: { + KERNEL_API_KEY: process.env.KERNEL_API_KEY!, + ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY!, + }, + timeoutMs: 300_000, +}) + +// 2. Write and run the Browser Use agent +const AGENT_SCRIPT = ` +import asyncio +from kernel import Kernel +from browser_use import Agent, Browser, ChatAnthropic + +async def main(): + kernel = Kernel() + kb = kernel.browsers.create() + browser = Browser(cdp_url=kb.cdp_ws_url) + + agent = Agent( + task="Go to https://news.ycombinator.com, find the top 5 stories, and return their titles and point counts as JSON", + llm=ChatAnthropic(model="claude-sonnet-4"), + browser=browser, + ) + result = await agent.run() + print(result) + +asyncio.run(main()) +` + +await sandbox.files.write('/home/user/agent_task.py', AGENT_SCRIPT) +const result = await sandbox.commands.run( + 'python3 /home/user/agent_task.py', + { timeoutMs: 180_000 }, +) +console.log(result.stdout) +await sandbox.kill() +``` +```python Python expandable +import os +from e2b import Sandbox + +# 1. Create the sandbox with API keys +sandbox = Sandbox.create( + "kernel-browser", + envs={ + "KERNEL_API_KEY": os.environ["KERNEL_API_KEY"], + "ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"], + }, + timeout=300, +) + +# 2. Write and run the Browser Use agent +AGENT_SCRIPT = ''' +import asyncio +from kernel import Kernel +from browser_use import Agent, Browser, ChatAnthropic + +async def main(): + kernel = Kernel() + kb = kernel.browsers.create() + browser = Browser(cdp_url=kb.cdp_ws_url) + + agent = Agent( + task="Go to https://news.ycombinator.com, find the top 5 stories, and return their titles and point counts as JSON", + llm=ChatAnthropic(model="claude-sonnet-4"), + browser=browser, + ) + result = await agent.run() + print(result) + +asyncio.run(main()) +''' + +sandbox.files.write("/home/user/agent_task.py", AGENT_SCRIPT) +result = sandbox.commands.run("python3 /home/user/agent_task.py", timeout=180) +print(result.stdout) +sandbox.kill() +``` + + --- ## Live browser preview