What You'll Build
Before you start
Ensure you have these requirements ready:Node.js 22.18+
MongoDB
API Access
Step 1: Project setup
Clone and install
What gets installed?
What gets installed?
Core packages:
@browserbasehq/stagehand- AI-powered data extractionmongodb- MongoDB driver for data storagezod- Schema validation for type safety
chalk&boxen- Terminal styling and output formattingplaywright- Browser automation engine
Step 2: Start MongoDB
If using local MongoDB, ensure it’s running:MongoDB Atlas users can skip this step as the database is already hosted in the cloud.
Step 3: Configuration
Environment variables
Create your.env file with the required configuration:
Step 4: Configure Stagehand
The integration uses Browserbase cloud browsers:Node.js stagehand.ts
Step 5: Run your first extraction
What happens when you run the agent:- Connects to MongoDB and creates necessary collections
- Navigates to Amazon laptop category
- Extracts product listings with AI-powered data extraction
- Extracts detailed information for the first 3 products
- Stores all data in MongoDB with schema validation
- Runs analysis queries and displays results
Execute the extraction
Customization options
Extend data schema
Add custom fields to capture more product information:Node.js
Custom extraction instructions
Modify the AI extraction to capture specific data:Node.js
Expected output
The extraction stores product records in the configured MongoDB database and prints analysis results. Inspect the stored documents to confirm that the fields match your schema; product values and availability vary by run.Cleanup
Confirm the browser session ended, and release it from the Sessions dashboard if necessary. Remove only the tutorial records fromscraper_db when you no longer need them. If you started a local mongod process for this tutorial, stop that process with Ctrl+C; leave shared databases running.
Need help? Join the Stagehand Slack community for support and to share your projects!