Session States
Sessions transition through these states:active- Session is running and ready to accept connectionsclosed- Session has been terminated normallyerror- Session encountered an error and terminated unexpectedly
Creating a Session
Create a new session with optional configuration.Getting Session Details
Retrieve current information about a specific session:Listing Sessions
Query all your sessions with optional filtering by status:Stopping a Session
Always stop sessions when you’re done to free up resources:Complete Lifecycle Example
Here’s a complete example demonstrating best practices for session management:Automatic Timeout
Sessions automatically stop after some time based on their timeout. By default, this is based on your team’s default Session Timeout setting which you can change on the Settings page. You can also configure the timeout per session during session creation:Error Handling
Use try-finally blocks to guarantee sessions are stopped, even when errors occur:Long Running Sessions
By default, when you disconnect from a session with an automation library like Playwright or Puppeteer, your session will automatically stop. To keep your session alive across disconnects, you can add the&keepAlive=true query parameter to your session’s WebSocket endpoint when you connect via CDP. This will keep your session alive until it times out based on the session’s timeout value (default team setting or timeoutMinutes parameter passed in when you create the session) or if you stop the session manually via the API.
The
keepAlive won’t work if all the pages in the browser are closed. If all pages get closed, then the session will automatically stop.Best Practices
Follow these patterns to build reliable, cost-effective automation:1. Always Use Try-Finally
Wrap session usage in try-finally blocks to guarantee cleanup:2. Set Appropriate Timeouts
Match timeout to task duration. Add a buffer for unexpected delays:- Quick tasks: 5-10 minutes
- Data scraping: 15-30 minutes
- Long workflows: 30-60 minutes
3. Monitor Session State
Check session status before long-running operations:4. Clean Up Orphaned Sessions
Periodically audit for abandoned sessions:5. Handle Network Failures
Network issues can leave sessions running. Always implement cleanup:Next Steps
Connect with Puppeteer
Control sessions with Puppeteer
Connect with Playwright
Control sessions with Playwright
Profiles
Persist browser state across sessions
Recordings
Record and replay session activity