Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

persist a unique device_id and send it with sync events

Nick Sweeting90862026fae4
diff --git a/browser_use/sync/auth.py b/browser_use/sync/auth.py
index a00922824..11d9cc7cb 100644
--- a/browser_use/sync/auth.py
+++ b/browser_use/sync/auth.py
@@ -10,6 +10,7 @@ from datetime import datetime
import httpx
from pydantic import BaseModel
+from uuid_extensions import uuid7str
from browser_use.config import CONFIG
@@ -17,6 +18,32 @@ from browser_use.config import CONFIG
TEMP_USER_ID = '99999999-9999-9999-9999-999999999999'
+def get_or_create_device_id() -> str:
+ """Get or create a persistent device ID for this installation."""
+ device_id_path = CONFIG.BROWSER_USE_CONFIG_DIR / 'device_id'
+
+ # Try to read existing device ID
+ if device_id_path.exists():
+ try:
+ device_id = device_id_path.read_text().strip()
+ if device_id: # Make sure it's not empty
+ return device_id
+ except Exception:
+ # If we can't read it, we'll create a new one
+ pass
+
+ # Create new device ID
+ device_id = uuid7str()
+
+ # Ensure config directory exists
+ CONFIG.BROWSER_USE_CONFIG_DIR.mkdir(parents=True, exist_ok=True)
+
+ # Write device ID to file
+ device_id_path.write_text(device_id)
+
+ return device_id
+
+
class CloudAuthConfig(BaseModel):
"""Configuration for cloud authentication"""
@@ -71,6 +98,9 @@ class DeviceAuthClient:
# Temporary user ID for pre-auth events
self.temp_user_id = TEMP_USER_ID
+ # Get or create persistent device ID
+ self.device_id = get_or_create_device_id()
+
# Load existing auth if available
self.auth_config = CloudAuthConfig.load_from_file()
@@ -104,6 +134,7 @@ class DeviceAuthClient:
'client_id': self.client_id,
'scope': self.scope,
'agent_session_id': agent_session_id,
+ 'device_id': self.device_id,
},
)
response.raise_for_status()
@@ -116,6 +147,7 @@ class DeviceAuthClient:
'client_id': self.client_id,
'scope': self.scope,
'agent_session_id': agent_session_id,
+ 'device_id': self.device_id,
},
)
response.raise_for_status()
diff --git a/tests/ci/test_sync_client_auth.py b/tests/ci/test_sync_client_auth.py
index 7e51e329a..4bb774d2e 100644
--- a/tests/ci/test_sync_client_auth.py
+++ b/tests/ci/test_sync_client_auth.py
@@ -146,6 +146,7 @@ class TestDeviceAuthClient:
body = request.get_data(as_text=True)
assert 'client_id=library' in body
assert 'agent_session_id=test-session-id' in body
+ assert 'device_id=' in body # Should include device_id
async def test_poll_for_token_pending(self, httpserver: HTTPServer, http_client, temp_config_dir):
"""Test polling when authorization is pending."""