The Stagehand class is the main entry point for Stagehand v3. It manages browser lifecycle, provides AI-powered automation methods, and handles both local and remote browser environments.
Copy
Ask AI
import { Stagehand } from "@browserbasehq/stagehand";const stagehand = new Stagehand(options);await stagehand.init();
Access pages for browser automation. Pages are accessed through the context.
Copy
Ask AI
// Get the first page (created automatically on init)const page = stagehand.context.pages()[0];// Or get the active pageconst activePage = stagehand.context.activePage();// Create a new pageconst newPage = await stagehand.context.newPage();
import { Stagehand } from "@browserbasehq/stagehand";// Remote browser on Browserbaseconst stagehand = new Stagehand({ env: "BROWSERBASE", apiKey: process.env.BROWSERBASE_API_KEY, projectId: process.env.BROWSERBASE_PROJECT_ID, model: "anthropic/claude-3-5-sonnet-20241022"});await stagehand.init();const page = stagehand.context.pages()[0];await page.goto("https://example.com");const data = await stagehand.extract("get page title", z.object({ title: z.string()}));await stagehand.close();
Copy
Ask AI
import { Stagehand } from "@browserbasehq/stagehand";// Local browserconst stagehand = new Stagehand({ env: "LOCAL", model: "openai/gpt-4o"});await stagehand.init();const page = stagehand.context.pages()[0];// Use the pageawait page.goto("https://example.com");await stagehand.act("click the login button");// Cleanupawait stagehand.close();
Copy
Ask AI
import { Stagehand } from "@browserbasehq/stagehand";const stagehand = new Stagehand({ env: "LOCAL", model: { modelName: "gpt-4o", apiKey: process.env.OPENAI_API_KEY, baseURL: "https://custom-proxy.com/v1" }, systemPrompt: "You are a helpful automation assistant.", verbose: 2, selfHeal: true});await stagehand.init();
Copy
Ask AI
import { Stagehand } from "@browserbasehq/stagehand";const stagehand = new Stagehand({ env: "LOCAL" });await stagehand.init();// Get the first pageconst page1 = stagehand.context.pages()[0];await page1.goto("https://example.com");// Create second pageconst page2 = await stagehand.context.newPage();await page2.goto("https://another-site.com");// Switch active pagestagehand.context.setActivePage(page2);// Now context.activePage() returns page2await stagehand.act("click the button");await stagehand.close();
Copy
Ask AI
import { Stagehand } from "@browserbasehq/stagehand";const stagehand = new Stagehand({ env: "LOCAL", model: "openai/gpt-4o"});await stagehand.init();const page = stagehand.context.pages()[0];await page.goto("https://example.com");await stagehand.act("fill out the form");await stagehand.extract("get form data", schema);// Get usage metricsconst metrics = await stagehand.metrics;console.log("Total tokens used:", metrics.totalPromptTokens + metrics.totalCompletionTokens);console.log("Act operations:", { tokens: metrics.actPromptTokens + metrics.actCompletionTokens, time: metrics.actInferenceTimeMs});await stagehand.close();
Copy
Ask AI
import { Stagehand } from "@browserbasehq/stagehand";const stagehand = new Stagehand({ env: "LOCAL", verbose: 2, logger: (logLine) => { console.log(`[${logLine.category}] ${logLine.message}`); if (logLine.auxiliary) { console.log("Details:", logLine.auxiliary); } }});await stagehand.init();// All operations will now log through your custom logger