@@ -40,11 +40,15 @@ class ApiParams(TypedDict, total=False):
4040 proxy : Optional [ProxyTypes ]
4141 """Proxy to use for the request. In case of a sandbox it applies to all **requests made to the returned sandbox**."""
4242
43+ sandbox_url : Optional [str ]
44+ """URL to connect to sandbox, defaults to `E2B_SANDBOX_URL` environment variable."""
45+
4346
4447class ConnectionConfig :
4548 """
4649 Configuration for the connection to the API.
4750 """
51+ envd_port = 49983
4852
4953 @staticmethod
5054 def _domain ():
@@ -62,6 +66,10 @@ def _api_key():
6266 def _api_url ():
6367 return os .getenv ("E2B_API_URL" )
6468
69+ @staticmethod
70+ def _sandbox_url ():
71+ return os .getenv ("E2B_SANDBOX_URL" )
72+
6573 @staticmethod
6674 def _access_token ():
6775 return os .getenv ("E2B_ACCESS_TOKEN" )
@@ -72,6 +80,7 @@ def __init__(
7280 debug : Optional [bool ] = None ,
7381 api_key : Optional [str ] = None ,
7482 api_url : Optional [str ] = None ,
83+ sandbox_url : Optional [str ] = None ,
7584 access_token : Optional [str ] = None ,
7685 request_timeout : Optional [float ] = None ,
7786 headers : Optional [Dict [str , str ]] = None ,
@@ -106,6 +115,8 @@ def __init__(
106115 or ("http://localhost:3000" if self .debug else f"https://api.{ self .domain } " )
107116 )
108117
118+ self ._sandbox_url = sandbox_url or ConnectionConfig ._sandbox_url ()
119+
109120 @staticmethod
110121 def _get_request_timeout (
111122 default_timeout : Optional [float ],
@@ -121,6 +132,28 @@ def _get_request_timeout(
121132 def get_request_timeout (self , request_timeout : Optional [float ] = None ):
122133 return self ._get_request_timeout (self .request_timeout , request_timeout )
123134
135+ def get_sandbox_url (self , sandbox_id : str , sandbox_domain : str ) -> str :
136+ if self ._sandbox_url :
137+ return self ._sandbox_url
138+
139+ return f"{ 'http' if self .debug else 'https' } ://{ self .get_host (sandbox_id , sandbox_domain , self .envd_port )} "
140+
141+ def get_host (self , sandbox_id : str , sandbox_domain : str , port : int ) -> str :
142+ """
143+ Get the host address to connect to the sandbox.
144+ You can then use this address to connect to the sandbox port from outside the sandbox via HTTP or WebSocket.
145+
146+ :param port: Port to connect to
147+ :param sandbox_domain: Domain to connect to
148+ :param sandbox_id: Sandbox to connect to
149+
150+ :return: Host address to connect to
151+ """
152+ if self .debug :
153+ return f"localhost:{ port } "
154+
155+ return f"{ port } -{ sandbox_id } .{ sandbox_domain } "
156+
124157 def get_api_params (
125158 self ,
126159 ** opts : Unpack [ApiParams ],
0 commit comments