- Creating a Context via the Contexts API
- Passing the Context ID into the Create Sessions API
View or run the example template here
Why use Contexts?
- Reusing cookies & session data: Maintain login states across multiple sessions without needing to log in repeatedly.
- Preserving authentication: Store and reuse authentication tokens, reducing the need to re-enter credentials.
- Retaining application data: Persist localStorage, IndexedDB, and other site-specific data written by web applications.
- Faster workflows: Skip login flows and re-authentication steps, reducing the total time of your automation runs.
What data does a Context store?
What data does a Context store?
A Context persists the contents of the Chromium user data directory, including:
- Cookies — including session cookies, explicitly backed up and restored between sessions
- localStorage — per-origin key-value storage
- IndexedDB — structured client-side databases
- Session Storage — tab-scoped storage
- Service Workers — site-controlled caching and offline support
- Web Data — form autofill entries and saved form data
- Browser preferences — site-level settings, permissions, and security state (e.g., HSTS)
Context data can include stored credentials and other sensitive browsing
data. Because of this, Contexts are uniquely encrypted at rest to ensure
security.
Create a Context
To create a Context, use the Create Context API. This returns a unique Context ID that you can pass into new sessions to persist data.- Node.js
- Python
- cURL
Use a Context
After creating a Context, use it in a new session to reuse cookies, authentication, and application data. This creates a returning user experience, speeding up your workflow and eliminating repeated logins.After closing a session with
persist: true, wait a few seconds before reusing the Context to ensure data is synchronized.- Node.js
- Python
Set persist
If you need to store new cookies, authentication tokens, or application data, you must setpersist: true when creating a session. This ensures that any changes made during the session—such as logging in or saving site preferences—are retained for future sessions instead of being lost when the session ends. The data will be saved when the session closes.
persist: false prevents changes from being saved to the Context. It’s useful for less common use cases, such as:
- Read-only session: If you need to access saved cookies or data without modifying them.
- Prevent session state changes: Avoids overwriting stored login tokens or user data.
Login workflow
A typical flow to persist a website login across Browserbase sessions would be as follows:- Create a Context (get a
contextId). - Start a first Browserbase session with that
contextIdandpersist: true. - Log in to a website inside this first session, either manually through the live view feature or programmatically.
- End this session.
- Wait a few seconds to ensure that the Context is updated.
- Start a second Browserbase session (and future sessions) with the same
contextIdand visit the same website — you should now be signed in automatically without having to repeat the login process.
Login best practices
Browserbase Contexts don’t expire (see Context expiration), but sites can still force a log out. Here are a few additional best practices:- Avoid simultaneous logins — Avoid having multiple sessions using the same Context at once. Sites may force a log out.
- Use a consistent geolocation — Some sites check user location, use a geolocated proxy for these cases.
- One Context per site, per login — Prevent an individual Context from becoming too large, which can slow down your session.
Context expiration
Contexts live indefinitely on Browserbase’s infrastructure. Once created, a Context persists until you explicitly delete it or it becomes invalidated. You can create a Context once and reuse it across sessions for weeks or months. However, there are several ways a Context can become invalid or unusable:Client-side invalidation
- Explicit deletion: Calling the Delete Context API permanently removes the Context
- Project deletion: If the parent project is deleted, all associated Contexts are removed
- Account changes: Account suspension or deletion will invalidate all Contexts
Application-level invalidation
While the context itself persists, the data stored within it can become stale or invalid due to external factors:- Session expiration: Websites may expire authentication cookies after a set period (e.g., 30 days), requiring re-authentication even with a persisted Context
- Password changes: If you change your password on a website, stored session cookies may be invalidated by that website
- Server-side logout: Websites can invalidate sessions server-side (e.g., “log out of all devices”)
- Token revocation: OAuth tokens or API keys stored in cookies may be revoked by the service provider
- Security events: Websites may force re-authentication after detecting suspicious activity
To handle application-level invalidation, implement checks in your automation
to detect logged-out states and re-authenticate when necessary.
Delete a Context
When you no longer need a Context, delete it using the Delete Context API. Once deleted, a Context can’t be used to create new sessions.Deleting a Context is permanent and can’t be undone.
- Node.js
- Python
- cURL
Handling authentication
Once you set up Contexts, follow the authentication guide to easily log into
websites.