Best practices for tasks combining browser automation, code execution, and file handling: create a template from the all-in-one image and complete page visits, screenshots, downloads, data cleaning, and result export in one sandbox.
AIO Sandbox is designed for agent tasks where "browser automation + code execution + file handling" happen in sequence. Create a template based on the all-in-one image (
The following example uses Node.js to connect to the browser inside the sandbox and run Python code in the same sandbox. The business side only needs the SDK and Playwright Core; the browser runs inside the sandbox.
The example flow:
all-in-one): port 3000 provides the browser service and port 5000 provides the Code Interpreter/envd service. An agent can complete page visits, screenshots, downloads, data cleaning, code computation, and result export in the same isolated environment.
If a task only needs page visits, button clicks, screenshots, or file downloads, prefer the Browser Use Sandbox. Choose AIO Sandbox only when browser artifacts must be handed to Python/Node.js immediately for further processing.
Use Cases
| Scenario | Description |
|---|---|
| Scrape then analyze | Visit dynamic pages with the browser, save HTML, screenshots, or downloads, then clean, aggregate, and export with Python |
| Automated test reports | Run browser E2E tests, then analyze logs, screenshots, and test results in the same sandbox |
| Content-production agents | Generate page screenshots, PDFs, tables, JSON, and archive files |
| Multi-tool agents | Combine the browser, terminal commands, Code Interpreter, and file APIs in one session |
Recommended Flow
- Create a template based on the all-in-one image (
all-in-one) and obtain itstemplateCode—see Template Management. - Create a sandbox from the template with a generous timeout. Browser cold start, page loading, and code execution all consume time.
- Get the public host of the browser service via
sandbox.getHost(3000). - Poll
/healthuntil the browser service is ready. - Connect Playwright or Puppeteer through the CDP endpoint to visit pages, interact, screenshot, and download.
- Write browser artifacts to the sandbox filesystem, then continue processing them through the Code Interpreter or commands.
- Download result files and record URLs, action summaries, screenshot paths, script versions, and outputs.
- Destroy the sandbox when the task ends, cleaning up temporary credentials and intermediate files.
Prepare the Local Environment
The following example uses Node.js to connect to the browser inside the sandbox and run Python code in the same sandbox. The business side only needs the SDK and Playwright Core; the browser runs inside the sandbox.
e2b and @e2b/code-interpreter are pinned to the versions verified in these examples (2.31.0 / 2.6.1). Newer SDK versions return 405 when creating a sandbox.Example Code
The example flow:
- Create a sandbox.
- Wait until the browser service
/healthreturns 200. - Connect to the cloud browser over CDP, visit
https://example.com, and write the screenshot to a task directory inside the sandbox. - Use the Code Interpreter to read the browser artifacts and produce a structured summary.
Integration Advice
- Use a fixed task directory such as
/tmp/aio-task/<task-id>and keep screenshots, HTML, downloads, scripts, and result files together. - Log inputs, outputs, logs, and errors separately for the browser phase and the code phase. When troubleshooting, first determine whether the page operation or the subsequent script failed.
- Validate the type, size, and path of downloaded files before passing them to the code phase.
- Web content and downloaded files may carry prompt injection. Do not treat page text directly as system instructions or high-privilege tool instructions.
- Inject browser accounts, cookies, model keys, and business credentials at runtime; never bake them into the template image.
- Set a maximum page count, maximum download size, command timeouts, and sandbox lifetime so an agent cannot crawl or compute without bound.
- To watch the browser live, use the noVNC page built into the image:
https://<sandbox-host>/static/vnc.html?path=/ws/livestream&autoconnect=true. Thepathparameter is mandatory and must start with/—a relativews/livestreamresolves to/static/ws/livestreamand fails; the root path/vnc.htmlreturns 404. WebSocket clients can connect directly towss://<sandbox-host>/ws/livestream(equivalent alias/ws/liveview).
Next Steps
- Using Browser Use Sandbox: Best practices for pure web automation tasks
- Template Management: Create a template from the all-in-one image
- Manage and Use Instances: Instance lifecycle and data-plane operations