Using a Remote Browser

Initialize a Browser

Initialize a Browser

In this guide, we will initialize a Bytebot-managed remote browser. If you want to use a local Puppeteer instance instead, click here.

Initialize Bytebot

To start, you will need to import the Bytebot SDK. If you haven’t installed Bytebot yet, please refer to our installation guide.

1import { BytebotClient } from '@bytebot/sdk';

The BytebotClient includes all of the functions needed to start a browser session and create any browser actions. Its constructor requires an API key to be successfully initialized.

1import { BytebotClient } from '@bytebot/sdk';
2
3const bytebot = new BytebotClient({
4 apiKey: process.env.BYTEBOT_API_KEY,
5});

If you are using a .env, you may need to install dotenv to import it.

1import { BytebotClient } from '@bytebot/sdk';
2import 'dotenv/config'
3
4const bytebot = new BytebotClient({
5 apiKey: process.env.BYTEBOT_API_KEY,
6});

Start a Session

Before generating and executing a browser action, you will need to start a browser session. To start a browser session, use BytebotClient.browser.startSession(url: string) , where the url is the initial page on the browser.

If successful, the function will return an object with a sessionId attribute.

1import { BytebotClient } from '@bytebot/sdk';
2import 'dotenv/config'
3
4const bytebot = new BytebotClient({
5 apiKey: process.env.BYTEBOT_API_KEY,
6});
7
8async function run() {
9 const browser = await bytebot.browser.startSession(
10 "https://developer.chrome.com/"
11 );
12
13 if (browser.sessionId) {
14 //success!
15 }
16}
17
18run().catch(console.error);

Because createSession is initializing a complete browser instance in a virtual environment, it can take 10s to 30s to complete.

End a Session

Once you are done with your browser, you should end your session to destroy the browser. To end a browser session, use BytebotClient.endSession(sessionId: string) , where the sessionId is a string of the session’s identifier.

1import { Table, Column, Attribute, Text, BytebotClient } from "./src/index.ts";
2import "dotenv/config";
3
4const bytebot = new BytebotClient({
5 apiKey: process.env.BYTEBOT_API_KEY,
6});
7
8async function run() {
9 const browser = await bytebot.browser.startSession(
10 "https://www.ycombinator.com/companies"
11 );
12
13 if (browser.sessionId) {
14 //logic
15 bytebot.browser.endSession(browser.sessionId);
16 }
17}
18
19run().catch(console.error);

When successful, the function will return an object with a sessionId of the closed browser or an error represented as a string.

With the basics down, you are now ready to create actions and extract data.