Skip to main content
When autoResume is enabled, a paused sandbox automatically resumes when it receives activity (an SDK call or inbound HTTP request). You don’t need to check sandbox state or call Sandbox.connect() yourself. Configure it through the lifecycle object when creating a sandbox.
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create({
  timeoutMs: 10 * 60 * 1000,
  lifecycle: {
    onTimeout: 'pause',
    autoResume: true,
  },
})

Lifecycle options

OptionValueDescription
onTimeout / on_timeoutkill (default)Sandbox is terminated when timeout is reached
pauseSandbox is paused when timeout is reached
autoResume / auto_resumefalse (default)Paused sandboxes stay paused
truePaused sandboxes resume on activity. Requires onTimeout: "pause"
To resume a paused sandbox without auto-resume, use Sandbox.connect().

Examples

Web server

Auto-resume is useful for web or preview servers that should wake up on inbound traffic.
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create({
  timeoutMs: 10 * 60 * 1000,
  lifecycle: {
    onTimeout: 'pause',
    autoResume: true,
  },
})

await sandbox.commands.run(
  `python3 -m pip -q install 'flask>=2.2'`
)

await sandbox.files.write(
  '/home/user/app.py',
  [
    'from flask import Flask',
    'app = Flask(__name__)',
    '@app.route("/")',
    'def hello():',
    '    return "Hello, World!"',
    'app.run(host="0.0.0.0", port=3000)',
    '',
  ].join('\n')
)

await sandbox.commands.run(
  'python3 -u /home/user/app.py > /home/user/flask.log 2>&1',
  { background: true }
)

await new Promise((resolve) => setTimeout(resolve, 1000))

const previewHost = sandbox.getHost(3000)
console.log(`Preview URL: https://${previewHost}`)

Agent tool calls

Create a sandbox once and reuse it across tool calls. If it gets paused between calls, it resumes automatically.
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create({
  timeoutMs: 5 * 60 * 1000,
  lifecycle: {
    onTimeout: 'pause',
    autoResume: true,
  },
})

async function runToolTask(command) {
  const result = await sandbox.commands.run(command)
  return result.stdout
}

console.log(await runToolTask('python -c "print(2 + 2)"'))

Per-user sandboxes

Keep a map of sandbox instances by user. Each sandbox auto-resumes when the user makes a new request.
import { Sandbox } from 'e2b'

const userSandboxes = new Map()

async function getSandbox(userId) {
  let sandbox = userSandboxes.get(userId)

  if (!sandbox) {
    sandbox = await Sandbox.create({
      timeoutMs: 5 * 60 * 1000,
      lifecycle: {
        onTimeout: 'pause',
        autoResume: true,
      },
    })
    userSandboxes.set(userId, sandbox)
  }

  return sandbox
}

const sandbox = await getSandbox('user-123')
const result = await sandbox.commands.run('echo "Hello from your sandbox"')
console.log(result.stdout)

Cleanup

Auto-resume is persistent — if a resumed sandbox times out again, it pauses again (rather than being killed). Calling .kill() permanently deletes the sandbox.