// Basic usage
// Defaults to Browserbase; if no API key is provided, it will default to LOCAL
// Default model is gpt-4o
const stagehand = new Stagehand();
// Custom configuration
const stagehand = new Stagehand({
env: "LOCAL",
// env: "BROWSERBASE", // To run remotely on Browserbase (needs API keys)
verbose: 1,
enableCaching: true,
logger: (logLine: LogLine) => {
console.log(`[${logLine.category}] ${logLine.message}`);
},
// LLM configuration
modelName: "google/gemini-2.0-flash", /* Name of the model to use in "provider/model" format */
modelClientOptions: {
apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY, /* Model API key */
}, /* Configuration options for the model client */
apiKey: process.env.BROWSERBASE_API_KEY,
projectId: process.env.BROWSERBASE_PROJECT_ID,
/* API keys for authentication (if you want to use Browserbase) */
browserbaseSessionID:
undefined, /* Can set Session ID for resuming Browserbase sessions */
browserbaseSessionCreateParams: { /* Browser Session Params */
projectId: process.env.BROWSERBASE_PROJECT_ID!,
proxies: true, /* Using Browserbase's Proxies */
browserSettings: {
advancedStealth: true, /* Only available on Scale Plans */
blockAds: true, /* To Block Ad Popups (defaults to False) */
context: {
id: "<context-id>", /* The Browserbase Context ID. Will be a 36 character uuid. */
persist: true, /* Whether or not to persist the context after browsing. Defaults to false. */
},
viewport: { // Browser Size (ie 1920x1080, 1024x768)
width: 1024,
height: 768,
},
},
},
localBrowserLaunchOptions: {
headless: true, // Launches the browser in headless mode.
executablePath: '/path/to/chrome', // Custom path to the Chrome executable.
args: ['--no-sandbox', '--disable-setuid-sandbox'], // Additional launch arguments.
env: { NODE_ENV: "production" }, // Environment variables for the browser process.
handleSIGHUP: true,
handleSIGINT: true,
handleSIGTERM: true,
ignoreDefaultArgs: false, // or specify an array of arguments to ignore.
proxy: {
server: 'http://proxy.example.com:8080',
bypass: 'localhost',
username: 'user',
password: 'pass'
},
tracesDir: '/path/to/traces', // Directory to store trace files.
userDataDir: '/path/to/user/data', // Custom user data directory.
acceptDownloads: true,
downloadsPath: '/path/to/downloads',
extraHTTPHeaders: { 'User-Agent': 'Custom Agent' },
geolocation: { latitude: 37.7749, longitude: -122.4194, accuracy: 10 },
permissions: ["geolocation", "notifications"],
locale: "en-US",
viewport: { width: 1280, height: 720 },
deviceScaleFactor: 1,
hasTouch: false,
ignoreHTTPSErrors: true,
recordVideo: { dir: '/path/to/videos', size: { width: 1280, height: 720 } },
recordHar: {
path: '/path/to/har.har',
mode: "full",
omitContent: false,
content: "embed",
urlFilter: '.*'
},
chromiumSandbox: true,
devtools: true,
bypassCSP: false,
cdpUrl: 'http://localhost:9222',
preserveUserDataDir: false, // Whether to preserve the user data directory after Stagehand is closed. Defaults to false.
},
});
// Resume existing Browserbase session
const stagehand = new Stagehand({
env: "BROWSERBASE",
browserbaseSessionID: "existing-session-id",
});