# Browser sandboxes

A browser sandbox is a headless Chromium that kosa8 Cloud runs for you on Cloudflare Browser Run. You drive it over the Chrome DevTools Protocol (CDP) through `api.kosa8.com`: open one, get its `cdp_url`, and hand that to Playwright, Puppeteer or any other CDP client. Your agent gets a real browser without one inside its sandbox, and nothing on Cloudflare's side is ever exposed to you or to it.

## Open a browser

```
curl -s -X POST https://api.kosa8.com/v1/browsers \
  -H "Authorization: Bearer $KOSA8_API_KEY" -H 'Content-Type: application/json' \
  -d '{"ttl_s":600,"name":"scrape"}'
# 201 {"browser":{"id":"brw_…","name":"scrape","sandbox_id":null,"state":"open","ttl_s":600,
#   "created_at":…,"expires_at":…,"closed_at":null,"close_reason":null,"seconds":0,
#   "cdp_url":"wss://api.kosa8.com/v1/browsers/brw_…/cdp?token=…","cdp_url_expires_at":…},
#   "price_per_hour_usd":…}
```

- **ttl_s**: how long it lives, 30 to 3600 seconds (default 300). At its end it is closed. It also closes sooner when idle: after `min(ttl_s, 20 minutes)` with no CDP traffic.

- **sandbox_id** (optional): attach it to one of your sandboxes. It closes when that sandbox is deleted.

- **name** (optional): 1 to 64 letters, digits, `.`, `_` or `-`.

`cdp_url` is a `wss://` address any CDP client opens as it is. The token in it is short-lived (at most 10 minutes, and never past the browser's end) and names this one browser. `GET /v1/browsers/{id}` hands out a fresh one whenever you need to connect again.

## List, get and close

```
curl -s https://api.kosa8.com/v1/browsers -H "Authorization: Bearer $KOSA8_API_KEY"
# {"browsers":[…],"enabled":true,"limit":3,"price_per_hour_usd":…}   ?state=all for closed ones too
curl -s https://api.kosa8.com/v1/browsers/brw_… -H "Authorization: Bearer $KOSA8_API_KEY"   # a fresh cdp_url
curl -s -X DELETE https://api.kosa8.com/v1/browsers/brw_… -H "Authorization: Bearer $KOSA8_API_KEY"
# {"ok":true,"id":"brw_…"}
```

A browser has `state`, `created_at`, `expires_at`, and once closed `closed_at` and `close_reason`. `seconds` is how long it has been billed. Close it as soon as you are done: billing stops at once.

## Playwright

```
import { chromium } from "playwright";

const r = await fetch("https://api.kosa8.com/v1/browsers", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.KOSA8_API_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({ ttl_s: 300 }),
});
const { browser: b } = await r.json();

const browser = await chromium.connectOverCDP(b.cdp_url);
const page = browser.contexts()[0]?.pages()[0] ?? await browser.newPage();
await page.goto("https://example.com");
console.log(await page.title());
await page.screenshot({ path: "example.png" });
await browser.close();

await fetch(`https://api.kosa8.com/v1/browsers/${b.id}`, {
  method: "DELETE", headers: { Authorization: `Bearer ${process.env.KOSA8_API_KEY}` },
});
```

In Python: `browser = await playwright.chromium.connect_over_cdp(b["cdp_url"])`.

## Puppeteer

```
import puppeteer from "puppeteer-core";

const browser = await puppeteer.connect({ browserWSEndpoint: b.cdp_url });
const page = await browser.newPage();
await page.goto("https://example.com");
await page.screenshot({ path: "example.png" });
await browser.disconnect();
```

## One page, no CDP client: `/page`

For an agent that only wants a page, kosa8 can drive the browser for you. It opens the URL in a new tab, waits for it to load (up to 15 seconds), answers, and closes the tab.

```
curl -s -X POST https://api.kosa8.com/v1/browsers/brw_…/page \
  -H "Authorization: Bearer $KOSA8_API_KEY" -H 'Content-Type: application/json' \
  -d '{"url":"https://example.com","screenshot":true,"text":true}'
# {"browser_id":"brw_…","page":{"url":"https://example.com/","title":"Example Domain",
#   "text":"…","screenshot_png_base64":"iVBORw0…"}}
```

`screenshot`, `text` (the page's text, up to 20,000 characters) and `full_page` (the whole page, not just the viewport) are each optional. A screenshot too large to return comes back as `screenshot_note` instead; connect over `cdp_url` for that page.

## From the CLI

```
$ kosa8 cloud browser create --ttl 600 --name scrape
Browser brw_… is open until 2026-11-22 14:10 (… per browser-hour, billed per second).
cdp_url: wss://api.kosa8.com/v1/browsers/brw_…/cdp?token=…
Playwright: await chromium.connectOverCDP(cdp_url)   (a fresh URL: kosa8 cloud browser get brw_…)
$ kosa8 cloud browser screenshot brw_… https://example.com -o example.png
Saved example.png (45526 bytes): https://example.com/ "Example Domain"
$ kosa8 cloud browser ls            # --all for closed ones too
$ kosa8 cloud browser get brw_…     # a fresh cdp_url
$ kosa8 cloud browser rm brw_…
```

`--sandbox sbx_…` attaches it to a sandbox, and `--json` prints the API's answer. See [`kosa8 cloud browser`](https://docs.kosa8.com/reference/cloud-browser/).

## From the SDKs

**Not in the published SDKs yet.** Python `kosa8` 0.5.5 and `@kosa8/sdk` 0.5.3 have no browser helper: call the REST routes above (as in the Playwright example). The next Python and JavaScript releases add one that closes the browser for you on the way out:

```
# Python (next release, not in 0.5.5)
from kosa8 import Kosa8Cloud
cloud = Kosa8Cloud()                      # KOSA8_API_KEY from the environment
with cloud.browser(ttl_s=300) as b:
    png = b.screenshot("https://example.com")
    # or drive it: playwright.chromium.connect_over_cdp(b.cdp_url)
```

```
// JavaScript / TypeScript (next release, not in 0.5.3)
import { Kosa8Cloud } from "@kosa8/sdk";
const cloud = new Kosa8Cloud();
await using b = await cloud.browser({ ttlS: 300 });
const png = await b.screenshot("https://example.com");
const pw = await chromium.connectOverCDP(b.cdpUrl);
```

## MCP tools

The [kosa8 Cloud MCP server](https://docs.kosa8.com/guides/cloud-mcp/) gives an agent five tools, so it can browse without writing any CDP code:

| Tool | What it does |
|---|---|
| `browser_open` | Opens a browser (optional `ttl_s` and `sandbox_id`) and answers its id and `cdp_url`. |
| `browser_goto` | Opens a URL and answers the final URL, the title and the page's text. |
| `browser_screenshot` | Opens a URL and answers a PNG screenshot (optional `full_page`). |
| `browser_list` | Lists the organization's open browsers. |
| `browser_close` | Closes a browser. |

## Price and limits

- **Billed per browser-hour, per second**, at the rate on [the pricing page](https://kosa8.com/pricing/), from when it opens until it is closed or reaches its ttl, whichever comes first.

- Against your plan's included sandbox-hours, a browser-hour counts as an hour of the smallest sandbox (0.25 vCPU).

- It needs a plan, like a sandbox: without one, a create answers `402 plan_required`. The spending cap and new-account limit apply as usual.

- Open at once, per organization: Solo 1, Pro 3, Team 5, Scale 10. Past that, a create answers `429 browser_limit`. kosa8 Cloud runs up to 200 browsers across all accounts; when it is full, a create answers `503 browsers_full`, so try again in a minute.

## Errors

| Status | Code | Meaning |
|---|---|---|
| 400 | `invalid_ttl`, `invalid_name`, `invalid_url` | A field is out of range or malformed. |
| 402 | `plan_required` | The organization has no plan. Pick one at app.kosa8.com. |
| 403 | `not_enabled` | Browser sandboxes are not turned on for this organization. |
| 410 | `browser_closed` | The browser has closed (ttl, idle, deleted, or its sandbox was deleted). Open a new one. |
| 429 | `browser_limit`, `browser_rate` | Too many open at once for the plan, or too many started in the same few seconds. |
| 503 | `browsers_full`, `browser_unavailable` | No browser could be started right now. Retry after `retry_after_s`. |

---
Source: https://docs.kosa8.com/guides/cloud-browsers/
