Cloud CDP endpoint
Connect Puppeteer, Playwright or any CDP client to browsergateway.com with a project key, and add profiles, recording and traces per session.
The cloud CDP endpoint is where your automation code connects. Each connection is routed to one of the providers in your project, with the project's strategy, failover and queue applied, and billed as one session.
wss://cdp.browsergateway.io/v1/connect?token=bg_YOUR_ROUTER_KEYThe project key is on the Keys & access page of the dashboard. It identifies both the workspace and the project, so the same code routes to a different set of providers when you switch keys. The key can also be sent as an Authorization: Bearer bg_... header instead of the token query parameter.
The project key is not the account token (bga_...) used by the Cloud REST API. The same project key also works for the Cloud Data API and the Cloud MCP server.
Connect
Puppeteer:
import puppeteer from "puppeteer-core";
const browser = await puppeteer.connect({
browserWSEndpoint: "wss://cdp.browsergateway.io/v1/connect?token=bg_YOUR_ROUTER_KEY",
});
const page = await browser.newPage();
await page.goto("https://en.wikipedia.org/wiki/Web_browser");
await browser.close();Playwright:
import { chromium } from "playwright-core";
const browser = await chromium.connectOverCDP(
"wss://cdp.browsergateway.io/v1/connect?token=bg_YOUR_ROUTER_KEY",
);A successful connection returns three response headers: X-Session-Id (the id shown on the Sessions page), X-Provider (the provider the session was routed to) and X-Session-Resumable (true when a dropped connection can get the same browser back, see Reconnect after a dropped connection).
Query options
| Parameter | Effect |
|---|---|
token=bg_... | Project key. Required unless sent as a bearer header. |
provider=<slug> | Route only to this provider. No failover to other providers. |
profile=<slug> | Start from a saved profile and save changes back to it when the session ends. A slug that does not exist yet is created as an empty profile. See Profiles. |
readOnly=1 | With profile, load the profile without saving changes back. |
session_record=true | Record the session as video. Needs wallet balance. See Recordings. |
observability=true | Capture a trace of network requests, console output and navigations. Needs wallet balance. See Traces. |
sessionKey=<name> | Name the session yourself (8 to 64 letters, digits, - or _). Connecting again with the same name after a dropped connection gives you the same browser. See Reconnect after a dropped connection. |
sessionId=<id> | Reconnect to the browser of a session whose connection dropped, by the id Browser Gateway gave it. See Reconnect after a dropped connection. |
Session limits
- Every session closes after 4 hours.
- A session with no traffic in either direction closes after the project's idle timeout: 5 minutes by default, adjustable from 10 seconds to 14 minutes in the project (Settings, Projects, or
sessionIdleTimeoutMsonPATCH /v1/routers/:id). - There is no workspace-wide limit on concurrent sessions. Each provider's own
maxConcurrentlimits how many sessions it takes at once. When every eligible provider is full, the connection waits in the project's queue (50 waiting connections and 30 seconds by default, both adjustable in the project).
Reconnect after a dropped connection
A WebSocket that stays open for a long time can be cut by the network between your code and Browser Gateway, for example when an edge server restarts or sheds load. Long agent and scraping jobs typically see this about once per session-hour, and sometimes more often during network events. When the connection drops, Browser Gateway keeps the session's browser for 60 seconds so your code can reconnect to it, with its open tabs, page, cookies and storage as they were.
Write long-running code to expect a dropped connection and reconnect.
Name the session (simplest)
Add sessionKey with a name of your choice (8 to 64 letters, digits, - or _), and connect again with the same address when the connection drops:
import puppeteer from "puppeteer-core";
const ENDPOINT = "wss://cdp.browsergateway.io/v1/connect?token=bg_YOUR_ROUTER_KEY&sessionKey=nightly-crawl-42";
let finished = false;
let browser = await puppeteer.connect({ browserWSEndpoint: ENDPOINT });
function reconnectOnDrop(current) {
current.on("disconnected", async () => {
if (finished) return;
browser = await puppeteer.connect({ browserWSEndpoint: ENDPOINT });
reconnectOnDrop(browser);
});
}
reconnectOnDrop(browser);
// ... your automation, using `browser` ...
finished = true;
await browser.close();- While the browser is held, the same address gives you the same browser back.
- After the 60 seconds, or when the browser is gone, the same address starts a new session under that name with a fresh browser. Check
browser.pages()after reconnecting before assuming your page is still there. - Use a different name for each job. Two jobs sharing a name share one browser.
- The response headers
X-Session-KeyandX-Session-Resumed(trueorfalse) tell you which of the two happened.
Reconnect by session id
Without sessionKey, reconnect with the id from the X-Session-Id response header:
wss://cdp.browsergateway.io/v1/connect?token=bg_YOUR_ROUTER_KEY&sessionId=sess_...A reconnect by id after the 60 seconds, or after the browser is gone, is refused with 410; connect again without sessionId to start a new session. Puppeteer and Playwright do not show response headers, so read the id during the WebSocket handshake:
import puppeteer from "puppeteer-core";
import WebSocket from "ws";
const ENDPOINT = "wss://cdp.browsergateway.io/v1/connect?token=bg_YOUR_ROUTER_KEY";
function open(url) {
return new Promise((resolve, reject) => {
const ws = new WebSocket(url);
let sessionId = null;
ws.on("upgrade", (res) => { sessionId = res.headers["x-session-id"]; });
ws.on("open", () => resolve({ ws, sessionId }));
ws.on("unexpected-response", (_req, res) => reject(new Error(`HTTP ${res.statusCode}`)));
ws.on("error", reject);
});
}
async function connect(sessionId) {
const { ws, sessionId: id } = await open(sessionId ? `${ENDPOINT}&sessionId=${sessionId}` : ENDPOINT);
const transport = { send: (m) => ws.send(m), close: () => ws.close() };
ws.on("message", (data) => transport.onmessage?.(data.toString()));
ws.on("close", () => transport.onclose?.());
return { browser: await puppeteer.connect({ transport }), sessionId: id };
}When a session can be resumed
The first connection answers X-Session-Resumable: true when a dropped connection can get the same browser back. That is the case when:
- The provider can hand the browser back: a browserserve provider with its resume window switched on, or a hosted Browserless provider.
- The session does not run on managed browsers or on a provider that starts its sessions through its own API.
- With a
profile, the provider is browserserve. Recording (session_record) and traces (observability) continue across a reconnect into the same recording and trace. - The connection broke. Closing the connection yourself, or calling
browser.close(), ends the session at once and frees the browser.
A resumed session stays one session on the Sessions page and is billed once. After a reconnect, find your page again with browser.pages(); page objects from the previous connection belong to the old socket.
When the session cannot be resumed, reconnect without sessionKey or sessionId to start a new session; with a profile, the new session starts from the last saved state.
Connect errors
A refused connection returns an HTTP status and a plain-text message before the WebSocket opens.
| Status | Meaning |
|---|---|
| 400 | The project has no providers, the provider slug is not in this project, or the session asks to save to a profile but no provider in the project can save to it. |
| 401 | The project key is missing, wrong or revoked. |
| 402 | The workspace is over its monthly session ceiling with an empty wallet, session_record or observability was requested with an empty wallet, a new profile slug would pass the 500-profile limit, or the workspace is suspended. |
| 409 | The project's Profiles, Recording or Traces switch is off for a feature the session asked for, or another session is currently saving to the same profile (retry after the Retry-After seconds). |
| 413 | The saved profile is larger than the per-profile size limit. |
| 410 | A reconnect with sessionId came after the 60-second window, or the session's browser is gone. Connect without sessionId to start a new session. |
| 429 | The queue timed out, every eligible provider is full, or too many failed attempts came from your IP. Honour Retry-After when it is present. |
| 502 | Every eligible provider failed to open a browser. The message lists each attempt. |
| 503 | The pinned provider is cooling down or full, no provider can take the session, the queue is full, or the project's settings changed while the session was starting. |
Project switches
Each project has four switches. All are on for a new project.
| Switch | Where to change it | When off |
|---|---|---|
| Recording | Recordings page header | session_record=true returns 409 |
| Traces | Sessions page header | observability=true returns 409 |
| Profiles | Profiles page header | profile= returns 409 |
| Webhooks | Webhooks page header | No webhook deliveries for this project's sessions |
The same switches are available over the REST API on /v1/routers/:id/settings.
Profiles
?profile=<slug> loads a saved profile into the browser before your code gets it: cookies and localStorage for every saved origin. When the session ends, the new state is saved back as a new version of the profile.
- Any provider can save. Every provider can load and save any profile, except a provider set to a different profile (the provider's
profilefield). If every provider in the project is set to a different profile, the connect returns 400. A browser that still holds a different profile is refused: the connection closes with code 4409 and a reason naming both profiles. On a provider that reuses browsers, anything an earlier session left behind can be saved with the profile. AddreadOnly=1to load the profile without saving. - One writer at a time. Only one session can save to a profile at once. A second saving session gets 409 until the first ends. Read-only sessions have no such limit.
- IndexedDB is saved and restored only on browserserve providers.
- sessionStorage is never saved.
Create and manage profiles on the Profiles page or with the profile endpoints. See Profiles for how capture and injection work.
Recordings
?session_record=true records what the browser renders during the session. It needs a positive wallet balance and the project's Recording switch.
- Charged at
$0.007per minute, billed per second, at session close. See Billing. - A recording stops early when its running cost would use up the wallet balance, or when it reaches 1 GB. The session itself keeps running. The recording's
truncatedfield says which happened. - After the session ends, the recording is encoded to MP4 automatically. It appears on the Recordings page and on the session's page in the dashboard.
- Recordings are kept for 30 days.
Over the REST API, list recordings with GET /v1/replays. When mp4Status is ready, download the video with GET /v1/replays/:id/export/download. If encoding failed or has not started, POST /v1/replays/:id/export starts it again.
Traces
?observability=true captures a trace of the session: network requests, console output, navigations and other page events. It reads the CDP traffic without changing it, so it works with any provider.
- Charged at
$0.05per 1,000 events at session close. - Capture stops after 2,000,000 events in one session. The session keeps running.
- Traces are kept for 60 days.
Traces appear on the session's page in the dashboard. Over the REST API, list them with GET /v1/inspections and read the events with GET /v1/inspections/:id/events.
Playground
The Playground page in the dashboard opens a live browser on a provider you choose and streams it to the page, so you can click and type into it. It uses your project key automatically. It can open a saved profile, read-only unless Save changes to the profile is checked. See Live Playground for how the stream works.
Provider status
GET https://cdp.browsergateway.io/v1/status with the project key (?token= or bearer header) lists the project's providers with their health and priority:
{
"ok": true,
"providers": [
{ "id": "browserless-eu", "slug": "browserless-eu", "healthy": true, "priority": 100 }
]
}