For AI agents: a documentation index is available at /llms.txt
Skip to main content

Disconnect and Reconnect to a Browser

Keep a browser session alive for a short window after disconnecting and reconnect to it with all cookies, localStorage, and state intact. Ideal for scripts that need to briefly detach and resume within seconds or minutes.

Long-lived sessions

Need cookies and data to survive for hours or days between runs? See Continue Browser State Across Runs for an approach that keeps browser state alive much longer.

Prerequisites

Steps

Reconnection works in two phases: first you start a session and request a reconnection endpoint, then you reconnect using the returned URL. The browser stays alive in the background for the duration you specify.

Use BrowserQL over HTTP to start a session, perform actions, and get a reconnection endpoint for follow-up requests.

View Full Code on GitHub

1. Start a session and get the reconnection endpoint

Send a BQL mutation that navigates to a page and calls reconnect to keep the browser alive:

curl -X POST \
"https://production-sfo.browserless.io/stealth/bql?token=YOUR_API_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{
"query": "mutation StartSession { goto(url: \"https://example.com\", waitUntil: domContentLoaded) { status } reconnect(timeout: 60000) { browserQLEndpoint browserWSEndpoint } }",
"variables": {},
"operationName": "StartSession"
}'

2. Reconnect with the returned endpoint

Use the browserQLEndpoint from the response (append your token) to send a follow-up mutation to the same browser:

curl -X POST \
"RETURNED_BROWSERQL_ENDPOINT?token=YOUR_API_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{
"query": "mutation ContinueSession { html { html } }",
"variables": {},
"operationName": "ContinueSession"
}'

3. Check the output

The first response includes the reconnection URLs:

{
"data": {
"goto": { "status": 200 },
"reconnect": {
"browserQLEndpoint": "https://production-sfo.browserless.io/stealth/bql/abc123",
"browserWSEndpoint": "wss://production-sfo.browserless.io/chromium/abc123"
}
}
}

The second response returns the page HTML from the still-running browser.

Next steps