1

Get your API Key

Go over the Dashboard’s Settings tab:

Copy your API Key and Project ID from the input fields, then update your .env file by adding entries for BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID.

Alternatively, you can temporarily set the environment variables for a single bash command by prefixing it with BROWSERBASE_API_KEY=<your_api_key> BROWSERBASE_PROJECT_ID=<your_project_id> in your terminal.

2

Install Selenium

npm i selenium-webdriver
3

Update your code or clone a template

Running your existing code with Browserbase only requires a few line changes:

import http from "http";
import webdriver from "selenium-webdriver";
import chrome from "selenium-webdriver/chrome";

async function createSession() {
  const response = await fetch(`https://www.browserbase.com/v1/sessions`, {
    method: 'POST',
    headers: {
      "x-bb-api-key": process.env.BROWSERBASE_API_KEY as string,
      "Content-Type": "application/json"
    },
    body: JSON.stringify({ projectId: process.env.BROWSERBASE_PROJECT_ID })
  });
  return await response.json();
}


(async () => {
  const session = await createSession();

  const customHttpAgent = new http.Agent({});
  (customHttpAgent as any).addRequest = (req: any, options: any) => {
    // Session ID needs to be set here
    req.setHeader("session-id", session.id);
    req.setHeader("x-bb-api-key", process.env.BROWSERBASE_API_KEY);
    (http.Agent.prototype as any).addRequest.call(customHttpAgent, req, options);
  };

  // We set a debuggerAddress so the server-side WebDriver can connect.
  const options = new chrome.Options();
  options.debuggerAddress("localhost:9223");

  const driver = new webdriver.Builder()
    .forBrowser("chrome")
    .setChromeOptions(options)
    .usingHttpAgent(customHttpAgent)
    .usingServer(
      `http://connect.browserbase.com/webdriver` // Selenium only supports HTTP
    )
    .build();

  await driver.get("https://www.browserbase.com");

  // Make sure to quit the driver so your session is ended!
  await driver.quit();
})().catch((error) => console.error(error.message));
Be sure to set your BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID environment variables to the values you copied from the Dashboard.

Get started with a Selenium template

Clone this GitHub repo to get started with Selenium


4

Inspect the completed Session

You can find all the recent sessions on the Dashboard’s Overview, along with essential metrics:

Select your Session to inspect it with the Session Inspector.

Start building