Key-value pairs for variable substitution using %variableName% syntax in your instruction. Variables are not shared with LLM providers, making them ideal for sensitive data like passwords and API keys.Values can be simple primitives (string, number, boolean) or rich objects with an optional description ({ value, description? }).Example:
Copy
Ask AI
// Simple valuesawait stagehand.act("type %password% into the password field", { variables: { password: process.env.USER_PASSWORD }});// Rich values with descriptionsawait stagehand.act("type %password% into the password field", { variables: { password: { value: process.env.USER_PASSWORD, description: "The user's login password" } }});
Iframe and Shadow DOM interactions are supported out of the box. Stagehand automatically handles iframe traversal and shadow DOM elements without requiring additional configuration or flags.
import { Stagehand } from "@browserbasehq/stagehand";// Initialize with Browserbase (API key and project ID from environment variables)// Set BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID in your environmentconst stagehand = new Stagehand({ env: "BROWSERBASE" });await stagehand.init();const page = stagehand.context.pages()[0];await page.goto("https://example.com");// Simple actionawait stagehand.act("click the login button");
Copy
Ask AI
// Variables are NOT shared with LLM providersawait stagehand.act("type %username% into the email field", { variables: { username: "user@example.com" }});await stagehand.act("type %password% into the password field", { variables: { password: process.env.USER_PASSWORD }});await stagehand.act("click the login button");
Copy
Ask AI
// Using string formatawait stagehand.act("choose 'Peach' from the favorite color dropdown", { model: "google/gemini-2.5-flash", timeout: 10000});// Using object format with custom configurationawait stagehand.act("choose 'Peach' from the favorite color dropdown", { model: { modelName: "google/gemini-2.5-flash", apiKey: process.env.GOOGLE_API_KEY, baseURL: "https://custom-api-endpoint.com" }, timeout: 10000});
Copy
Ask AI
// Create multiple pagesconst page1 = stagehand.context.pages()[0];const page2 = await stagehand.context.newPage();// Perform actions on specific pagesawait stagehand.act("click the first link", { page: page1 });await stagehand.act("click the second link", { page: page2 });
Auto-caching is now available in v3. See the caching guide for more details.
Copy
Ask AI
// Observe first to plan the actionconst [action] = await stagehand.observe("click the submit button");// Cache and reuse the actionif (action) { await stagehand.act(action);}// Later, reuse the same cached actionawait stagehand.act(action);