How WebSocket authentication works in the Python SDK
mainStarting from version 0.3.3, the Hopx Python SDK requires JWT authentication for WebSocket connections. The Sandbox object manages the lifecycle of the JWT token and automatically passes it to the WebSocketClient. When a token is refreshed, the Sandbox calls update_jwt_token() on the client to ensure subsequent connections use the valid credentials.
Key components involved in the authentication flow:
Sandbox: Retrieves the token viaget_token()and initializes theWebSocketClientwith it.WebSocketClient: Stores the_jwt_tokenand includes it in theAuthorization: Bearer <token>header during thewebsockets.connect()call.AsyncTerminal: Retrieves the token from the sandbox to authenticate terminal WebSocket connections.
# Conceptual flow of token passing in Sandbox
token = self.get_token()
self._ws_client = WebSocketClient(agent_url, token)
# On token refresh
if "auth_token" in response:
self._ws_client.update_jwt_token(response["auth_token"])