Skip to main content
1

Get your Browserbase API Key

Go over the Dashboard’s Settings tab:
Then copy your API Key directly from the input and set the BROWSERBASE_API_KEY environment variable.
2

Get your OpenAI API Key

Create an OpenAI account and get your API key from the OpenAI platform.Set the environment variable:
3

Create a new project

Initialize a new Node.js project:
4

Install dependencies

Install the required packages:
5

Create the Stagehand tools

Create src/mastra/tools/index.ts with the web automation tools:
Node.js src/mastra/tools/index.ts
6

Create the browser agent

Create src/mastra/agents/index.ts:
Node.js src/mastra/agents/index.ts
7

Initialize Mastra

Create src/mastra/index.ts:
Node.js src/mastra/index.ts
8

Configure Environment

Create a .env file in the project root:
.env
9

Start the Agent

Add to your package.json:
package.json
Run the development server:
This starts the Mastra interface where you can interact with your web automation agent.

How it works

The integration provides three main tools for your AI agent:
  • Act: Perform actions like clicking buttons or filling forms
  • Observe: Find and identify elements on web pages
  • Extract: Pull structured data from websites

Try these commands

Once your agent is running, try these natural language instructions:
The agent will automatically choose the right tools and execute the web automation tasks.

Example Agent on GitHub

Find the sample project for this integration

Expected output

Running npm run dev opens the Mastra development interface. A successful web-act call returns success: true and a message describing the action; extraction results follow the schema you supplied.

Cleanup

Close any Stagehand and browser sessions owned by your session manager, then stop the development server with Ctrl+C. Release interrupted sessions using Manage browser sessions. Remove the tutorial’s mastra.db file only if you no longer need its stored conversation history.

Next steps

  • Custom instructions: Modify the agent’s behavior in src/mastra/agents/index.ts
  • Add tools: Create additional tools for specific automation needs
  • Production setup: Add proper error handling and logging for production use
You now have a fully functional AI agent that can automate web tasks through natural language! Learn more about advanced features in the Mastra documentation.

Stagehand quickstart

Run browser actions with Stagehand.

Functions quickstart

Deploy a browser workflow as a Function.