Close a Browser Session
Fully terminate a remote browser session and release all resources by calling browser.close() or sending a DELETE request to the session stop endpoint.
Want to detach from a browser without terminating it so you can reconnect later? See Disconnect and Reconnect to a Browser for the graceful disconnect pattern.
- A Browserless API token from your account dashboard
Steps
Closing a session shuts down the browser process and frees compute resources. Use browser.close() in framework code, or DELETE /session/{id} for REST-managed sessions.
- REST API
- Frameworks
- BQL
Use the Session API to create a session, then explicitly terminate it with a DELETE request when finished.
- cURL
- JavaScript
- Python
- Java
- C#
1. Create a session
curl -X POST "https://production-sfo.browserless.io/session?token=YOUR_API_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{
"ttl": 60000,
"stealth": true
}'
The response includes a stop field with the DELETE URL.
2. Close the session
curl -X DELETE "STOP_URL_FROM_STEP_1&force=true"
The force=true parameter ensures the session is terminated immediately, even if a browser is still connected.
1. Create a session and do work
const TOKEN = "YOUR_API_TOKEN_HERE";
const sessionResponse = await fetch(
`https://production-sfo.browserless.io/session?token=${TOKEN}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ttl: 60000, stealth: true }),
}
);
const session = await sessionResponse.json();
console.log("Session created:", session.id);
2. Close the session
const closeResponse = await fetch(`${session.stop}&force=true`, {
method: "DELETE",
});
console.log("Session closed:", closeResponse.status === 200 ? "success" : "failed");
1. Install dependencies
pip install requests
2. Create a session and do work
import requests
TOKEN = "YOUR_API_TOKEN_HERE"
session_response = requests.post(
f"https://production-sfo.browserless.io/session?token={TOKEN}",
json={"ttl": 60000, "stealth": True},
)
session = session_response.json()
print("Session created:", session["id"])
3. Close the session
close_response = requests.delete(f"{session['stop']}&force=true")
print("Session closed:", "success" if close_response.ok else "failed")
1. Create a session
import java.net.URI;
import java.net.http.*;
String token = "YOUR_API_TOKEN_HERE";
String endpoint = "https://production-sfo.browserless.io/session?token=" + token;
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(endpoint))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(
"{\"ttl\": 60000, \"stealth\": true}"
))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
String body = response.body();
System.out.println("Session created: " + body);
// Parse stop URL from response
int stopStart = body.indexOf("\"stop\":\"") + 8;
int stopEnd = body.indexOf("\"", stopStart);
String stopUrl = body.substring(stopStart, stopEnd);
2. Close the session
HttpRequest deleteRequest = HttpRequest.newBuilder()
.uri(URI.create(stopUrl + "&force=true"))
.DELETE()
.build();
HttpResponse<String> deleteResponse = client.send(
deleteRequest, HttpResponse.BodyHandlers.ofString()
);
System.out.println("Session closed: " + deleteResponse.statusCode());
1. Create a session
using System.Net.Http;
using System.Text;
using System.Text.Json;
var token = "YOUR_API_TOKEN_HERE";
var client = new HttpClient();
var content = new StringContent(
JsonSerializer.Serialize(new { ttl = 60000, stealth = true }),
Encoding.UTF8,
"application/json"
);
var response = await client.PostAsync(
$"https://production-sfo.browserless.io/session?token={token}",
content
);
var body = await response.Content.ReadAsStringAsync();
var session = JsonSerializer.Deserialize<JsonElement>(body);
var stopUrl = session.GetProperty("stop").GetString();
Console.WriteLine($"Session created: {session.GetProperty("id")}");
2. Close the session
var closeResponse = await client.DeleteAsync($"{stopUrl}&force=true");
Console.WriteLine($"Session closed: {closeResponse.StatusCode}");
Call browser.close() to fully terminate the remote browser process and release the Browserless session.
- Puppeteer
- Playwright
1. Install dependencies
npm install puppeteer-core
2. Connect, do work, and close
import puppeteer from "puppeteer-core";
const TOKEN = "YOUR_API_TOKEN_HERE";
const browser = await puppeteer.connect({
browserWSEndpoint: `wss://production-sfo.browserless.io?token=${TOKEN}`,
});
try {
const page = await browser.newPage();
await page.goto("https://example.com", { waitUntil: "networkidle2" });
const title = await page.title();
console.log("Page title:", title);
} finally {
await browser.close();
console.log("Browser closed — session resources released.");
}
browser.close() terminates the remote browser process. After calling it, the WebSocket connection is severed and the session cannot be reconnected.
1. Install dependencies
npm install playwright-core
2. Connect, do work, and close
import { chromium } from "playwright-core";
const TOKEN = "YOUR_API_TOKEN_HERE";
const browser = await chromium.connectOverCDP(
`wss://production-sfo.browserless.io?token=${TOKEN}&stealth`
);
try {
const context = browser.contexts()[0] ?? await browser.newContext();
const page = await context.newPage();
await page.goto("https://example.com", { waitUntil: "networkidle" });
const title = await page.title();
console.log("Page title:", title);
} finally {
await browser.close();
console.log("Browser closed — session resources released.");
}
browser.close() shuts down the remote Chromium process. Use this instead of browser.disconnect() when you are done and do not need to reconnect.
BQL sessions are automatically closed when the mutation completes. For Session API sessions using BQL, terminate them with a DELETE request to the stop URL.
1. Create a session with a BQL endpoint
curl -X POST "https://production-sfo.browserless.io/session?token=YOUR_API_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{"ttl": 60000, "stealth": true}'
Save the browserQL and stop fields from the response.
2. Run a BQL mutation against the session
mutation CloseExample {
goto(url: "https://example.com", waitUntil: networkIdle) {
status
}
text(selector: "h1") {
text
}
}
3. Send it
Paste into the BQL IDE and click Run.
4. Close the session
curl -X DELETE "STOP_URL_FROM_STEP_1&force=true"
Without this explicit DELETE, the session remains alive until its TTL expires and continues consuming resources.
5. Check the output
{
"data": {
"goto": { "status": 200 },
"text": { "text": "Example Domain" }
}
}