Skip to content

An open-source, faster Devin alternative

TypeScript

Fix Python SDK WebSocket auth: pass api_key as query param fallback

motatoes2ee5143416cc
diff --git a/sdks/python/opencomputer/agent.py b/sdks/python/opencomputer/agent.py
index 2d3ac681..98201d3c 100644
--- a/sdks/python/opencomputer/agent.py
+++ b/sdks/python/opencomputer/agent.py
@@ -48,11 +48,13 @@ class Agent:
sandbox_id: str,
connect_url: str,
token: str,
+ api_key: str = "",
):
self._client = client
self._sandbox_id = sandbox_id
self._connect_url = connect_url
self._token = token
+ self._api_key = api_key
async def start(
self,
@@ -138,8 +140,12 @@ class Agent:
"https://", "wss://"
)
ws_endpoint = f"{ws_url}/sandboxes/{self._sandbox_id}/exec/{session_id}"
+ # WebSocket API cannot set custom headers, so pass credentials as query params.
+ # Prefer JWT token (direct worker access); fall back to API key (control plane).
if self._token:
ws_endpoint += f"?token={self._token}"
+ elif self._api_key:
+ ws_endpoint += f"?api_key={self._api_key}"
ws = await websockets.connect(ws_endpoint)
diff --git a/sdks/python/opencomputer/pty.py b/sdks/python/opencomputer/pty.py
index 99c2d387..c00d0afc 100644
--- a/sdks/python/opencomputer/pty.py
+++ b/sdks/python/opencomputer/pty.py
@@ -70,9 +70,13 @@ class Pty:
ws_url = self._api_url.replace("http://", "ws://").replace("https://", "wss://")
ws_url = f"{ws_url}/sandboxes/{self._sandbox_id}/pty/{session_id}"
+ # Pass credentials as both header and query param for compatibility.
+ # Python websockets supports custom headers, but query params work
+ # consistently across all WebSocket implementations.
headers = {}
if self._api_key:
headers["X-API-Key"] = self._api_key
+ ws_url += f"?api_key={self._api_key}"
ws = await websockets.connect(ws_url, additional_headers=headers)
diff --git a/sdks/python/opencomputer/sandbox.py b/sdks/python/opencomputer/sandbox.py
index da4ad08d..4aac233e 100644
--- a/sdks/python/opencomputer/sandbox.py
+++ b/sdks/python/opencomputer/sandbox.py
@@ -215,7 +215,7 @@ class Sandbox:
@property
def agent(self) -> Agent:
"""Access Claude Agent SDK sessions."""
- return Agent(self._ops_client, self.sandbox_id, self._connect_url, self._token)
+ return Agent(self._ops_client, self.sandbox_id, self._connect_url, self._token, self._api_key)
@property
def files(self) -> Filesystem: