# Connect to running sandbox - Documentation

> For the complete documentation index, see [llms.txt](/llms.txt). Markdown is available with `Accept: text/markdown` and `.md` URL variants.

Source: /docs/guides/sandbox-connect

# Connect to running sandbox

You can use the `Sandbox.connect()` method to connect to a running or paused sandbox and continue controlling it with the SDK.
This is useful for reusing the same sandbox instance across serverless function invocations, restoring a web app user session, reconnecting to long-running work, or resuming a paused sandbox after a short period of inactivity.

##

[​](#1-get-the-sandbox-id)

1. Get the sandbox ID

You need to get the ID of the sandbox before connecting. You can use the `Sandbox.list()` method.

JavaScript & TypeScript

Python

```
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}`)
```

```
from novita_sandbox.code_interpreter import Sandbox, SandboxQuery, SandboxState

sandbox = Sandbox.create()

# Get all running sandboxes
running_sandboxes_paginator = Sandbox.list(query=SandboxQuery(state=[SandboxState.RUNNING]))

running_sandboxes = running_sandboxes_paginator.next_items()
if len(running_sandboxes) == 0:
raise Exception("No running sandboxes found")
running_sandbox_id = running_sandboxes[0].sandbox_id

# got a running sandbox.
print("got a running sandbox: ", running_sandbox_id)

sandbox.kill()
```

##

[​](#2-connect-to-the-sandbox)

2. Connect to the sandbox

You can use the `Sandbox.connect()` method to connect to the sandbox once you have the sandbox ID.

JavaScript & TypeScript

Python

```
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
// ...
```

```
from novita_sandbox.code_interpreter import Sandbox, SandboxQuery, SandboxState

sandbox = Sandbox.create()

# Get all running sandboxes.
running_sandboxes_paginator = Sandbox.list(query=SandboxQuery(state=[SandboxState.RUNNING]))

running_sandboxes = running_sandboxes_paginator.next_items()
if len(running_sandboxes) == 0:
raise Exception("No running sandboxes found")
running_sandbox_id = running_sandboxes[0].sandbox_id

# connect to the sandbox.
sandbox = Sandbox.connect(running_sandbox_id)
print("got a running sandbox: ", sandbox.sandbox_id)

# Now you can use the sandbox as usual
# ...

sandbox.kill()
```

Last modified on June 24, 2026
