Skip to main content
Stagehand supports two primary environments:
  • Browserbase - Cloud-managed browser infrastructure optimized for production web automation at scale
  • Local - Run browsers directly on your machine for development and debugging

Browserbase Environment

Browserbase provides managed cloud browser infrastructure optimized for web automation at scale. It offers advanced features like stealth mode, proxy support, and persistent contexts.

Browserbase

Discover the power of cloud-managed browser infrastructure with Browserbase.

Multi-Region Support

Stagehand API is available in multiple regions to optimize latency and support data residency requirements. The SDK automatically routes requests to the correct regional API endpoint based on your browser session’s region. Configure your browser session region in browserbaseSessionCreateParams:
The API endpoint must match your browser session region. If there’s a mismatch, you’ll receive an error: Session is in region 'X' but this API instance serves 'Y'. Please route your request to the X Stagehand API endpoint.

Disabling Stagehand API

If you want to use Stagehand purely as a local library without routing through the Stagehand API, you can disable API mode:
Disabling the API is useful when you want to manage browser sessions directly while still using Stagehand’s automation features locally.

Environment Variables

Before getting started, set up the required environment variables:
Get your API key from the Browserbase Dashboard

Using Stagehand with Browserbase

Basic Setup

The simplest way to get started is with default settings:

Advanced Configuration

Configure browser settings, proxy support, and other session parameters:

Alternative: Browserbase SDK

If you prefer to manage sessions directly, you can use the Browserbase SDK:

Connecting to an Existing Session

Connect to a previously created Browserbase session using its session ID:

Local Environment

The local environment runs browsers directly on your machine, providing full control over browser instances and configurations. Ideal for development, debugging, and scenarios requiring custom browser setups.

Environment Comparison

Basic Local Setup

Advanced Local Configuration

Customize browser launch options for local development:

Default Local Launch Arguments

When Stagehand launches a local browser, it adds the following Chrome arguments before any values in localBrowserLaunchOptions.args:
Chrome Launcher also adds its own default arguments. To remove default arguments, set localBrowserLaunchOptions.ignoreDefaultArgs:
Use ignoreDefaultArgs: true to remove all default arguments. Use a string array to remove only exact matches while preserving the rest.

Advanced Configuration

Keep Alive

The keepAlive option controls whether the browser remains running after stagehand.close() is called or when the parent process exits unexpectedly (e.g., crash, SIGTERM, SIGINT). By default, Stagehand terminates the browser and cleans up all resources when it shuts down. Setting keepAlive: true keeps the browser running independently so you can reconnect to it later.

Behavior by Environment

Local Environment

When running locally with keepAlive: true, the Chrome process is detached from the Node.js event loop, allowing your script to exit while the browser stays open. This is useful for debugging or for handing off a browser session to another process.

Browserbase Environment

On Browserbase, keepAlive: true keeps the cloud session active so you can reconnect later using browserbaseSessionID. This is useful for long-running workflows that span multiple script executions.
The top-level keepAlive option overrides browserbaseSessionCreateParams.keepAlive when both are provided.

Fixed CDP Debugging Port

Specify a fixed Chrome DevTools Protocol (CDP) debugging port instead of using a randomly assigned one.
If no port is specified, a random port will be assigned.

DOM Settle Timeout

Configure how long Stagehand waits for the DOM to stabilize before taking actions.

What is DOM Settling?

DOM settling ensures that:
  • Animations complete before interacting with elements
  • Lazy-loaded content has time to appear
  • JavaScript updates finish before actions are taken
  • Dynamic content is fully rendered

When to Adjust

Increase domSettleTimeout for pages with:
  • Heavy animations or transitions
  • Lazy-loading or infinite scroll
  • Dynamic JavaScript frameworks (React, Vue, Angular)
  • Complex single-page applications
Setting domSettleTimeout too low may cause actions to fail on elements that aren’t ready. Setting it too high increases execution time unnecessarily.

Troubleshooting

  • Verify your BROWSERBASE_API_KEY is set correctly
  • Check that your API key has the necessary permissions
  • Ensure your Browserbase account has sufficient credits
  • Install Chrome or Chromium on your system
  • Set the correct executablePath for your Chrome installation
  • Check that required dependencies are installed (Linux: libnss3-dev libatk-bridge2.0-dev libgtk-3-dev libxss1 libasound2)
  • Increase session timeout in browserbaseSessionCreateParams.timeout
  • Use keepAlive: true for long-running sessions
  • Monitor session usage to avoid unexpected terminations