Skip to main content
Kernel browsers were designed to be lightweight, fast, and efficient for cloud-based browser automations at scale. They can be used as part of the Kernel app platform or connected to from another service with the Chrome DevTools Protocol.

1. Create a Kernel browser

First, install the Kernel SDK:
  • Typescript/Javascript: npm install @onkernel/sdk
  • Python: pip install kernel
Use our SDK to create a browser:
import Kernel from '@onkernel/sdk';

const kernel = new Kernel();

const kernelBrowser = await kernel.browsers.create();
console.log(kernelBrowser.session_id);

2. Connect over CDP

Then, you can connect to the browser with any Chrome DevTools Protocol framework, such as Playwright or Puppeteer.
import { chromium } from 'playwright';

// Playwright
const browser = await chromium.connectOverCDP(kernelBrowser.cdp_ws_url);

// Or with Puppeteer
import puppeteer from 'puppeteer';

const browser = await puppeteer.connect({
  browserWSEndpoint: kernelBrowser.cdp_ws_url,
  defaultViewport: null, // Optional: inherit viewport from the browser
});

3. Tear it down

When you’re finished with the browser, you can delete it:
import Kernel from '@onkernel/sdk';

const kernel = new Kernel();

await kernel.browsers.deleteByID(kernelBrowser.session_id);
Non-persisted browsers automatically delete after a timeout (default 60 seconds) if they don’t receive a CDP or live view connection. You can configure this timeout when creating the browser.

Full example

Once you’ve connected to the Kernel browser, you can do anything with it.
Kernel browsers launch with a default context and page. Make sure to access the existing context and page (contexts()[0] and pages()[0]), rather than trying to create a new one.
import Kernel from '@onkernel/sdk';
import { chromium } from 'playwright';

const kernel = new Kernel();

const kernelBrowser = await kernel.browsers.create();
const browser = await chromium.connectOverCDP(kernelBrowser.cdp_ws_url);

try {
  const context = browser.contexts()[0] || (await browser.newContext());
  const page = context.pages()[0] || (await context.newPage());
  await page.goto('https://www.onkernel.com');
  const title = await page.title();
} catch (error) {
  console.error(error);
} finally {
  await browser.close();
  await kernel.browsers.deleteByID(kernelBrowser.session_id);
}

Connection notes

  • CDP connections are meant to be long-lived but may eventually close. Websocket connections typically can remain active for up to 1 hour, after which they may close automatically. Browser sessions themselves are unaffected—reconnect to the same cdp_ws_url to continue using the browser.
  • Browsers persist independently of CDP. Depending on your browser persistence or timeout configuration, it will continue running even if the CDP connection closes. You can reconnect to the same cdp_ws_url if you’re unexpectedly disconnected.
  • We recommend implementing reconnect logic, as network interruptions or lifecycle events can cause CDP sessions to close. Detect disconnects and automatically re-establish a CDP connection when this occurs.