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 Playwright, sync_playwright
from browserbase import Browserbase

bb = Browserbase(api_key=BROWSERBASE_API_KEY)


def run(playwright: Playwright) -> None:
    # Create a session on Browserbase
    session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID)

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

    try:
        # Execute Playwright actions on the remote browser tab
        page.goto("https://news.ycombinator.com/")
        page_title = page.title()
        assert (
        page_title == "Hacker News"
        ), f"Page title is not 'Hacker News', it is '{page_title}'"
        page.screenshot(path="screenshot.png")
    finally:
        page.close()
        browser.close()

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

if __name__ == "__main__":
    with sync_playwright() as playwright:
        run(playwright)