Appearance
Connect to a Running Sandbox
The SDK allows you to reconnect to an active sandbox instance using Sandbox.connect(). This is useful for reusing a sandbox after a period of inactivity.
Step 1: Retrieve the Sandbox ID
First, get the sandbox ID by listing running sandboxes with Sandbox.list().
javascript
import { Sandbox } from '@e2b/code-interpreter'
async function main() {
const sbx = await Sandbox.create()
const paginator = await Sandbox.list({
query: { state: ['running'] },
})
const runningSandboxes = await paginator.nextItems()
if (runningSandboxes.length === 0) {
throw new Error('No running sandboxes found')
}
const sandboxId = runningSandboxes[0].sandboxId
console.log('Sandbox ID:', sandboxId)
}
main()Step 2: Connect to the Sandbox
Once you have the ID, establish a connection and resume operations.
javascript
import { Sandbox } from '@e2b/code-interpreter'
async function main() {
const sandboxId = 'your-sandbox-id' // Use the ID from Step 1
const sandbox = await Sandbox.connect(sandboxId)
const result = await sandbox.commands.run("whoami")
console.log(`Running in sandbox ${sandbox.sandboxId} as "${result.stdout.trim()}"`)
await sandbox.kill()
}
main()