List running sandboxes

You can list all running sandboxes using the Sandbox.list() method.

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

const sandbox = await Sandbox.create({
  metadata: {
    name: 'My Sandbox',
  },
})

const runningSandboxes = await Sandbox.list() 
const runningSandbox = runningSandboxes[0]
console.log('Running sandbox metadata:', runningSandbox.metadata)
console.log('Running sandbox id:', runningSandbox.sandboxId)
console.log('Running sandbox started at:', runningSandbox.startedAt)
console.log('Running sandbox template id:', runningSandbox.templateId)

The code above will output something like this:

Terminal
Running sandbox metadata: {
  name: "My Sandbox",
}
Running sandbox id: ixjj3iankaishgcge4jwn-b0b684e9
Running sandbox started at: 2024-10-15T21:13:07.311Z
Running sandbox template id: 3e4rngfa34txe0gxc1zf

Filtering sandboxes

You can filter sandboxes by specifying Metadata key value pairs. Specifying multiple key value pairs will return sandboxes that match all of them.

This can be useful when you have a large number of sandboxes and want to find only specific ones. The filtering is performed on the server.

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

// Create sandbox with metadata.
const sandbox = await Sandbox.create({
  metadata: {
    env: 'dev', 
    app: 'my-app', 
    userId: '123', 
  },
})

// List running sandboxes that has `userId` key with value `123` and `env` key with value `dev`.
const runningSandboxes = await Sandbox.list({
    filters: { userId: '123', env: 'dev' } 
})