Skip to main content

Overview

The keepAlive feature allows you to keep sessions alive across disconnects, permitting you to reconnect and continue using the same session.
When connection closes
Without Keep AliveSession ends
With Keep AliveSession stays available for reconnection
Session keep alive is only available on paid plans.

Why Use Keep Alive?

  • Reconnection workflows: Connect, disconnect, and reconnect to the same session
  • Multiple connections: Different scripts or tools can connect to the same session
  • Survive disconnects: Network issues won’t end your session
  • Performance: Reconnecting to an existing session is more performant than creating a new one
  • Session reuse: Re-run scripts against the same session without recreating it
  • Billing optimization: keep alive lets you avoid minimum browser usage charges if running many short-lived sessions (under 1 minute)

Using Keep Alive

Creating a Keep Alive Session

Setting keepAlive to true will keep the session available for later use. You can reconnect to the keep alive session using the same connect URL.
import Browserbase from "browserbase";

const bb = new Browserbase({ apiKey: process.env.BROWSERBASE_API_KEY! });
const session = await bb.sessions.create({
  projectId: process.env.BROWSERBASE_PROJECT_ID!,
  keepAlive: true,
});

// Reconnect later using the same connect URL
console.log("Connect URL:", session.connectUrl);

Releasing a Keep Alive Session

Keep alive sessions must be explicitly released to stop, otherwise they will continue until the session’s timeout.
Release keep alive sessions when you’re done to avoid being charged for unused browser minutes.
await bb.sessions.update(session.id, {
  status: "REQUEST_RELEASE",
  projectId: process.env.BROWSERBASE_PROJECT_ID!,
});

Keep Alive and Timeouts

Keep alive sessions still respect session timeouts. If you need a session that both survives disconnects and runs for an extended period, configure both options:
const session = await bb.sessions.create({
  projectId: process.env.BROWSERBASE_PROJECT_ID!,
  keepAlive: true,
  timeout: 3600, // 1 hour
});

Session Timeouts

Learn how to extend session duration beyond the default timeout.