Managing Browser Session Lifetime

Anchor provides multiple ways to control and terminate browser sessions. In addition to manually stopping sessions via the stop session API, you can configure two types of automatic timeout mechanisms to manage session lifetime effectively. For the full list of available options, view the interactive api documentation.

Timeout Configuration Options

The API offers two distinct timeout parameters through the session.timeout object to automatically manage browser session termination.

Idle Timeout

The idle_timeout parameter automatically terminates sessions after a period of inactivity. This timer starts after the last connection to the browser has disconnected, and any new connection will restart the timer. This includes live view sessions, CDP (Chrome DevTools Protocol) connections, and OS-level control connections. The idle timeout is particularly useful for sessions with unknown length, allowing users to interact with browsers for as long as they need without keeping stale browsers alive unnecessarily. When set to 3 minutes for example, the session will terminate after 3 minutes with no active connections. The default value is 5 minutes, and you can disable automatic termination for idle sessions by setting it to -1.

Maximum Duration

The max_duration parameter sets a hard limit on total session lifetime. Unlike the idle timeout, this will automatically terminate the session after the specified duration regardless of activity level. This acts as a safety mechanism to ensure sessions don’t run indefinitely. The default maximum duration is 180 minutes (3 hours), but you can adjust this based on your needs. Setting max_duration to 10 will terminate the session after exactly 10 minutes, whether the browser is actively being used or not. There is no upper limit on how long you can set the maximum duration.

Implementation Example

curl --request POST \
  --url https://api.anchorbrowser.io/v1/sessions \
  --header 'Content-Type: application/json' \
  --header 'anchor-api-key: your_api_key_here' \
  --data '{
    "session": {
      "timeout": {
        "max_duration": 10,
        "idle_timeout": 3
      }
    }
  }'
In this example, replace "your_api_key_here" with your actual API key. The configuration sets a 10-minute hard session limit with max_duration, while idle_timeout ensures the session terminates after 3 minutes of no active connections. These two timeout mechanisms work independently, so the session will end when whichever condition is met first.