Skip to content

Commit b2fe664

Browse files
committed
rebased #564
1 parent 5ee025c commit b2fe664

48 files changed

Lines changed: 1581 additions & 558 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apps/web/src/app/(docs)/docs/sandbox/connect/page.mdx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@ To connect to a running sandbox, you first need to retrieve its ID. You can do t
1212
import { Sandbox } from "@e2b/code-interpreter"
1313

1414
// Get all running sandboxes
15-
const runningSandboxes = await Sandbox.list() // $HighlightLine
15+
const { sandboxes } = await Sandbox.list({ state: ['running'] }) // $HighlightLine
1616

17-
if (runningSandboxes.length === 0) {
17+
if (sandboxes.length === 0) {
1818
throw new Error("No running sandboxes found")
1919
}
2020

2121
// Get the ID of the sandbox you want to connect to
22-
const sandboxId = runningSandboxes[0].sandboxId
22+
const sandboxId = sandboxes[0].sandboxId
2323
```
2424

2525
```python
2626
from e2b_code_interpreter import Sandbox
2727

2828
# Get all running sandboxes
29-
running_sandboxes = Sandbox.list() # $HighlightLine
29+
running_sandboxes = Sandbox.list(state=['running']) # $HighlightLine
3030

3131
# Get the ID of the sandbox you want to connect to
32-
if len(running_sandboxes) == 0:
32+
if len(running_sandboxes.sandboxes) == 0:
3333
raise Exception("No running sandboxes found")
3434

3535
# Get the ID of the sandbox you want to connect to
36-
sandbox_id = running_sandboxes[0].sandbox_id
36+
sandbox_id = running_sandboxes.sandboxes[0].sandbox_id
3737
```
3838
</CodeGroup>
3939

@@ -46,14 +46,14 @@ Now that you have the sandbox ID, you can connect to the sandbox using the `Sand
4646
import { Sandbox } from "@e2b/code-interpreter"
4747

4848
// Get all running sandboxes
49-
const runningSandboxes = await Sandbox.list()
49+
const { sandboxes } = await Sandbox.list({ state: ['running'] })
5050

51-
if (runningSandboxes.length === 0) {
51+
if (sandboxes.length === 0) {
5252
throw new Error("No running sandboxes found")
5353
}
5454

5555
// Get the ID of the sandbox you want to connect to
56-
const sandboxId = runningSandboxes[0].sandboxId
56+
const sandboxId = sandboxes[0].sandboxId
5757

5858
// Connect to the sandbox
5959
const sandbox = await Sandbox.connect(sandboxId) // $HighlightLine
@@ -65,13 +65,13 @@ const sandbox = await Sandbox.connect(sandboxId) // $HighlightLine
6565
from e2b_code_interpreter import Sandbox
6666

6767
# Get all running sandboxes
68-
running_sandboxes = Sandbox.list()
68+
running_sandboxes = Sandbox.list(state=['running'])
6969

7070
# Get the ID of the sandbox you want to connect to
71-
if len(running_sandboxes) == 0:
71+
if len(running_sandboxes.sandboxes) == 0:
7272
raise Exception("No running sandboxes found")
7373

74-
sandbox_id = running_sandboxes[0].sandbox_id
74+
sandbox_id = running_sandboxes.sandboxes[0].sandbox_id
7575

7676
# Connect to the sandbox
7777
sandbox = Sandbox.connect(sandbox_id) # $HighlightLine

apps/web/src/app/(docs)/docs/sandbox/list/page.mdx

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const sandbox = await Sandbox.create({
1818
},
1919
})
2020

21-
const runningSandboxes = await Sandbox.list() // $HighlightLine
22-
const runningSandbox = runningSandboxes[0]
21+
const runningSandboxes = await Sandbox.list({ state: ['running'] }) // $HighlightLine
22+
const runningSandbox = runningSandboxes.sandboxes[0]
2323
console.log('Running sandbox metadata:', runningSandbox.metadata)
2424
console.log('Running sandbox id:', runningSandbox.sandboxId)
2525
console.log('Running sandbox started at:', runningSandbox.startedAt)
@@ -34,8 +34,8 @@ sandbox = Sandbox({
3434
},
3535
})
3636

37-
running_sandboxes = sandbox.list() # $HighlightLine
38-
running_sandbox = running_sandboxes[0]
37+
running_sandboxes = sandbox.list(state=['running']) # $HighlightLine
38+
running_sandbox = running_sandboxes.sandboxes[0]
3939
print('Running sandbox metadata:', running_sandbox.metadata)
4040
print('Running sandbox id:', running_sandbox.sandbox_id)
4141
print('Running sandbox started at:', running_sandbox.started_at)
@@ -63,6 +63,9 @@ Running sandbox template id: 3e4rngfa34txe0gxc1zf
6363

6464

6565
## Filtering sandboxes
66+
<Note>
67+
This feature is in a private beta.
68+
</Note>
6669

6770
You can filter sandboxes by specifying <Link href="/docs/sandbox/metadata">Metadata</Link> key value pairs.
6871
Specifying multiple key value pairs will return sandboxes that match all of them.
@@ -83,10 +86,9 @@ const sandbox = await Sandbox.create({
8386
})
8487

8588
// List running sandboxes that has `userId` key with value `123` and `env` key with value `dev`.
86-
const runningSandboxes = await Sandbox.list({
87-
query: {
88-
metadata: { userId: '123', env: 'dev' }, // $HighlightLine
89-
},
89+
const { sandboxes } = await Sandbox.list({
90+
state: ['running'],
91+
filters: { userId: '123', env: 'dev' } // $HighlightLine
9092
})
9193
```
9294
```python
@@ -95,20 +97,17 @@ from e2b_code_interpreter import Sandbox
9597
# Create sandbox with metadata.
9698
sandbox = Sandbox(
9799
metadata={
98-
"env": "dev", # $HighlightLine
99-
"app": "my-app", # $HighlightLine
100-
"user_id": "123", # $HighlightLine
100+
"env": "dev", # $HighlightLine
101+
"app": "my-app", # $HighlightLine
102+
"user_id": "123", # $HighlightLine
101103
},
102104
)
103105

104106
# List running sandboxes that has `userId` key with value `123` and `env` key with value `dev`.
105107
running_sandboxes = Sandbox.list(
106-
query=SandboxQuery(
107-
metadata={
108-
"userId": "123", # $HighlightLine
109-
"env": "dev", # $HighlightLine
110-
}
111-
),
112-
)
108+
state=['running'],
109+
filters={
110+
"userId": "123", "env": "dev" # $HighlightLine
111+
})
113112
```
114113
</CodeGroup>

apps/web/src/app/(docs)/docs/sandbox/metadata/page.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ const sandbox = await Sandbox.create({
2323
})
2424

2525
// List running sandboxes and access metadata.
26-
const runningSandboxes = await Sandbox.list()
26+
const { sandboxes } = await Sandbox.list({ state: ['running'] })
2727
// Will print:
2828
// {
2929
// 'userId': '123',
3030
// }
31-
console.log(runningSandboxes[0].metadata)
31+
console.log(sandboxes[0].metadata)
3232
```
3333
```python
3434
from e2b_code_interpreter import Sandbox
@@ -41,12 +41,12 @@ sandbox = Sandbox(
4141
)
4242

4343
# List running sandboxes and access metadata.
44-
running_sandboxes = Sandbox.list()
44+
running_sandboxes = Sandbox.list(state=['running'])
4545
# Will print:
4646
# {
4747
# 'userId': '123',
4848
# }
49-
print(running_sandboxes[0].metadata)
49+
print(running_sandboxes.sandboxes[0].metadata)
5050
```
5151
</CodeGroup>
5252

apps/web/src/app/(docs)/docs/sandbox/persistence/page.mdx

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Sandbox persistence
22

33
<Note>
4-
Sandbox persistence is currently in public beta:
5-
1. You'll need to install the [beta version of the SDKs](#1-installing-the-beta-version-of-the-sdks).
4+
Sandbox persistence is currently in beta:
5+
1. [Reach out to us](/docs/support) with your use case to get access to the beta.
6+
1. You'll need to install the [beta version of the SDKs](#installing-the-beta-version-of-the-sdks).
67
1. Consider [some limitations](#limitations-while-in-beta).
78
1. The persistence is free for all users during the beta.
89
</Note>
@@ -127,6 +128,84 @@ print('Sandbox resumed', same_sbx.sandbox_id) # $HighlightLine
127128
```
128129
</CodeGroup>
129130

131+
## 4. Listing paused sandboxes
132+
You can list all paused sandboxes by calling the `Sandbox.list` method by supplying the `state` parameter.
133+
134+
<CodeGroup>
135+
```js
136+
import { Sandbox } from '@e2b/code-interpreter'
137+
// or use Core: https://github.com/e2b-dev/e2b
138+
// import { Sandbox } from 'e2b'
139+
//
140+
// or use Desktop: https://github.com/e2b-dev/desktop
141+
// import { Sandbox } from '@e2b/desktop'
142+
143+
// List all paused sandboxes
144+
const { sandboxes } = await Sandbox.list({ state: ['paused'] }) // $HighlightLine
145+
console.log('Paused sandboxes', sandboxes) // $HighlightLine
146+
```
147+
```python
148+
from e2b import Sandbox
149+
# or use Core: https://github.com/e2b-dev/e2b
150+
# from e2b import Sandbox
151+
#
152+
# or use Desktop: https://github.com/e2b-dev/desktop
153+
# from e2b_desktop import Sandbox
154+
155+
# List all paused sandboxes
156+
sandboxes = Sandbox.list(state=['paused']) # $HighlightLine
157+
print('Paused sandboxes', sandboxes.sandboxes) # $HighlightLine
158+
```
159+
</CodeGroup>
160+
161+
## 5. Removing paused sandboxes
162+
163+
You can remove paused (and running!) sandboxes by calling the `kill` method on the Sandbox instance.
164+
165+
<CodeGroup>
166+
```js
167+
import { Sandbox } from '@e2b/code-interpreter'
168+
// or use Core: https://github.com/e2b-dev/e2b
169+
// import { Sandbox } from 'e2b'
170+
//
171+
// or use Desktop: https://github.com/e2b-dev/desktop
172+
// import { Sandbox } from '@e2b/desktop'
173+
174+
const sbx = await Sandbox.create()
175+
console.log('Sandbox created', sbx.sandboxId)
176+
177+
// Pause the sandbox
178+
// You can save the sandbox ID in your database
179+
// to resume the sandbox later
180+
const sandboxId = await sbx.pause()
181+
182+
// Remove the sandbox
183+
await sbx.kill() // $HighlightLine
184+
185+
// Remove sandbox by id
186+
await Sandbox.kill(sandboxId) // $HighlightLine
187+
```
188+
```python
189+
from e2b import Sandbox
190+
# or use Core: https://github.com/e2b-dev/e2b
191+
# from e2b import Sandbox
192+
#
193+
# or use Desktop: https://github.com/e2b-dev/desktop
194+
# from e2b_desktop import Sandbox
195+
196+
sbx = Sandbox()
197+
198+
# Pause the sandbox
199+
sandbox_id = sbx.pause()
200+
201+
# Remove the sandbox
202+
sbx.kill() # $HighlightLine
203+
204+
# Remove sandbox by id
205+
Sandbox.kill(sandbox_id) # $HighlightLine
206+
```
207+
</CodeGroup>
208+
130209
## Sandbox's timeout
131210
When you resume a sandbox, the sandbox's timeout is reset to the default timeout of an E2B sandbox - 5 minutes.
132211

apps/web/src/code/js/basics/metadata.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ await sandbox.keepAlive(60_000)
1010

1111
// Later, can be even from another process
1212
// List all running sandboxes
13-
const runningSandboxes = await Sandbox.list()
13+
const { sandboxes } = await Sandbox.list({ state: ['running'] })
1414
// Find the sandbox by metadata
1515
const found = runningSandboxes.find(s => s.metadata?.userID === 'uniqueID')
1616
if (found) {

apps/web/src/code/python/basics/metadata.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
# Later, can be even from another process
1313
# List all running sandboxes
14-
running_sandboxes = Sandbox.list()
14+
running_sandboxes = Sandbox.list(state=['running'])
1515

16-
# Find the sandbox by metadata
17-
for running_sandbox in running_sandboxes:
16+
# Find the sandbox by metadata
17+
for running_sandbox in running_sandboxes.sandboxes:
1818
if running_sandbox.metadata.get("user_id", "") == 'uniqueID':
1919
sandbox = Sandbox.reconnect(running_sandbox.sandbox_id)
2020
break

packages/cli/src/commands/sandbox/kill.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as commander from 'commander'
33
import { ensureAPIKey } from 'src/api'
44
import { asBold } from 'src/utils/format'
55
import * as e2b from 'e2b'
6+
import { SandboxInfo } from 'e2b'
67

78
async function killSandbox(sandboxID: string, apiKey: string) {
89
const killed = await e2b.Sandbox.kill(sandboxID, { apiKey })
@@ -17,7 +18,7 @@ export const killCommand = new commander.Command('kill')
1718
.description('kill sandbox')
1819
.argument(
1920
'[sandboxID]',
20-
`kill the sandbox specified by ${asBold('[sandboxID]')}`,
21+
`kill the sandbox specified by ${asBold('[sandboxID]')}`
2122
)
2223
.alias('kl')
2324
.option('-a, --all', 'kill all running sandboxes')
@@ -28,31 +29,30 @@ export const killCommand = new commander.Command('kill')
2829
if (!sandboxID && !all) {
2930
console.error(
3031
`You need to specify ${asBold('[sandboxID]')} or use ${asBold(
31-
'-a/--all',
32-
)} flag`,
32+
'-a/--all'
33+
)} flag`
3334
)
3435
process.exit(1)
3536
}
3637

3738
if (all && sandboxID) {
3839
console.error(
3940
`You cannot use ${asBold('-a/--all')} flag while specifying ${asBold(
40-
'[sandboxID]',
41-
)}`,
41+
'[sandboxID]'
42+
)}`
4243
)
4344
process.exit(1)
4445
}
4546

4647
if (all) {
47-
const sandboxes = await e2b.Sandbox.list({ apiKey })
48-
48+
const { sandboxes } = await e2b.Sandbox.list({ apiKey })
4949
if (sandboxes.length === 0) {
50-
console.log('No running sandboxes')
50+
console.log('No sandboxes found')
5151
process.exit(0)
5252
}
5353

5454
await Promise.all(
55-
sandboxes.map((sandbox) => killSandbox(sandbox.sandboxId, apiKey)),
55+
sandboxes.map((sandbox) => killSandbox(sandbox.sandboxId, apiKey))
5656
)
5757
} else {
5858
await killSandbox(sandboxID, apiKey)

0 commit comments

Comments
 (0)