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

# 実行中のサンドボックスに接続

export const SandboxConfigHint = () => {
  if (typeof document === "undefined") {
    return null;
  } else {
    return <Note>Before running the example code in this document, please ensure you have properly configured environment variables. For details, please refer to <a href="/docs/ja/guides/sandbox-your-first-agent-sandbox#configure-environment-variables">Configure Environment Variables</a>.</Note>;
  }
};

`Sandbox.connect()` メソッドを使用すると、実行中または一時停止中のサンドボックスに接続し、SDK で引き続き制御できます。

これは、サーバーレス関数の呼び出し間で同じサンドボックスインスタンスを再利用する、Web アプリのユーザーセッションを復元する、長時間実行中の作業に再接続する、または短時間の非アクティブ後に一時停止中のサンドボックスを再開する場合に便利です。

<SandboxConfigHint />

## 1. サンドボックス ID を取得する

接続する前に、サンドボックスの ID を取得する必要があります。`Sandbox.list()` メソッドを使用できます。

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  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}`)
  ```

  ```python Python icon="python" theme={"system"}
  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()
  ```
</CodeGroup>

## 2. サンドボックスに接続する

サンドボックス ID を取得したら、`Sandbox.connect()` メソッドを使用してサンドボックスに接続できます。

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  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
  // ...
  ```

  ```python Python icon="python" theme={"system"}
  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()
  ```
</CodeGroup>
