Skip to main content
You can use the Sandbox.connect() method to connect to a running sandbox, and then start controlling it with our SDK. This is useful when reusing the same sandbox instance for a user after a short period of inactivity.

1. Get the sandbox ID

You need to get the ID of the running sandbox before connection. You can use the Sandbox.list() method.
import { Sandbox } from "novita-sandbox/code-interpreter"

// Get all running sandboxes
const runningSandboxesPaginator = await Sandbox.list({
  query: {
    state: ["running"],
  },
})

const runningSandboxes = await runningSandboxesPaginator.nextItems()
if (runningSandboxes.length === 0) {
  throw new Error("No running sandboxes found")
}
const runningSandboxId = runningSandboxes[0].sandboxId

console.log(`got a running sandbox: ${runningSandboxId}`)

2. Connect to the sandbox

You can use the Sandbox.connect() method to connect to the sandbox once you have the sandbox ID.
import { Sandbox } from "novita-sandbox/code-interpreter"

// Get all running sandboxes
const runningSandboxesPaginator = await Sandbox.list({
  query: {
    state: ["running"],
  },
})

const runningSandboxes = await runningSandboxesPaginator.nextItems()
if (runningSandboxes.length === 0) {
  throw new Error("No running sandboxes found")
}
const runningSandboxId = runningSandboxes[0].sandboxId

// connect to the sandbox.
const sandbox = await Sandbox.connect(runningSandboxId)
console.log("connected to sandbox: ", sandbox.sandboxId)

// Now you can use the sandbox as usual
// ...
Last modified on June 5, 2026