Skip to main content

Browser & computer use

Kernel

Give the code in your sandbox a cloud browser. Your script runs in E2B and drives Chromium on Kernel over CDP with Playwright.

Maintained by
E2B
How it works
kernel-browser template, CDP
Package
kernel, @onkernel/sdk
Language
Python, TypeScript

Set it up

Get started

Paste the prompt into your coding agent, or follow the steps yourself.

Paste this into your coding agent

Add E2B sandboxes and Kernel browsers to this project.

  1. 1.Install e2b, the Kernel SDK and Playwright (e2b kernel playwright for Python, e2b @onkernel/sdk playwright-core for TypeScript) using the project's package manager.
  2. 2.The code needs the E2B_API_KEY environment variable. If it isn't set, ask me for my API key from https://console.e2b.dev/?tab=keys. Never hardcode it. Ask me for KERNEL_API_KEY the same way.
  3. 3.Follow the cloud browser guide at https://docs.e2b.dev/use-cases/remote-browser: create a sandbox from the kernel-browser template with KERNEL_API_KEY in its envs, and run a script there that opens a Kernel browser and connects Playwright over CDP.
  4. 4.Run it once and show me the output that came from the sandbox, then kill the sandbox.

Or set it up yourself

  1. 1. Install the SDKs

    pip install e2b kernel playwright
  2. 2. Set your API keys

    E2B_API_KEY=e2b_***
    KERNEL_API_KEY=kernel_***
  3. 3. Start a sandbox and your app

    import os
    from e2b import Sandbox
    
    FASTAPI_APP = """
    from fastapi import FastAPI
    from fastapi.responses import HTMLResponse
    
    app = FastAPI()
    
    @app.get("/")
    def home():
        return HTMLResponse("<h1>Home</h1><p>Welcome to the app.</p>")
    """
    
    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",
    )
  4. 4. Screenshot it with a Kernel browser

    app_url = f"https://{sandbox.get_host(8000)}"
    
    BROWSE_SCRIPT = f'''
    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.goto("{app_url}/", wait_until="networkidle")
        page.screenshot(path="/home/user/home.png")
        print("Captured /")
        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()

Security

What runs in the sandbox

Your app and browsing scripts run in E2B. The browser itself runs on Kernel's infrastructure.

  • CodeYour app and scripts, in a sandbox from the kernel-browser template with the Kernel SDK, Playwright and Browser Use installed.
  • BrowserChromium runs on Kernel, not in the sandbox. Scripts connect to it over CDP.
  • NetworkAny sandbox port gets a public HTTPS URL from get_host, so the Kernel browser can load the app you started.
  • SecretsKERNEL_API_KEY goes into the sandbox through envs. Your E2B key stays on your machine.

3B+ sandboxes started

Give every agent a machine