1
Get your API Key
Visit the Dashboardâs Overview tab:
Then copy and set the

BROWSERBASE_API_KEY
and BROWSERBASE_PROJECT_ID
environment variables in your .env
file.2
Install and create a virtual environment with UV
UV is a modern package manager for Python.
3
Install Browserbase and Browser Use
4
Create the Managed Browser Session Class
Weâll create a session management class that handles proper browser lifecycle management following Browser Use best practices.
main.py
5
Create Helper Functions
Add helper functions for creating Browserbase sessions and configuring browser profiles:
main.py
6
Create the Main Function
Implement the main execution function with comprehensive error handling:
main.py
7
Run your script
Run your script:You should see your Browserbase session start in Browserbase. The debug URL will be printed to console for real-time session monitoring.
Important Environment VariablesMake sure you have these environment variables in your
.env
file:BROWSERBASE_API_KEY
BROWSERBASE_PROJECT_ID
OPENAI_API_KEY
- Clean Resource Management: Context manager ensures proper cleanup without complexity
- Essential Error Handling: Handles browser disconnections and cleanup failures gracefully
- Simplified Approach: Focused on core functionality without unnecessary overhead
- Production Ready: Robust enough for real-world applications
- Set
keep_alive=False
for proper session cleanup - Use reasonable timeouts to prevent hanging processes
- Implement proper Playwright instance cleanup
- Handle expected browser disconnections after task completion
- Use structured error handling with clear logging
- Browser disconnection after successful form submission is expected behavior
- The context manager ensures proper cleanup even if errors occur
- Playwright instance cleanup prevents hanging processes
- Emergency cleanup handles initialization failures gracefully
- Reach out to us at support@browserbase.com for additional support