Class: E2B::Client

Inherits:
Object
  • Object
show all
Includes:
SandboxHelpers
Defined in:
lib/e2b/client.rb

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.

Examples:

Using Client

client = E2B::Client.new(api_key: "your-api-key")
sandbox = client.create(template: "base")

Using Sandbox directly (recommended, matches official SDK)

sandbox = E2B::Sandbox.create(template: "base", api_key: "your-key")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_or_options = nil) ⇒ Client

Initialize a new E2B client

Parameters:

  • config_or_options (Configuration, Hash, nil) (defaults to: nil)

    Configuration or options hash

Options Hash (config_or_options):

  • :api_key (String)

    API key for authentication

  • :api_url (String)

    API URL

  • :timeout_ms (Integer)

    Request timeout in milliseconds



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/e2b/client.rb', line 30

def initialize(config_or_options = nil)
  @config = resolve_config(config_or_options)
  @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

#configConfiguration (readonly)

Returns Client configuration.

Returns:



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

Parameters:

  • sandbox_id (String)

    The sandbox ID

  • timeout (Integer, nil) (defaults to: nil)

    Timeout in seconds

Returns:



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

Parameters:

  • template (String) (defaults to: "base")

    Template ID or alias

  • timeout (Integer, nil) (defaults to: nil)

    Sandbox timeout in seconds (or timeout_ms in milliseconds for backward compat)

  • timeout_ms (Integer, nil) (defaults to: nil)

    Sandbox timeout in milliseconds (deprecated, use timeout in seconds)

  • metadata (Hash, nil) (defaults to: nil)

    Custom metadata

  • envs (Hash{String => String}, nil) (defaults to: nil)

    Environment variables

Returns:

  • (Sandbox)

    The created sandbox instance



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.

Parameters:

  • sandbox_id (String)

    Source sandbox ID

Returns:



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.

Parameters:

  • snapshot_id (String)

    Snapshot identifier

Returns:

  • (Boolean)


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

Parameters:

  • sandbox_id (String)

    The sandbox ID

Returns:



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

Parameters:

  • sandbox_id (String)

    The sandbox ID

Returns:

  • (Boolean)


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

Parameters:

  • metadata (Hash, nil) (defaults to: nil)

    Filter by metadata

  • state (String, nil) (defaults to: nil)

    Filter by state

  • limit (Integer) (defaults to: 100)

    Maximum results

Returns:



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.

Parameters:

  • sandbox_id (String, nil) (defaults to: nil)

    Filter snapshots by source sandbox ID

  • limit (Integer) (defaults to: 100)

    Maximum results per page

  • next_token (String, nil) (defaults to: nil)

    Pagination token

Returns:



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

Parameters:

  • sandbox_id (String)

    The sandbox ID



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

Parameters:

  • sandbox_id (String)

    The sandbox ID

  • timeout (Integer, nil) (defaults to: nil)

    New timeout in seconds

Returns:



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

Parameters:

  • sandbox_id (String)

    The sandbox ID

  • timeout (Integer)

    Timeout in seconds



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