Skip to main content
Modal Sandboxes are secure containers for running code you did not write yourself, such as code an LLM generated. This guide runs that code in a Modal Sandbox and gives it a real browser on Anchor. Every sample is in Python and TypeScript. Tested with the Modal Python SDK 1.7.0 and Anchor Python SDK 1.0.1, and with the Modal JavaScript SDK 0.12.0 and Anchor TypeScript SDK 1.0.0.

What you’re integrating

Anchor SDK in a Modal image → Anchor Cloud. The Sandbox stays small. Chromium runs as a managed Anchor cloud session; the Sandbox holds the SDK and talks to it over HTTPS and CDP.
  • Modal stays the code runtime: install packages, run a script, call a model.
  • Anchor stays the remote browser: stealth, proxies, live view and session lifecycle.
  • Do not launch a browser inside the Sandbox.
Modal’s own computer use example takes the other route: it installs Chromium, a virtual display and a VNC server in the Sandbox image and tunnels the desktop out. That works for local, unprotected pages. With Anchor there is nothing to install or tunnel, the image builds in seconds, the browser gets past bot detection, and you watch it through Anchor’s live view instead of VNC.

Before you start

Install Modal and log in. modal setup writes a token that both the Python and JavaScript SDKs read.
Create an Anchor key at API Keys and store it as a Modal Secret, so it never sits in your code:
Keep the name ANCHORBROWSER_API_KEY. The Python SDK reads it on its own, and the TypeScript samples pass it to the SDK.

Build the image once

Modal recommends a named image for Sandboxes, so a Sandbox never waits on an image build when it starts. Build and publish one that holds the Anchor SDK and the Playwright client:
The Python build prints:
The TypeScript samples use top-level await, so set "type": "module" in your package.json. The same build in Modal’s Logs tab:
Modal logs showing the Anchor SDK and Playwright client installing and the image built in 6.80 seconds
playwright (Python) and playwright-core (Node.js) are only the client library that speaks CDP. You do not run playwright install, because the browser lives on Anchor. That is why the build takes seconds.

Run a web task from the Sandbox

Describe the goal in plain English and let Anchor’s agent drive the browser. The script below starts a Sandbox from the named image, attaches the Secret, runs the task and terminates the Sandbox.
Both print:
You run these with plain python or tsx, not modal run. A Sandbox created from outside Modal only needs an App to belong to, which modal.App.lookup and modal.apps.fromName provide. In the TypeScript version the Sandbox starts in /app, the directory where the image installed the packages, so node can resolve anchorbrowser. Each run shows up under the App’s Sandboxes tab in Modal:
Modal Sandboxes page for the anchor-browser app showing seven Sandboxes created, with lifetime, CPU and memory charts
A web task starts a browser session and leaves it running when the task finishes. End it as the scripts do, with delete_session(result.data.sessionId) in Python or Sessions.deleteSession in TypeScript, or it stays open until Anchor’s idle timeout.

Drive the browser yourself with Playwright

For step-by-step control, create an Anchor session and connect Playwright to it over CDP from inside the Sandbox.
Both print:
session.data.cdp_url and session.data.live_view_url carry a session token (rt). Anyone who has one of them can reach that browser until the session ends. Do not write them to logs you share.

Watch the browser

Open the live view URL in your own browser while the script runs, or open the session in the Anchor dashboard. You see the live browser next to the session details and the task run:
Anchor dashboard session page with the live browser on books.toscrape.com and a successful task run
This is the Anchor session streaming directly, so the Sandbox needs no tunnel, display or VNC server.

Lock the Sandbox to Anchor

By default a Modal Sandbox can reach any public address. When the code inside is untrusted, limit it to the two hosts the Anchor SDK needs with a Modal Outbound Policy:
Running the Playwright script in that Sandbox, followed by a request to another site, prints:
The code can still browse any site, because the page loads happen in Anchor’s browser, not in the Sandbox. It just cannot open its own connections anywhere else. This is the main reason to pair the two: the agent’s code is boxed in, and all web access goes through one browser you can watch and record. live.anchorbrowser.io is opened by the person watching, not by the Sandbox, so it does not need to be on the list.

Clean up two lifecycles

There are two things to end, and ending one does not end the other.
  • The Anchor session. End it in a finally block. Terminating the Sandbox stops the Sandbox only; the cloud browser keeps running and billing until its own timeout.
  • The Modal Sandbox. Call sb.terminate() in a finally block. A Sandbox lives for 5 minutes by default; pass timeout (seconds, Python) or timeoutMs (milliseconds, TypeScript) when you create it for up to 24 hours.
  • Set Anchor’s timeout.max_duration and timeout.idle_timeout, in minutes, as a safety net for a Sandbox that dies mid-run. See Session Timeout.
  • Reuse one Anchor session for a whole agent run instead of creating one per page.
To confirm nothing is left running:

API key

Each customer brings their own Anchor key as a Modal Secret named anchorbrowser. Anchor bills them directly for browser usage; Modal bills for the Sandbox.

Troubleshooting

  • 401 from Anchor inside the Sandbox: the Secret is missing or the variable has a different name. Run modal secret list and check the Secret holds ANCHORBROWSER_API_KEY.
  • The Sandbox waits a long time to start: it is building the image inline. Build and publish the named image first, then load it by name.
  • getaddrinfo ENOTFOUND connect.anchorbrowser.io after adding an Outbound Policy: only api.anchorbrowser.io is on the list. The session is created, but Playwright cannot open the CDP websocket. Add connect.anchorbrowser.io.
  • ERR_REQUIRE_ASYNC_MODULE when running a .ts script: the project is not an ES module. Set "type": "module" in package.json.
  • The Sandbox exits before the task finishes: raise the timeout on the Sandbox and on sb.exec. A web task can take a few minutes.
See the Sandboxes overview and Create a Session.