How to Get Started with 0Browser, a Headless Browser as a Service

Get Started

Let's see how easy it is to get started with 0Browser and Puppeteer.

In this example we will learn how to take a screenshot of a web page using JavaScript, Puppeteer and 0Browser. But first, let's make sure you have everything ready on your setup!

1. Prerequisites

1.1. Node.js

Before we start lets make sure you have Node.js installed. You can download and install Node from NodeJS.org website.

1.2. Initialize npm

If you haven't done so already go a head and create package.json file used by npm. Once initialized you will be able to add more dependencies to your project. You can find more information on this here.

> npm init -y
# reply yes to all default prompt questions.

1.3. Puppeteer

let's add Puppeteer by running the following command.

> npm i puppeteer-core -save

1.4. API Key

You will need your 0Browser API key from the 0Browser Account page.
Here is a tutorial on how to fetch your 0Browser API key.

2.Setup

Now that we have the prerequisites in order, let's get to the fun part!

Use your favorite IDE to create a new JS file called index.js.
Use Puppeteer to connect to 0Browser proxy endpoint at wss://proxy.0browser.com?token={YOUR-API-KEY}
Following code snippet shows how to connect to 0Browser and capture a screenshot of example.com web page.

const puppeteer = require('puppeteer-core');

(async () => {
    const browser = await puppeteer.connect({
        browserWSEndpoint: 
        'wss://proxy.0browser.com?token={YOUR-API-KEY}&timeout=60000',
    });
    const page = await browser.newPage();
    await page.goto('https://example.com');
    await page.screenshot({ path: 'capture.png' });

    await browser.close();
})();

Note that we are using the secure wss connection over ws. Find this source code on Github


TimeOut

Default connection timeout is 30000 mili-seconds (30 seconds). We can change that by passing in a value using timeout param such as 60000 mili-seconds (60 seconds) in the sample above.

3.Run

Use NodeJS to run the script above using below command.

 > node index.js

Thats it, Great job!


Next let's check out how we can fetch 0Browser API key from the account page
...
0Browser