> ## Documentation Index
> Fetch the complete documentation index at: https://tbd-6fc993ce-mintlify-add-deploy-button-docs-27400.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a Browser

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](/apps/develop) or connected to from another service with the Chrome DevTools Protocol.

## 1. Create a Kernel browser

<Info>
  First, install the Kernel SDK:

  * Typescript/Javascript: `npm install @onkernel/sdk`
  * Python: `pip install kernel`
</Info>

Use our SDK to create a browser:

<CodeGroup>
  ```typescript Typescript/Javascript theme={null}
  import Kernel from '@onkernel/sdk';

  const kernel = new Kernel();

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

  ```python Python theme={null}
  from kernel import Kernel

  kernel = Kernel()

  kernel_browser = kernel.browsers.create()
  print(kernel_browser.session_id)
  ```
</CodeGroup>

## 2. Connect over CDP

Then, you can connect to the browser with any Chrome DevTools Protocol framework, such as Playwright or Puppeteer. You can also use our [Computer Controls API](/browsers/computer-controls) to control the browser's mouse and keyboard without using a CDP connection. This is useful for vision-based LLM loops like [Claude Computer Use](/integrations/computer-use/anthropic).

<CodeGroup>
  ```typescript Typescript/Javascript theme={null}
  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
  });
  ```

  ```python Python theme={null}
  from playwright.async_api import async_playwright

  async with async_playwright() as playwright:
      browser = await playwright.chromium.connect_over_cdp(kernel_browser.cdp_ws_url)
  ```
</CodeGroup>

## 3. Tear it down

When you're finished with the browser, you can delete it:

<CodeGroup>
  ```typescript Typescript/Javascript theme={null}
  import Kernel from '@onkernel/sdk';

  const kernel = new Kernel();

  await kernel.browsers.deleteByID(kernelBrowser.session_id);
  ```

  ```python Python theme={null}
  from kernel import Kernel

  kernel = Kernel()
  await kernel.browsers.delete_by_id(kernel_browser.session_id)
  ```
</CodeGroup>

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](/browsers/termination#automatic-deletion-via-timeout) when creating the browser.

## Full example

Once you've connected to the Kernel browser, you can do anything with it.

<Info>
  Kernel browsers launch with a default context and page. Make sure to access
  the [existing context and
  page](https://playwright.dev/docs/api/class-browsertype#browser-type-connect-over-cdp)
  (`contexts()[0]` and `pages()[0]`), rather than trying to create a new one.
</Info>

<CodeGroup>
  ```typescript Typescript/Javascript theme={null}
  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);
  }
  ```

  ```python Python theme={null}
  from kernel import Kernel
  from playwright.async_api import async_playwright

  kernel = Kernel()

  kernel_browser = kernel.browsers.create()

  async with async_playwright() as playwright:
      browser = await playwright.chromium.connect_over_cdp(kernel_browser.cdp_ws_url)

      try:
          context = browser.contexts[0] if browser.contexts else await browser.new_context()
          page = context.pages[0] if context.pages else await context.new_page()
          await page.goto('https://www.onkernel.com')
          title = await page.title()
      except Exception as e:
          print(e)
      finally:
          await browser.close()
          await kernel.browsers.delete_by_id(kernel_browser.session_id)
  ```
</CodeGroup>
