Skip to main content
If you are working with Python, the official browserbase package is the easiest way to connect and act upon headless browsers running on Browserbase.

Installation

pip install browserbase

Basic usage

Here is an example using the Browserbase Python SDK to create and connect to a session using Playwright:
from playwright.sync_api import sync_playwright
from browserbase import Browserbase
import os

bb = Browserbase(api_key=os.environ["BROWSERBASE_API_KEY"])

# Create a session on Browserbase
session = bb.sessions.create(project_id=os.environ["BROWSERBASE_PROJECT_ID"])

# Connect to the remote session
playwright = sync_playwright().start()
browser = playwright.chromium.connect_over_cdp(session.connect_url)
context = browser.contexts[0]
page = context.pages[0]

page.goto("https://news.ycombinator.com/")
page.close()
browser.close()
playwright.stop()

print(f"Session complete! View replay at https://browserbase.com/sessions/{session.id}")

Session Configuration

When creating a session, you can pass various configuration options to customize the browser behavior.

All Configuration Options

session = bb.sessions.create(
    # Required
    project_id=os.environ["BROWSERBASE_PROJECT_ID"],

    # Proxy configuration
    proxies=True,  # Use default proxy

    # Region where the session runs
    region="us-west-2",

    # Keep session alive after disconnection
    keep_alive=True,

    # Session timeout in seconds (60-21600)
    timeout=3600,

    # Extension ID (uploaded via Upload Extension API)
    extension_id="ext_abc123",

    # Browser settings
    browser_settings={
        # Toggle features
        "blockAds": True,
        "solveCaptchas": True,
        "recordSession": True,
        "logSession": True,
        "advancedStealth": True,

        # Operating system (only available with advancedStealth)
        "os": "windows",

        # Context for session persistence
        "context": {
            "id": "my-context-id",
            "persist": True,
        },

        # Viewport configuration
        "viewport": {
            "width": 1920,
            "height": 1080,
        },

        # Custom captcha selectors
        "captchaImageSelector": "#captcha-image",
        "captchaInputSelector": "#captcha-input",
    },

    # Custom metadata for the session
    user_metadata={
        "userId": "user_123",
        "taskName": "scraping-job",
    },
)

Proxy Configuration

You can configure proxies as a boolean or as a list for more control:
# Using Browserbase managed proxy with geolocation
session = bb.sessions.create(
    project_id=os.environ["BROWSERBASE_PROJECT_ID"],
    proxies=[
        {
            "type": "browserbase",
            "geolocation": {
                "country": "US",
                "state": "CA",
                "city": "San Francisco",
            },
        },
    ],
)

# Using external proxy
session = bb.sessions.create(
    project_id=os.environ["BROWSERBASE_PROJECT_ID"],
    proxies=[
        {
            "type": "external",
            "server": "http://proxy.example.com:8080",
            "username": "user",
            "password": "pass",
            "domainPattern": "*.example.com",
        },
    ],
)

# Multiple proxies with domain patterns
session = bb.sessions.create(
    project_id=os.environ["BROWSERBASE_PROJECT_ID"],
    proxies=[
        {
            "type": "browserbase",
            "geolocation": {"country": "US"},
            "domainPattern": "*.google.com",
        },
        {
            "type": "external",
            "server": "http://proxy.example.com:8080",
            "domainPattern": "*.example.com",
        },
    ],
)

Common Configuration Examples

# Stealth mode with proxy
session = bb.sessions.create(
    project_id=os.environ["BROWSERBASE_PROJECT_ID"],
    proxies=True,
    browser_settings={
        "advancedStealth": True,
        "os": "windows",
    },
)

# Long-running session with keep-alive
session = bb.sessions.create(
    project_id=os.environ["BROWSERBASE_PROJECT_ID"],
    timeout=21600,  # 6 hours
    keep_alive=True,
)

# Session with context persistence
session = bb.sessions.create(
    project_id=os.environ["BROWSERBASE_PROJECT_ID"],
    browser_settings={
        "context": {
            "id": "user-session-context",
            "persist": True,
        },
    },
)

# Custom viewport and ad blocking
session = bb.sessions.create(
    project_id=os.environ["BROWSERBASE_PROJECT_ID"],
    browser_settings={
        "viewport": {"width": 1440, "height": 900},
        "blockAds": True,
    },
)

# Mobile viewport
session = bb.sessions.create(
    project_id=os.environ["BROWSERBASE_PROJECT_ID"],
    browser_settings={
        "viewport": {"width": 390, "height": 844},
    },
)

# Session in specific region with metadata
session = bb.sessions.create(
    project_id=os.environ["BROWSERBASE_PROJECT_ID"],
    region="eu-central-1",
    user_metadata={
        "jobId": "job_456",
        "environment": "production",
    },
)