Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Make possible to pass custom headers in the SDKs (#656)

Mish Ushakov1bdf809e8b1c
diff --git a/.changeset/unlucky-papayas-design.md b/.changeset/unlucky-papayas-design.md
new file mode 100644
index 000000000..a1c1005bf
--- /dev/null
+++ b/.changeset/unlucky-papayas-design.md
@@ -0,0 +1,6 @@
+---
+'@e2b/python-sdk': patch
+'e2b': patch
+---
+
+make it possible to pass custom headers in the SDk
diff --git a/packages/js-sdk/src/api/index.ts b/packages/js-sdk/src/api/index.ts
index 2d31fd108..4d765919c 100644
--- a/packages/js-sdk/src/api/index.ts
+++ b/packages/js-sdk/src/api/index.ts
@@ -58,6 +58,7 @@ class ApiClient {
...(config.accessToken && {
Authorization: `Bearer ${config.accessToken}`,
}),
+ ...config.headers,
},
})
diff --git a/packages/js-sdk/src/connectionConfig.ts b/packages/js-sdk/src/connectionConfig.ts
index ed9520399..a02c27e4f 100644
--- a/packages/js-sdk/src/connectionConfig.ts
+++ b/packages/js-sdk/src/connectionConfig.ts
@@ -12,19 +12,19 @@ export const KEEPALIVE_PING_HEADER = 'Keepalive-Ping-Interval'
export interface ConnectionOpts {
/**
* E2B API key to use for authentication.
- *
+ *
* @default E2B_API_KEY // environment variable
*/
apiKey?: string
/**
* E2B access token to use for authentication.
- *
+ *
* @default E2B_ACCESS_TOKEN // environment variable
*/
accessToken?: string
/**
* Domain to use for the API.
- *
+ *
* @default E2B_DOMAIN // environment variable or `e2b.app`
*/
domain?: string
@@ -36,7 +36,7 @@ export interface ConnectionOpts {
debug?: boolean
/**
* Timeout for requests to the API in **milliseconds**.
- *
+ *
* @default 30_000 // 30 seconds
*/
requestTimeoutMs?: number
@@ -44,6 +44,11 @@ export interface ConnectionOpts {
* Logger to use for logging messages. It can accept any object that implements `Logger` interface—for example, {@link console}.
*/
logger?: Logger
+
+ /**
+ * Additional headers to send with the request.
+ */
+ headers?: Record<string, string>
}
/**
@@ -60,6 +65,8 @@ export class ConnectionConfig {
readonly apiKey?: string
readonly accessToken?: string
+ readonly headers?: Record<string, string>
+
constructor(opts?: ConnectionOpts) {
this.apiKey = opts?.apiKey || ConnectionConfig.apiKey
this.debug = opts?.debug || ConnectionConfig.debug
@@ -67,6 +74,7 @@ export class ConnectionConfig {
this.accessToken = opts?.accessToken || ConnectionConfig.accessToken
this.requestTimeoutMs = opts?.requestTimeoutMs ?? REQUEST_TIMEOUT_MS
this.logger = opts?.logger
+ this.headers = opts?.headers
this.apiUrl = this.debug
? 'http://localhost:3000'
@@ -101,5 +109,4 @@ export class ConnectionConfig {
*/
export type Username = 'root' | 'user'
-
export const defaultUsername: Username = 'user'
diff --git a/packages/python-sdk/e2b/api/__init__.py b/packages/python-sdk/e2b/api/__init__.py
index 2d5aae547..49530b114 100644
--- a/packages/python-sdk/e2b/api/__init__.py
+++ b/packages/python-sdk/e2b/api/__init__.py
@@ -85,6 +85,11 @@ class ApiClient(AuthenticatedClient):
auth_header_name = "X-API-KEY" if require_api_key else "Authorization"
prefix = "" if require_api_key else "Bearer"
+ headers = {
+ **default_headers,
+ **(config.headers or {}),
+ }
+
super().__init__(
base_url=config.api_url,
httpx_args={
@@ -94,9 +99,7 @@ class ApiClient(AuthenticatedClient):
},
"transport": transport,
},
- headers={
- **default_headers,
- },
+ headers=headers,
token=token,
auth_header_name=auth_header_name,
prefix=prefix,
diff --git a/packages/python-sdk/e2b/connection_config.py b/packages/python-sdk/e2b/connection_config.py
index 6557d84f3..0a2f64816 100644
--- a/packages/python-sdk/e2b/connection_config.py
+++ b/packages/python-sdk/e2b/connection_config.py
@@ -1,6 +1,6 @@
import os
-from typing import Literal, Optional
+from typing import Literal, Optional, Dict
REQUEST_TIMEOUT: float = 30.0 # 30 seconds
@@ -36,11 +36,13 @@ class ConnectionConfig:
api_key: Optional[str] = None,
access_token: Optional[str] = None,
request_timeout: Optional[float] = None,
+ headers: Optional[Dict[str, str]] = None,
):
self.domain = domain or ConnectionConfig._domain()
self.debug = debug or ConnectionConfig._debug()
self.api_key = api_key or ConnectionConfig._api_key()
self.access_token = access_token or ConnectionConfig._access_token()
+ self.headers = headers
self.request_timeout = ConnectionConfig._get_request_timeout(
REQUEST_TIMEOUT,
diff --git a/packages/python-sdk/e2b/sandbox_async/sandbox_api.py b/packages/python-sdk/e2b/sandbox_async/sandbox_api.py
index 7e7bc29b5..e3799b71f 100644
--- a/packages/python-sdk/e2b/sandbox_async/sandbox_api.py
+++ b/packages/python-sdk/e2b/sandbox_async/sandbox_api.py
@@ -26,6 +26,7 @@ class SandboxApi(SandboxApiBase):
domain: Optional[str] = None,
debug: Optional[bool] = None,
request_timeout: Optional[float] = None,
+ headers: Optional[Dict[str, str]] = None,
) -> List[SandboxInfo]:
"""
List all running sandboxes.
@@ -35,6 +36,7 @@ class SandboxApi(SandboxApiBase):
:param domain: Domain to use for the request, only relevant for self-hosted environments
:param debug: Enable debug mode, all requested are then sent to localhost
:param request_timeout: Timeout for the request in **seconds**
+ :param headers: Additional headers to send with the request
:return: List of running sandboxes
"""
@@ -43,6 +45,7 @@ class SandboxApi(SandboxApiBase):
domain=domain,
debug=debug,
request_timeout=request_timeout,
+ headers=headers,
)
# Convert filters to the format expected by the API
@@ -92,6 +95,7 @@ class SandboxApi(SandboxApiBase):
domain: Optional[str] = None,
debug: Optional[bool] = None,
request_timeout: Optional[float] = None,
+ headers: Optional[Dict[str, str]] = None,
) -> SandboxInfo:
"""
Get the sandbox info.
@@ -100,6 +104,8 @@ class SandboxApi(SandboxApiBase):
:param domain: Domain to use for the request, defaults to `E2B_DOMAIN` environment variable
:param debug: Debug mode, defaults to `E2B_DEBUG` environment variable
:param request_timeout: Timeout for the request in **seconds**
+ :param headers: Additional headers to send with the request
+
:return: Sandbox info
"""
config = ConnectionConfig(
@@ -107,6 +113,7 @@ class SandboxApi(SandboxApiBase):
domain=domain,
debug=debug,
request_timeout=request_timeout,
+ headers=headers,
)
async with AsyncApiClient(config) as api_client:
@@ -143,12 +150,14 @@ class SandboxApi(SandboxApiBase):
domain: Optional[str] = None,
debug: Optional[bool] = None,
request_timeout: Optional[float] = None,
+ headers: Optional[Dict[str, str]] = None,
) -> bool:
config = ConnectionConfig(
api_key=api_key,
domain=domain,
debug=debug,
request_timeout=request_timeout,
+ headers=headers,
)
if config.debug:
@@ -178,12 +187,14 @@ class SandboxApi(SandboxApiBase):
domain: Optional[str] = None,
debug: Optional[bool] = None,
request_timeout: Optional[float] = None,
+ headers: Optional[Dict[str, str]] = None,
) -> None:
config = ConnectionConfig(
api_key=api_key,
domain=domain,
debug=debug,
request_timeout=request_timeout,
+ headers=headers,
)
if config.debug:
@@ -211,12 +222,14 @@ class SandboxApi(SandboxApiBase):
domain: Optional[str] = None,
debug: Optional[bool] = None,
request_timeout: Optional[float] = None,
+ headers: Optional[Dict[str, str]] = None,
) -> SandboxCreateResponse:
config = ConnectionConfig(
api_key=api_key,
domain=domain,
debug=debug,
request_timeout=request_timeout,
+ headers=headers,
)
async with AsyncApiClient(config) as api_client:
diff --git a/packages/python-sdk/e2b/sandbox_sync/sandbox_api.py b/packages/python-sdk/e2b/sandbox_sync/sandbox_api.py
index da4c5920c..484b7bdb9 100644
--- a/packages/python-sdk/e2b/sandbox_sync/sandbox_api.py
+++ b/packages/python-sdk/e2b/sandbox_sync/sandbox_api.py
@@ -28,6 +28,7 @@ class SandboxApi(SandboxApiBase):
domain: Optional[str] = None,
debug: Optional[bool] = None,
request_timeout: Optional[float] = None,
+ headers: Optional[Dict[str, str]] = None,
) -> List[SandboxInfo]:
"""
List all running sandboxes.
@@ -37,6 +38,7 @@ class SandboxApi(SandboxApiBase):
:param domain: Domain to use for the request, only relevant for self-hosted environments
:param debug: Enable debug mode, all requested are then sent to localhost
:param request_timeout: Timeout for the request in **seconds**
+ :param headers: Additional headers to send with the request
:return: List of running sandboxes
"""
@@ -45,6 +47,7 @@ class SandboxApi(SandboxApiBase):
domain=domain,
debug=debug,
request_timeout=request_timeout,
+ headers=headers,
)
# Convert filters to the format expected by the API
@@ -93,6 +96,7 @@ class SandboxApi(SandboxApiBase):
domain: Optional[str] = None,
debug: Optional[bool] = None,
request_timeout: Optional[float] = None,
+ headers: Optional[Dict[str, str]] = None,
) -> SandboxInfo:
"""
Get the sandbox info.
@@ -101,6 +105,8 @@ class SandboxApi(SandboxApiBase):
:param domain: Domain to use for the request, defaults to `E2B_DOMAIN` environment variable
:param debug: Debug mode, defaults to `E2B_DEBUG` environment variable
:param request_timeout: Timeout for the request in **seconds**
+ :param headers: Additional headers to send with the request
+
:return: Sandbox info
"""
config = ConnectionConfig(
@@ -108,6 +114,7 @@ class SandboxApi(SandboxApiBase):
domain=domain,
debug=debug,
request_timeout=request_timeout,
+ headers=headers,
)
with ApiClient(
@@ -146,12 +153,14 @@ class SandboxApi(SandboxApiBase):
domain: Optional[str] = None,
debug: Optional[bool] = None,
request_timeout: Optional[float] = None,
+ headers: Optional[Dict[str, str]] = None,
) -> bool:
config = ConnectionConfig(
api_key=api_key,
domain=domain,
debug=debug,
request_timeout=request_timeout,
+ headers=headers,
)
if config.debug:
@@ -183,12 +192,14 @@ class SandboxApi(SandboxApiBase):
domain: Optional[str] = None,
debug: Optional[bool] = None,
request_timeout: Optional[float] = None,
+ headers: Optional[Dict[str, str]] = None,
) -> None:
config = ConnectionConfig(
api_key=api_key,
domain=domain,
debug=debug,
request_timeout=request_timeout,
+ headers=headers,
)
if config.debug:
@@ -218,12 +229,14 @@ class SandboxApi(SandboxApiBase):
domain: Optional[str] = None,
debug: Optional[bool] = None,
request_timeout: Optional[float] = None,
+ headers: Optional[Dict[str, str]] = None,
) -> SandboxCreateResponse:
config = ConnectionConfig(
api_key=api_key,
domain=domain,
debug=debug,
request_timeout=request_timeout,
+ headers=headers,
)
with ApiClient(