The Sandbox.list() method supports pagination. In the advanced pagination section, you can find more information about pagination techniques using the updated method.
Copy
Ask AI
import { Sandbox, SandboxInfo } from '@e2b/code-interpreter'const sandbox = await Sandbox.create( { metadata: { name: 'My Sandbox', // $HighlightLine }, },)const paginator = Sandbox.list() // $HighlightLine// Get the first page of sandboxes (running and paused)const firstPage = await paginator.nextItems() // $HighlightLineconst runningSandbox = firstPage[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)// Get the next page of sandboxesconst nextPage = await paginator.nextItems() // $HighlightLine
The code above will output something like this:
Copy
Ask AI
Running sandbox metadata: {name: "My Sandbox"}Running sandbox id: ixjj3iankaishgcge4jwn-b0b684e9Running sandbox started at: 2024-10-15T21:13:07.311ZRunning sandbox template id: 3e4rngfa34txe0gxc1zf
Filter sandboxes by their current state. The state parameter can contain either “running” for running sandboxes or “paused” for paused sandboxes, or both.
Copy
Ask AI
import { Sandbox } from '@e2b/code-interpreter'// Create a sandbox.const sandbox = await Sandbox.create()// List sandboxes that are running or paused.const paginator = Sandbox.list({ query: { state: ['running', 'paused'], // $HighlightLine },})const sandboxes = await paginator.nextItems() // $HighlightLine
Filter sandboxes by the metadata key value pairs specified during Sandbox creation.
Copy
Ask AI
import { Sandbox } from '@e2b/code-interpreter'// Create sandbox with metadata.const sandbox = await Sandbox.create({ metadata: { env: 'dev', // $HighlightLine app: 'my-app', // $HighlightLine userId: '123', // $HighlightLine },})// List all sandboxes that has `userId` key with value `123` and `env` key with value `dev`.const paginator = Sandbox.list({ query: { metadata: { userId: '123', env: 'dev' }, // $HighlightLine },})const sandboxes = await paginator.nextItems() // $HighlightLine
For more granular pagination, you can set custom per-page item limit (default and maximum is 100) and specify an offset parameter (nextToken or next_token) to start paginating from.
Copy
Ask AI
import { Sandbox } from '@e2b/code-interpreter'const paginator = Sandbox.list({ limit: 100, // $HighlightLine nextToken: '<base64-encoded-token>', // $HighlightLine})// Additional paginator properties// Whether there is a next pagepaginator.hasNext// Next page tokenpaginator.nextToken// Fetch the next pageawait paginator.nextItems() // $HighlightLine
You can fetch all pages by looping through the paginator while checking if there is a next page (using hasNext or has_next property) and fetching until there are no more pages left to fetch:
Copy
Ask AI
import { Sandbox } from '@e2b/code-interpreter'const paginator = Sandbox.list()// Loop through all pagesconst sandboxes: SandboxInfo[] = []while (paginator.hasNext) { // $HighlightLine const items = await paginator.nextItems() sandboxes.push(...items)}
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.
Copy
Ask AI
import { Sandbox } from '@e2b/code-interpreter'// Create sandbox with metadata.const sandbox = await Sandbox.create({ metadata: { env: 'dev', // $HighlightLine app: 'my-app', // $HighlightLine userId: '123', // $HighlightLine },})// List running sandboxes that has `userId` key with value `123` and `env` key with value `dev`.const runningSandboxes = await Sandbox.list({ query: { metadata: { userId: '123', env: 'dev' }, // $HighlightLine },})