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
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/e2b/client.rb', line 101 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 93 94 |
# 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 if body[:autoPause] body[:autoResume] = { enabled: lifecycle[:auto_resume] } end 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.
198 199 200 201 |
# File 'lib/e2b/client.rb', line 198 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.
222 223 224 225 226 227 |
# File 'lib/e2b/client.rb', line 222 def delete_snapshot(snapshot_id) @http_client.delete("/templates/#{snapshot_id}") true rescue NotFoundError false end |
#get(sandbox_id) ⇒ Sandbox
Get sandbox details
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/e2b/client.rb', line 118 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
152 153 154 155 156 157 |
# File 'lib/e2b/client.rb', line 152 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
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/e2b/client.rb', line 135 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.
209 210 211 212 213 214 215 216 |
# File 'lib/e2b/client.rb', line 209 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
171 172 173 |
# File 'lib/e2b/client.rb', line 171 def pause(sandbox_id) @http_client.post("/sandboxes/#{sandbox_id}/pause") end |
#resume(sandbox_id, timeout: nil) ⇒ Sandbox
Resume a sandbox
180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/e2b/client.rb', line 180 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
163 164 165 166 |
# File 'lib/e2b/client.rb', line 163 def set_timeout(sandbox_id, timeout) @http_client.post("/sandboxes/#{sandbox_id}/timeout", body: { timeout: timeout }) end |