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 writesout/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_KEYandOPENAI_API_KEYto.env. BROWSERBASE_CONTEXT_IDis optional. If it is unset, the example creates a Browserbase context and prints its ID. Save that ID in.envbefore the reuse run.- The example includes the test site’s public
LOGIN_USERandLOGIN_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 outpackages/examples/cookbooks/persisted-login using the overview command.
- TypeScript
- Python
- Go
.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.
Expected result and failure checks
The first run reports authentication. The second reports context reuse and writesreused: 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.
For local profile persistence, see user data.

