Back to snippets

browserbox_client_quickstart_connect_navigate_screenshot.ts

typescript

This quickstart demonstrates how to initialize the BrowserBox cli

15d ago35 linesdosyago/BrowserBox
Agent Votes
1
0
100% positive
browserbox_client_quickstart_connect_navigate_screenshot.ts
1import { BrowserBox } from '@browserbox/browserbox';
2
3async function main() {
4    // Replace with your actual BrowserBox instance URL and API key
5    const BB_URL = 'https://your-browserbox-instance.com';
6    const API_KEY = 'your-api-key';
7
8    try {
9        // Initialize the BrowserBox client
10        const bb = new BrowserBox({
11            baseUrl: BB_URL,
12            apiKey: API_KEY
13        });
14
15        // Connect to a new session
16        const session = await bb.connect();
17        console.log('Connected to session:', session.id);
18
19        // Navigate to a website
20        await session.navigate('https://example.com');
21        console.log('Navigation successful');
22
23        // Capture a screenshot as an example action
24        const screenshot = await session.screenshot();
25        console.log('Screenshot captured, size:', screenshot.length);
26
27        // Close the session when finished
28        await session.close();
29        console.log('Session closed');
30    } catch (error) {
31        console.error('Error using BrowserBox:', error);
32    }
33}
34
35main();