Key Takeaways
- Programmatic Session Control: The Browserless Sessions API allows you to create, configure, and manage browser sessions with full control, enabling automation that can resume statefully between runs.
- Designed for Orchestration: Sessions can be created and reused across distributed systems or scheduled workflows, with TTL-based control and explicit cleanup options via API endpoints.
- Improved Efficiency and Reliability: By persisting session data like cookies and localStorage, you can avoid repeated login flows and reduce automation friction, especially for authenticated or long-running tasks.
Introduction
Browser automation often fails when session state doesn’t persist. Repeated logins, CAPTCHAs, and lost data like cookies or localStorage slow execution and raise failure rates. Standard sessions have limited control and short TTLs, forcing scripts to rebuild context. The Browserless Sessions API, introduced in BaaS v2, gives you programmatic control over session creation, configuration, and connection. It supports long-lived sessions, external orchestration, and lifecycle management so scripts can resume with context intact. In this article, you’ll learn when to use the Sessions API, how to configure and connect sessions, and how to manage them efficiently.
Choosing Between Browser Sessions and the Session API
When you're automating browser tasks, one of the most frustrating issues is losing session state. Having to log in again, deal with CAPTCHA, or reset user state adds friction and slows everything down. Browserless gives you two solid ways to hang on to session data: Browser Sessions and the Sessions API.
Browser Sessions are the simpler option; they work with any automation library, such as Puppeteer or Playwright, and let you reconnect within a short TTL window. Just remember: don’t call browser.close(), use browser.disconnect() instead.
This keeps cookies, localStorage, sessionStorage, and cache alive for a few seconds to a few minutes, depending on your plan. Great for scripts that run quickly and may require a rapid reconnect.
The Sessions API, on the other hand, gives you full control. You can create a session via REST, set your TTL (up to 30 minutes), toggle stealth or headless mode, pass Chromium flags, and even set a proxy all before your script connects.
It’s designed for longer-running workflows or automation that needs to be triggered from outside your main codebase (like a job queue or scheduler).
Use Browser Sessions if you just want a fast reconnect during a single automation run. Go with the Sessions API when you’re building something more structured, especially if you need your automation to resume later or run across different systems.
Creating a Session with the Session API
The Sessions API allows you to create and control long-lived browser sessions programmatically, separate from your automation logic. Sessions can be configured, reused across script runs, and shut down explicitly when you're done.
This is especially useful when workflows span multiple services or steps, or when you want browser state like localStorage or sessionStorage to persist between reconnects. Let’s walk through a full example that sets browser state, disconnects, reconnects, and confirms that data remains available throughout.
Create and Configure the Session
Lets start by creating a session using a POST
request to the Sessions API. You define session behavior and lifecycle parameters, such as TTL, headless mode, and stealth settings.
This returns two values: a WebSocket URL (connect
) for Puppeteer to attach to the session, and a REST URL (stop
) to clean up the session manually.
Set State Inside the Browser
Once connected, you can treat this like any other Puppeteer browser, but now with the benefit that the state you create will persist.
This simulates a user interacting with the site and storing data that you want to persist across sessions without needing to run login or UI setup logic every time.
Disconnect and Reconnect
You can now disconnect safely, leaving the browser session alive inside Browserless.
Later, reconnect using the same connect
URL:
This confirms that the cart data stored earlier is still available without any manual rehydration, login flows, or reruns of setup logic.
End the Session When Done
When you're finished, explicitly delete the session to free up resources:
What This Unlocks
This pattern supports workflows where automation needs to pause and resume, like filling multi-step forms, saving progress between retries, or chaining jobs across services.
Rather than restarting a new browser each time and replaying state, you're working from a persistent, reconnectable browser instance with full context.
You gain precision over session lifespan, external control via REST, and seamless continuity for scripts that would otherwise reset on every execution.
The Sessions API is purpose-built for this kind of controlled, stateful automation. It's ideal for orchestrated workloads, hybrid flows with user input, or multi-stage scraping where losing context means rework.
Best Practices and Session Management
Managing sessions efficiently is fundamental when building scalable, repeatable automation. The following practices are designed to maintain clean execution environments, conserve resources, and reduce session-related failures in multi-step workflows.
Always Terminate Sessions Explicitly
Sessions should always be closed intentionally. For automation libraries like Puppeteer or Playwright, call browser.close() once your task is complete. This shuts down the browser instance and signals to Browserless that the session is finished. If you're using the Sessions API, use the DELETE endpoint to remove the session explicitly:
Relying on TTL expiration as a substitute can lead to resource retention longer than necessary, especially when workflows crash or are aborted mid-run. Explicit cleanup prevents this and reduces the memory footprint across concurrent sessions.
Use Minimal TTL for Workload Requirements
Each session created with the Sessions API accepts a ttl value (in milliseconds) that defines its lifespan. While the API allows a maximum of 30 minutes, it’s recommended to align TTL with the expected job duration. For example:
Over-provisioning TTL across all sessions increases memory usage and keeps stale state available longer than required. For tasks that complete in seconds or minutes, shorter TTLs improve throughput and free up compute faster for other jobs.
Attach Contextual Metadata in Orchestration Layer
The Sessions API does not natively support metadata, but incorporating identifiers such as job ID, workflow ID, or task type into your orchestration layer enables accurate session tracking. This can be done by associating session IDs with relevant metadata in your job queue, database, or task scheduler.
This practice supports better diagnostics, traceability, and cleanup in systems where multiple workflows or users may be using sessions concurrently.
Store Session Identifiers for Reconnection
Session reconnection depends on the browserWSEndpoint returned at session creation. To reuse the session, persist this value with the session ID in a data store accessible by downstream components. This is especially important in distributed systems, where different services may be responsible for creating, executing, and terminating automation.
Without persistent session references, any failure between session creation and connection will require reinitializing browser state, increasing total execution time.
Avoid TTL Overhead on Lightweight Jobs
There is no benefit to assigning 30-minute TTLs to sessions performing tasks that complete in under 2 minutes. Doing so increases idle session count, raises memory consumption, and introduces unnecessary latency in high-load systems.
Configure TTL to reflect the expected runtime profile of the task. This creates predictable resource churn and lowers the probability of resource exhaustion under peak load.
Implement Periodic Cleanup for Orphaned Sessions
Not all sessions will terminate cleanly. Jobs may crash, be retried mid-process, or be abandoned due to external interruptions. These sessions remain active until TTL expires.
To prevent the accumulation of such orphaned sessions, implement periodic cleanup routines that check for expired or unused sessions and remove them via the Sessions API.
This reduces persistent memory usage and avoids scenarios where state from unintended sessions impacts subsequent runs. Regular cleanup complements short TTL configurations and maintains a stable automation environment.
Conclusion
Our Browserless Sessions API adds reliability and persistent state to browser automation, making it easier to build scalable, repeatable workflows. It's a strong fit for distributed systems, long-running scraping, and authenticated flows that need consistent session behavior. With external orchestration support and TTL control, it works well with modern job queues and microservices. Start with one task, expand as needed. Sign up for a Browserless account to get started with the Sessions API.
FAQs
What is the Browserless Sessions API?
The Browserless Sessions API is a REST-based interface that lets developers programmatically create and manage browser sessions, providing persistent state across automation tasks.
How do I use the Sessions API in browser automation?
You initiate a session using a POST request, configure parameters like TTL, headless mode, and proxy settings, then connect using the returned WebSocket endpoint from your automation tool (e.g., Puppeteer or Playwright).
What’s the difference between Browser Sessions and the Sessions API in Browserless?
Browser Sessions support quick reconnects for short TTLs and are simpler to use, while the Sessions API offers external orchestration, explicit lifecycle control, and longer TTLs up to 30 minutes.
Why use the Sessions API for browser automation?
It provides persistent session state, reduces repetitive tasks like logging in, integrates well with job queues, and allows automation workflows to resume across distributed systems.
Can I store cookies and localStorage with the Browserless Sessions API?
Yes. The Sessions API maintains session data like cookies, localStorage, and sessionStorage, which are preserved across reconnections until the session is closed or expires.