import { Stagehand } from '@browserbasehq/stagehand';
import BrowsercashSDK from '@browsercash/sdk';
async function run() {
const client = new BrowsercashSDK({
apiKey: process.env.BROWSER_API_KEY!,
});
// Create Browser Cash session
const session = await client.browser.session.create();
// Initialize Stagehand with Browser Cash CDP URL
const stagehand = new Stagehand({
env: 'LOCAL',
localBrowserLaunchOptions: {
cdpUrl: session.cdpUrl,
},
});
await stagehand.init();
// Use natural language to interact with pages
await stagehand.page.goto('https://example.com');
await stagehand.act({ action: 'click on the login button' });
await stagehand.act({ action: 'type "[email protected]" in the email field' });
await stagehand.act({ action: 'type "password123" in the password field' });
await stagehand.act({ action: 'click submit' });
// Extract data using natural language
const data = await stagehand.extract({
instruction: 'Extract the user profile information',
schema: {
type: 'object',
properties: {
name: { type: 'string' },
email: { type: 'string' },
},
},
});
console.log(data);
// Cleanup
await stagehand.close();
await client.browser.session.stop({ sessionId: session.sessionId });
}
run().catch(console.error);