沙箱实例
更新时间:2025-11-06
前提条件
- 请按照文档配置环境变量。
- 目前仅支持官方代码沙箱,即template名为 code-interpreter-v1。
创建代码沙箱
创建默认代码沙箱实例:
1from e2b_code_interpreter import Sandbox
2sandbox = Sandbox.create()
1import { Sandbox } from '@e2b/code-interpreter'
2const sandbox = await Sandbox.create()
创建指定模板和超时时间的代码沙箱实例
1from e2b_code_interpreter import Sandbox
2# 创建一个代码沙箱,指定模板为code-interpreter-v1
3# 您可以在创建时指定超时时间,不指定时默认为300s
4# 当沙箱的运行时间超过超时时间时,沙箱将会被自动删除
5sandbox = Sandbox.create(
6 template="code-interpreter-v1",
7 timeout=60 # 单位为秒
8)
1import { Sandbox } from '@e2b/code-interpreter'
2
3// 创建一个沙箱,其超时设置为60s,注意单位是毫秒
4const sandbox = await Sandbox.create({
5 timeoutMs: 60_000,
6})
获取沙箱信息
1from e2b_code_interpreter import Sandbox
2
3# 创建一个沙箱,其超时设置为60s
4sandbox = Sandbox.create(timeout=60)
5
6# 获取沙箱信息
7info = sandbox.get_info()
8
9print(info)
10
11# SandboxInfo(sandbox_id='abcddxxxxxxx',
12# template_id='code-interpreter-v1',
13# metadata={},
14# started_at=datetime.datetime(2025, 10, 24, 15, 42, 59, 255612, tzinfo=tzutc()),
15# end_at=datetime.datetime(2025, 10, 24, 15, 47, 59, 255612, tzinfo=tzutc())
16# )
1import { Sandbox } from '@e2b/code-interpreter'
2
3// 创建一个沙箱,其超时设置为60s
4const sandbox = await Sandbox.create({ timeoutMs: 60_000 })
5
6// 获取沙箱信息
7const info = await sandbox.getInfo()
8
9console.log(info)
10
11// {
12// "sandboxId": "xxxxxxxxxxxx",
13// "templateId": "xxxxx",
14// "name": "xxx",
15// "metadata": {},
16// "startedAt": "2025-10-24T15:37:58.076Z",
17// "endAt": "2025-10-24T15:42:58.076Z"
18// }
删除沙箱
然后在您的代码中使用 dotenv 库来引入,通过该种方式引入的环境变量在当前代码范围内生效。
1from e2b_code_interpreter import Sandbox
2# 创建沙箱
3sandbox = Sandbox.create()
4# 删除沙箱
5sandbox.kill()
1import { Sandbox } from '@e2b/code-interpreter'
2// 创建沙箱
3const sandbox = await Sandbox.create({ timeoutMs: 60_000 })
4
5// 立即删除沙箱
6await sandbox.kill()
更新沙箱生存时间
您可以更新已在运行中的沙箱的生存超时时间。
1from e2b_code_interpreter import Sandbox
2
3# 创建一个60s超时的沙箱
4sandbox = Sandbox.create(timeout=60)
5
6# 更新沙箱的超时时间为 30s
7sandbox.set_timeout(30)
1import { Sandbox } from '@e2b/code-interpreter'
2
3// 创建一个60s超时的沙箱
4const sandbox = await Sandbox.create({ timeoutMs: 60_000 })
5
6// 更新沙箱的超时时间为 30s
7await sandbox.setTimeout(30_000)
获取沙箱列表
1from e2b_code_interpreter import Sandbox
2# 调用list方法获取分页器
3paginator = Sandbox.list()
4# 若分页器还有下一页,则取出下一页
5while paginator.has_next:
6 page = paginator.next_items()
7 # 打印当前取出页的沙箱实例信息
8 for sandbox_info in page:
9 print(sandbox_info)
10# 也可以指定一页中有多少实例
11paginator_limit_5 = Sandbox.list(limit=5)
12# 也可以获取当前分页器的偏移值
13print(paginator_limit_5.next_token) # None
14if paginator_limit_5.has_next:
15 paginator_limit_5.next_items()
16print(paginator_limit_5.next_token) # offset_5
1import { Sandbox } from '@e2b/code-interpreter'
2
3// 创建沙箱
4const sandbox = await Sandbox.create({ timeoutMs: 60_000 })
5// 获取沙箱列表
6const paginator = Sandbox.list()
7
8// 获取沙箱列表的第一页
9const firstPage = await paginator.nextItems()
10
11const runningSandbox = firstPage[0]
12
13console.log('Running sandbox metadata:', runningSandbox.metadata)
14console.log('Running sandbox id:', runningSandbox.sandboxId)
15console.log('Running sandbox started at:', runningSandbox.startedAt)
16console.log('Running sandbox template id:', runningSandbox.templateId)
17
18// 获取沙箱列表下一页
19const nextPage = await paginator.nextItems()
连接到已running沙箱
获取沙箱ID
1from e2b_code_interpreter import Sandbox
2
3# 获取运行中沙箱列表
4paginator = Sandbox.list()
5
6# 获取一个你想连接的沙箱id
7running_sandboxes = paginator.next_items()
8if len(running_sandboxes) == 0:
9 raise Exception("No running sandboxes found")
10
11# 获取一个你想连接的沙箱id
12sandbox_id = running_sandboxes[0].sandbox_id
1import { Sandbox } from '@e2b/code-interpreter'
2const sbx = await Sandbox.create()
3
4// 获取运行中沙箱列表
5const paginator = await Sandbox.list({
6 query: { state: ['running'] },
7})
8// 获取一个你想连接的沙箱id
9const runningSandboxes = await paginator.nextItems()
10if (runningSandboxes.length === 0) {
11 throw new Error('No running sandboxes found')
12}
13
14// 获取一个你想连接的沙箱id
15const sandboxId = runningSandboxes[0].sandboxId
连接沙箱
1from e2b_code_interpreter import Sandbox
2
3# 获取运行中沙箱列表
4running_sandboxes = Sandbox.list()
5
6# 获取一个你想连接的沙箱id
7if len(running_sandboxes) == 0:
8 raise Exception("No running sandboxes found")
9
10sandbox_id = running_sandboxes[0].sandbox_id
11
12# 连接沙箱
13sandbox = Sandbox.connect(sandbox_id)
14# 现在您可以直接使用沙箱了
15# ...
1import { Sandbox } from "@e2b/code-interpreter"
2
3// 获取运行中沙箱列表
4const runningSandboxes = await Sandbox.list()
5
6if (runningSandboxes.length === 0) {
7 throw new Error("No running sandboxes found")
8}
9
10// 获取一个你想连接的沙箱id
11const sandboxId = runningSandboxes[0].sandboxId
12
13// 连接沙箱
14const sandbox = await Sandbox.connect(sandboxId)
15// 现在您可以直接使用沙箱了
16// ...
