Appearance
Fresh 🌱
Stagehand ​
Stagehand is an open source AI browser automation framework. It lets developers choose what to write in code vs. natural language. By integrating with Kernel, you can run Stagehand automations with cloud-hosted browsers.
INFO
This guide is compatible with Stagehand SDK v3. If you're using an earlier version, please refer to the Stagehand migration guide or upgrade to v3.
Adding Kernel to existing Stagehand implementations ​
If you already have a Stagehand (v3) implementation, you can easily switch to using Kernel's cloud browsers by updating your browser configuration.
1. Install the Kernel SDK ​
bash
npm install @onkernel/sdk2. Initialize Kernel and create a browser ​
Import the libraries and create a cloud browser session:
typescript
import { Stagehand } from "@browserbasehq/stagehand";
import Kernel from '@onkernel/sdk';
import { z } from "zod";
const kernel = new Kernel();
const kernelBrowser = await kernel.browsers.create({ stealth: true });
console.log("Live view url: ", kernelBrowser.browser_live_view_url);3. Update your browser configuration ​
Replace your existing browser setup to use Kernel's CDP URL:
typescript
const stagehand = new Stagehand({
env: "LOCAL",
localBrowserLaunchOptions: {
cdpUrl: kernelBrowser.cdp_ws_url,
},
model: "openai/gpt-4.1",
apiKey: process.env.OPENAI_API_KEY,
verbose: 1,
domSettleTimeout: 30_000
});
await stagehand.init();4. Use your Stagehand automation ​
Use Stagehand's page methods with the Kernel-powered browser:
typescript
const page = stagehand.context.pages()[0];
await page.goto("https://onkernel.com");
await stagehand.act("Click on Blog in the navbar");
await stagehand.act("Click on the newest blog post");
const output = await stagehand.extract(
"Extract a summary of the blog post",
z.object({ summary: z.string() })
);
console.log("Newest blog post summary: ", output.summary);
// Clean up
await stagehand.close();
await kernel.browsers.deleteByID(kernelBrowser.session_id);Quick setup with our Stagehand example app ​
Alternatively, you can use our Kernel app template that includes a pre-configured Stagehand integration:
bash
kernel create --name my-stagehand-app --language typescript --template stagehandThen follow the deploy and invoke guides to deploy and run your Stagehand automation on Kernel's infrastructure.
Benefits of using Kernel with Stagehand ​
- No local browser management: Run automations without installing or maintaining browsers locally
- Scalability: Launch multiple browser sessions in parallel
- Stealth mode: Built-in anti-detection features for web scraping
- Session state: Maintain browser state across runs via Profiles
- Live view: Debug your automations with real-time browser viewing
Next steps ​
- Check out live view for debugging your automations
- Learn about stealth mode for avoiding detection
- Learn how to properly terminate browser sessions
- Learn how to deploy your Stagehand app to Kernel