Skip to main content

When to use this

Use this when a recurring job needs an authenticated browser without logging in on every run. Browserbase contexts retain site data; a deterministic protected-page marker proves authentication. Use ordinary locators for a login form you own. Stagehand is useful when adapting the fill steps to an unfamiliar form.

Goal and output

Log into the public Internet test site, persist its cookies in a Browserbase context, and reuse that context on the next run. Each run writes out/login.json with an authentication check, a reuse flag, and the Browserbase session ID.

Agent prompt

Adapt persisted login to an application I own.

Prerequisites and inputs

  • Add BROWSERBASE_API_KEY and OPENAI_API_KEY to .env.
  • BROWSERBASE_CONTEXT_ID is optional. If it is unset, the example creates a Browserbase context and prints its ID. Save that ID in .env before the reuse run.
  • The example includes the test site’s public LOGIN_USER and LOGIN_PASSWORD. Use credentials for your own account when adapting it.
  • TypeScript requires Node.js 22.18+ and pnpm. Python requires Python 3.11+ and uv. Go requires Go 1.26+.

Check out and run

Check out packages/examples/cookbooks/persisted-login using the overview command.
Fill in .env before the first run. Run sequentially so context persistence finishes before reuse.

How the job works

1

Open a persisted context

Launch a five-minute cloud session with the supplied or auto-created context and persist: true.
2

Check protected-page access

Visit the protected page. If its logout marker exists, reuse the authenticated context without model calls.
3

Log in once

Otherwise, fill username and password through %variable% placeholders and click Login once. Check protected-page access again.
4

Verify and close

Write the receipt only after authentication is verified. Close Stagehand and the browser, which persists browser data for the next session.
The excerpts show context configuration and the authentication probe. The runnable folder fills both credentials, submits once, verifies protected-page access, and closes the browser to persist the context.

Expected result and failure checks

The first run reports authentication. The second reports context reuse and writes reused: true. Both print a session link for inspection. Context IDs carry authenticated state. Treat them as credentials, keep them out of version control, and use separate contexts per account and environment.
Run one writer per Browserbase context. Wait for browser closure and persistence before starting the next session.
For local profile persistence, see user data.

Adapt to your site

Change the fixed login URL, protected URL, and authenticated marker together. The marker must prove access to a protected page, not merely the presence of a generic header. Keep credentials in variables and serialize writes to each context. Add an explicit MFA flow when your application requires it. Browser data persistence and form approval.