Fork a sandbox
The snapshot is taken once however many forks you ask for — up to 100 in one request — which makes forking the cheap way to fan an agent out into parallel attempts.
The original sandbox pauses briefly while it is forked, which drops every active connection — WebSockets, PTYs, command streams. Reconnect afterwards. The pause is longer the more the disk has changed since the last snapshot.
Fork once#
import { Sandbox } from 'e2b'
const sandbox = await Sandbox.create()
await sandbox.files.write('/home/user/state.txt', 'shared state')
const [fork] = await sandbox.fork()
if (fork instanceof Sandbox) {
// The fork starts with the original's files, processes, and memory
await fork.commands.run('cat /home/user/state.txt')
}from e2b import Sandbox
sandbox = Sandbox.create()
sandbox.files.write('/home/user/state.txt', 'shared state')
fork, = sandbox.fork()
if isinstance(fork, Sandbox):
# The fork starts with the original's files, processes, and memory
fork.commands.run('cat /home/user/state.txt')e2b sandbox fork <sandboxID>Fork many, handle partial failure#
A partial failure does not fail the call: the returned list holds Sandbox instances for the forks that started and error values for those that did not.
const results = await sandbox.fork({ count: 5, timeoutMs: 60_000 })
const forks = results.filter((r) => r instanceof Sandbox)
const errors = results.filter((r) => !(r instanceof Sandbox))
for (const error of errors) {
console.error('Fork failed:', error.message)
}results = sandbox.fork(count=5, timeout=60)
forks = [r for r in results if isinstance(r, Sandbox)]
errors = [r for r in results if not isinstance(r, Sandbox)]
for error in errors:
print('Fork failed:', error)You can also fork by ID without holding a sandbox object: Sandbox.fork(sandboxId) in JavaScript, Sandbox.fork(sandbox_id) in Python.
Forking or snapshots?#
Forking gives you running copies now, in one call. A snapshot is a persistent checkpoint you create sandboxes from later, as many times as you like.