Skip to content
Lifecycle

Sandbox Lifecycle

This document covers the management of sandbox instances, including creation, timeout configuration, information retrieval, and shutdown procedures.

Default Behavior

Default behavior: A sandbox runs for 5 minutes by default, but custom timeout settings are supported.

Runtime limits:

  • The maximum runtime for a single sandbox is 1 hour by default
  • For longer runtimes, contact us to request an adjustment

Creating a Sandbox with a Timeout

javascript
import { Sandbox } from '@e2b/code-interpreter'

// Create sandbox and keep it running for 60 seconds.
// Note: The units are milliseconds.
async function main() {
  const sandbox = await Sandbox.create({
    timeoutMs: 60_000,
  })
  await sandbox.kill()
}

main()

Note: JavaScript/TypeScript uses milliseconds as the unit.

Changing the Timeout at Runtime

You can reset the lifecycle of a running sandbox:

javascript
import { Sandbox } from '@e2b/code-interpreter'

async function main() {
  // Create sandbox and keep it running for 60 seconds.
  const sandbox = await Sandbox.create({ timeoutMs: 60_000 })

  // Change the sandbox timeout to 30 seconds.
  // The new timeout will be 30 seconds from now.
  await sandbox.setTimeout(30_000)

  await sandbox.kill()
}

main()

Important: The new timeout is calculated from the current moment.

Retrieving Sandbox Information

Use the getInfo() or get_info() method to access the sandbox ID, template, metadata, and timestamps.

javascript
import { Sandbox } from '@e2b/code-interpreter'

async function main() {
  // Create sandbox and keep it running for 60 seconds.
  const sandbox = await Sandbox.create({ timeoutMs: 60_000 })

  // Retrieve sandbox information.
  const info = await sandbox.getInfo()

  console.log(info)

  // {
  //   "sandboxId": "iiny0783cype8gmoawzmx-ce30bc46",
  //   "templateId": "rki5dems9wqfm4r03t7g",
  //   "name": "base",
  //   "metadata": {},
  //   "startedAt": "2025-03-24T15:37:58.076Z",
  //   "endAt": "2025-03-24T15:42:58.076Z"
  // }

  await sandbox.kill()
}

main()

Shutting Down a Sandbox

The kill() method immediately terminates a sandbox, regardless of the remaining timeout.

javascript
import { Sandbox } from '@e2b/code-interpreter'

async function main() {
  // Create sandbox and keep it running for 60 seconds.
  const sandbox = await Sandbox.create({ timeoutMs: 60_000 })

  // Shutdown the sandbox immediately.
  await sandbox.kill()
}

main()

Use Cases

Supports interactive applications by extending sandbox duration based on user activity, preventing premature termination of active sessions.