Class: E2B::Client
Overview
Client for interacting with the E2B API
This class provides a convenient wrapper around Sandbox class methods. For the most direct API (matching the official SDK pattern), use Sandbox.create, Sandbox.connect, etc. directly.
Instance Attribute Summary collapse
-
#config ⇒ Configuration
readonly
Client configuration.
Instance Method Summary collapse
-
#connect(sandbox_id, timeout: nil) ⇒ Sandbox
Connect to an existing sandbox.
-
#create(template: "base", timeout: nil, timeout_ms: nil, metadata: nil, envs: nil, secure: true, allow_internet_access: true, network: nil, lifecycle: nil, auto_pause: nil, mcp: nil, request_timeout: nil, **_opts) ⇒ Sandbox
Create a new sandbox.
-
#create_snapshot(sandbox_id) ⇒ Models::SnapshotInfo
Create a snapshot from an existing sandbox.
-
#delete_snapshot(snapshot_id) ⇒ Boolean
Delete a snapshot template.
-
#get(sandbox_id) ⇒ Sandbox
Get sandbox details.
-
#initialize(config_or_options = nil) ⇒ Client
constructor
Initialize a new E2B client.
-
#kill(sandbox_id) ⇒ Boolean
Kill a sandbox.
-
#list(metadata: nil, state: nil, limit: 100, next_token: nil) ⇒ SandboxPaginator
List sandboxes.
-
#list_snapshots(sandbox_id: nil, limit: 100, next_token: nil) ⇒ SnapshotPaginator
List snapshots for the team, optionally filtered by source sandbox.
-
#pause(sandbox_id) ⇒ Object
Pause a sandbox.
-
#resume(sandbox_id, timeout: nil) ⇒ Sandbox
Resume a sandbox.
-
#set_timeout(sandbox_id, timeout) ⇒ Object
Set sandbox timeout.
Constructor Details
#initialize(config_or_options = nil) ⇒ Client
Initialize a new E2B client
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/e2b/client.rb', line 30 def initialize( = nil) @config = resolve_config() @config.validate! @http_client = API::HttpClient.new( base_url: @config.api_url, api_key: @config.api_key, access_token: @config.access_token, logger: @config.logger ) @domain = @config.domain end |
Instance Attribute Details
#config ⇒ Configuration (readonly)
Returns Client configuration.
22 23 24 |
# File 'lib/e2b/client.rb', line 22 def config @config end |
Instance Method Details
#connect(sandbox_id, timeout: nil) ⇒ Sandbox
Connect to an existing sandbox
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/e2b/client.rb', line 99 def connect(sandbox_id, timeout: nil) timeout_seconds = timeout || ((@config.sandbox_timeout_ms || (Sandbox::DEFAULT_TIMEOUT * 1000)) / 1000).to_i response = @http_client.post("/sandboxes/#{sandbox_id}/connect", body: { timeout: timeout_seconds }) Sandbox.new( sandbox_data: response, http_client: @http_client, api_key: @config.api_key, domain: @domain ) end |
#create(template: "base", timeout: nil, timeout_ms: nil, metadata: nil, envs: nil, secure: true, allow_internet_access: true, network: nil, lifecycle: nil, auto_pause: nil, mcp: nil, request_timeout: nil, **_opts) ⇒ Sandbox
Create a new sandbox
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/e2b/client.rb', line 52 def create(template: "base", timeout: nil, timeout_ms: nil, metadata: nil, envs: nil, secure: true, allow_internet_access: true, network: nil, lifecycle: nil, auto_pause: nil, mcp: nil, request_timeout: nil, **_opts) # Support both seconds and milliseconds for backward compat timeout_seconds = if timeout timeout elsif timeout_ms (timeout_ms / 1000).to_i else (@config.sandbox_timeout_ms / 1000).to_i end template = resolved_template(template, mcp: mcp) lifecycle = normalized_lifecycle(lifecycle: lifecycle, auto_pause: auto_pause) body = { templateID: template, timeout: timeout_seconds, secure: secure, allow_internet_access: allow_internet_access, autoPause: lifecycle[:on_timeout] == "pause" } body[:metadata] = if body[:envVars] = envs if envs body[:mcp] = mcp if mcp body[:network] = network if network body[:autoResume] = { enabled: lifecycle[:auto_resume] } if body[:autoPause] response = @http_client.post("/sandboxes", body: body, timeout: request_timeout || @config.request_timeout || 120) ensure_supported_envd_version!(response, @http_client) sandbox = Sandbox.new( sandbox_data: response, http_client: @http_client, api_key: @config.api_key, domain: @domain ) start_mcp_gateway(sandbox, mcp) if mcp sandbox end |
#create_snapshot(sandbox_id) ⇒ Models::SnapshotInfo
Create a snapshot from an existing sandbox.
196 197 198 199 |
# File 'lib/e2b/client.rb', line 196 def create_snapshot(sandbox_id) response = @http_client.post("/sandboxes/#{sandbox_id}/snapshots") Models::SnapshotInfo.from_hash(response) end |
#delete_snapshot(snapshot_id) ⇒ Boolean
Delete a snapshot template.
220 221 222 223 224 225 |
# File 'lib/e2b/client.rb', line 220 def delete_snapshot(snapshot_id) @http_client.delete("/templates/#{snapshot_id}") true rescue NotFoundError false end |
#get(sandbox_id) ⇒ Sandbox
Get sandbox details
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/e2b/client.rb', line 116 def get(sandbox_id) response = @http_client.get("/sandboxes/#{sandbox_id}") Sandbox.new( sandbox_data: response, http_client: @http_client, api_key: @config.api_key, domain: @domain ) end |
#kill(sandbox_id) ⇒ Boolean
Kill a sandbox
150 151 152 153 154 155 |
# File 'lib/e2b/client.rb', line 150 def kill(sandbox_id) @http_client.delete("/sandboxes/#{sandbox_id}") true rescue NotFoundError true end |
#list(metadata: nil, state: nil, limit: 100, next_token: nil) ⇒ SandboxPaginator
List sandboxes
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/e2b/client.rb', line 133 def list(metadata: nil, state: nil, limit: 100, next_token: nil) query = {} query[:metadata] = if query[:state] = state if state SandboxPaginator.new( http_client: @http_client, query: query.empty? ? nil : query, limit: limit, next_token: next_token ) end |
#list_snapshots(sandbox_id: nil, limit: 100, next_token: nil) ⇒ SnapshotPaginator
List snapshots for the team, optionally filtered by source sandbox.
207 208 209 210 211 212 213 214 |
# File 'lib/e2b/client.rb', line 207 def list_snapshots(sandbox_id: nil, limit: 100, next_token: nil) SnapshotPaginator.new( http_client: @http_client, sandbox_id: sandbox_id, limit: limit, next_token: next_token ) end |
#pause(sandbox_id) ⇒ Object
Pause a sandbox
169 170 171 |
# File 'lib/e2b/client.rb', line 169 def pause(sandbox_id) @http_client.post("/sandboxes/#{sandbox_id}/pause") end |
#resume(sandbox_id, timeout: nil) ⇒ Sandbox
Resume a sandbox
178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/e2b/client.rb', line 178 def resume(sandbox_id, timeout: nil) timeout_seconds = timeout || ((@config.sandbox_timeout_ms || (Sandbox::DEFAULT_TIMEOUT * 1000)) / 1000).to_i body = { timeout: timeout_seconds } response = @http_client.post("/sandboxes/#{sandbox_id}/connect", body: body) Sandbox.new( sandbox_data: response, http_client: @http_client, api_key: @config.api_key, domain: @domain ) end |
#set_timeout(sandbox_id, timeout) ⇒ Object
Set sandbox timeout
161 162 163 164 |
# File 'lib/e2b/client.rb', line 161 def set_timeout(sandbox_id, timeout) @http_client.post("/sandboxes/#{sandbox_id}/timeout", body: { timeout: timeout }) end |