Initialize Stagehand with your preferred configuration:
Copy
Ask AI
import { Stagehand } from "@browserbasehq/stagehand";// For local developmentconst stagehand = new Stagehand({ env: "LOCAL", verbose: 2, enableCaching: false,});// For production with Browserbaseconst stagehand = new Stagehand({ env: "BROWSERBASE", verbose: 1, enableCaching: true,});
// Navigate to a websiteconst navigateTool = stagehandToolkit.tools.find( (t) => t.name === "stagehand_navigate");await navigateTool.invoke("https://www.google.com");// Perform an actionconst actionTool = stagehandToolkit.tools.find( (t) => t.name === "stagehand_act");await actionTool.invoke('Search for "OpenAI"');// Observe the pageconst observeTool = stagehandToolkit.tools.find( (t) => t.name === "stagehand_observe");const result = await observeTool.invoke( "What actions can be performed on the current page?");console.log(JSON.parse(result));
Integrate with LangGraph for complex automation workflows:
Copy
Ask AI
npm install @langchain/langgraph @langchain/community @langchain/core @langchain/openai @browserbasehq/stagehandimport { createReactAgent } from "@langchain/langgraph/prebuilt";// Create an LLMconst llm = new ChatOpenAI({ model: "gpt-4", temperature: 0,});// Create an agent with Stagehand toolsconst agent = createReactAgent({ llm, tools: stagehandToolkit.tools,});// Execute a complex workflowconst result = await agent.invoke({ messages: [ { role: "user", content: "Go to example.com, find the contact form, and extract all the form fields" } ]});