Skip to content

Usage

Minimal working example (TypeScript)

The four core primitives: act, extract, observe, and agent.

typescript
import { Stagehand } from "@browserbasehq/stagehand";
import { z } from "zod";
import dotenv from "dotenv";

dotenv.config({ path: ".env" });

const stagehand = new Stagehand({ env: "BROWSERBASE" });

await stagehand.init();

const page = stagehand.context.pages()[0];

// Navigate to a page
await page.goto("https://github.com/browserbase");

// act: natural language action
await stagehand.act("click on the stagehand repo");

// extract: pull structured data using a zod schema
const price = await stagehand.extract(
  "extract the price",
  z.object({ price: z.number() })
);

// observe: see what is interactable on the page
const interactables = await stagehand.observe("What can I click on this page?");

// agent: multi-step autonomous loop
const agent = stagehand.agent();
await agent.execute("Get to the latest PR");

await stagehand.close();

Local execution mode

To run against your local installed Chrome instead of Browserbase, set env: "LOCAL":

typescript
const stagehand = new Stagehand({ env: "LOCAL" });

Local mode does not require a Browserbase API key, but Chrome must be installed.

Key notes

  • act accepts natural language and maps it to DOM interactions. No selectors required.
  • extract requires a Zod schema to type the returned data.
  • observe is useful for building decision trees or debugging what the agent sees.
  • agent runs a full ReAct-style loop until the goal is complete or it gets stuck.
  • Stagehand wraps Playwright under the hood, so raw Playwright page calls are fully available alongside the AI primitives.

Templates and real-world examples

Browserbase maintains a library of ready-to-run automation templates:

https://www.browserbase.com/templates