1

Get your API Key

Go over the Dashboard’s Settings tab:

Then copy and set the BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID environment variables in your .env file.

2

Install and create a virtual environment with UV

UV is a modern package manager for Python.

uv venv
3

Install Browserbase and Browser Use

uv venv
uv pip install browserbase browser-use
4

Override the default context provided from Browser Use

Browser Use provides a default context that you can override to use Browserbase.

Below is an example of a class that overrides the default context to use Browserbase.

main.py
from dotenv import load_dotenv
import os
import asyncio
from logging import getLogger
from browserbase import Browserbase
from browser_use import Agent, Browser, BrowserConfig
from browser_use.browser.context import BrowserContext, BrowserContextConfig, BrowserSession
from langchain_anthropic import ChatAnthropic

class UseBrowserbaseContext(BrowserContext):
    async def _initialize_session(self):
        """Override to use existing Browserbase page"""

        playwright_browser = await self.browser.get_playwright_browser()
        context = await self._create_context(playwright_browser)
        self._add_new_page_listener(context)

        # Get existing pages from the context
        pages = context.pages

        # Use the existing page from Browserbase
        if not pages:
            # Only create a new page if none exists (shouldn't happen with Browserbase)
            page = await context.new_page()
        else:
            # Use the first existing page
            page = pages[0]

        initial_state = self._get_initial_state(page)
        self.session = BrowserSession(
            context=context,
            current_page=page,
            cached_state=initial_state,
        )
        return self.session
5

Update your Browser Use code to use Browserbase

Below we’ve create a main() function that uses Browserbase to navigate to a website, fill out the form, and submit it.

main.py
async def main():
    load_dotenv()

    # Initialize Browserbase
    bb = Browserbase(api_key=os.environ["BROWSERBASE_API_KEY"])
    session = bb.sessions.create(
        project_id=os.environ["BROWSERBASE_PROJECT_ID"],
    )

    print(f"🅱️  \033[1;32mUsing Browserbase Session!\033[0m Watch live here: \033[1;34mhttps://www.browserbase.com/sessions/{session.id}\033[0m 🅱️")

    # Configure and initialize browser
    browser_config = BrowserConfig(cdp_url=session.connect_url)
    browser = Browser(config=browser_config)

    # Configure browser context
    context_config = BrowserContextConfig(
        wait_for_network_idle_page_load_time=3.0,
        highlight_elements=True,
    )
    context = UseBrowserbaseContext(browser, context_config)
    session = await context.get_session()

    # Set up agent
    llm = ChatAnthropic(
        model_name="claude-3-5-sonnet-20240620",
        temperature=0.0,
        timeout=100,
    )
    agent = Agent(
        task="go to the browserbase docs and list the integrations available",
        llm=llm,
        browser=browser,
        browser_context=context,
    )

    # Run agent and cleanup
    await agent.run()
    await session.current_page.close()
    await browser.close()

if __name__ == "__main__":
    asyncio.run(main())
6

Run your script

Run your script:

uv run main.py

You should see you’re Browserbase session start in Browserbase.