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 'novita-sandbox/code-interpreter'// Create a sandbox with metadata.const sandbox = await Sandbox.create({ metadata: { env: 'dev', app: 'my-app', userId: '123', },})// List all running sandboxes that has `userId` key with value `123` and `env` key with value `dev`.const runningSandboxesPaginator = await Sandbox.list({ query: { metadata: { userId: '123', env: 'dev' }, },})const runningSandboxes = await runningSandboxesPaginator.nextItems()for (const runningSandbox of runningSandboxes) { console.log(`list running sandbox (${runningSandbox.sandboxId}) metadata:`, runningSandbox.metadata)}await sandbox.kill()
The Sandbox.list() method now supports pagination. In the advanced pagination section, you can find more information about pagination techniques using the updated method.
import { Sandbox } from 'novita-sandbox/code-interpreter'const sandbox = await Sandbox.create()const paginator = Sandbox.list()// Get the first page of sandboxes (running and paused)const firstPage = await paginator.nextItems()if (paginator.hasNext) { // Get the next page of sandboxes const nextPage = await paginator.nextItems()}await sandbox.kill()
Filter sandboxes by their current state. The state parameter can contain either “running” for running sandboxes or “paused” for paused sandboxes, or both.
import { Sandbox } from 'novita-sandbox/code-interpreter'const sandbox = await Sandbox.create()// List all sandboxes that are running or paused.const paginator = Sandbox.list({ query: { state: ['running', 'paused'], },})const sandboxes = await paginator.nextItems()await sandbox.kill()
Filter sandboxes by the metadata key value pairs specified during Sandbox creation.
import { Sandbox } from 'novita-sandbox/code-interpreter'// Create sandbox with metadata.const sandbox = await Sandbox.create({ metadata: { env: 'dev', app: 'my-app', userId: '123', },})// 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' }, },})const sandboxes = await paginator.nextItems()await sandbox.kill()
For more granular pagination, you can set custom per-page item limit (default and maximum is 1000) and specify an offset parameter (nextToken or next_token) to start paginating from.
import { Sandbox } from 'novita-sandbox/code-interpreter'const sandbox = await Sandbox.create()const paginator = Sandbox.list({ limit: 1000, // nextToken: '<base64-encoded-token>',})// Fetch the next pageawait paginator.nextItems()// Additional paginator properties// Whether there is a next pageconsole.log("paginator.hasNext: ", paginator.hasNext)// Next page tokenconsole.log("paginator.nextToken: ", paginator.nextToken)await sandbox.kill()
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: