> ## Documentation Index
> Fetch the complete documentation index at: https://docs.novita.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect to sandbox

You can use the `novita.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 user's web app session, reconnecting to long-running work, or resuming a paused sandbox after a short period of inactivity.

## Connect to the sandbox

Before connecting, you need the sandbox ID. You can obtain it with `novita.sandbox.list()`, then pass it to `novita.sandbox.connect()`.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Novita, SandboxState } from 'novita-sandbox'

  const novita = new Novita()

  // --- Get the sandbox ID ---
  // Get all running sandboxes
  const runningSandboxesPaginator = await novita.sandbox.list({
    query: {
      state: [SandboxState.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 novita.sandbox.connect(runningSandboxId)
  console.log('connected to sandbox: ', sandbox.sandboxId)

  // Now you can use the sandbox as usual
  // ...
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox import Novita, SandboxQuery, SandboxState

  novita = Novita()

  # --- Get the sandbox ID ---
  # Get all running sandboxes
  running_sandboxes_paginator = novita.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 = novita.sandbox.connect(running_sandbox_id)
  print("connected to sandbox: ", sandbox.sandbox_id)

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

  sandbox.kill()
  ```

  ```bash CLI icon="terminal" theme={"system"}
  # Attach an interactive terminal to a running sandbox (alias: sandbox cn)
  novita-sandbox-cli sandbox connect <sandboxID>
  ```
</CodeGroup>
