# Connect to running sandbox (/docs/sandbox/connect)

<!-- agent-signals: reading_time_min: 2 · est_tokens: 819 · updated: 2026-07-30 -->
Related: [Auto-resume on request](/docs/sandbox/auto-resume.md), [Environment variables](/docs/sandbox/environment-variables.md), [Filesystem-only snapshots](/docs/sandbox/filesystem-only-snapshots.md), [Sandbox forking](/docs/sandbox/fork.md), [Git integration](/docs/sandbox/git-integration.md), [Monitor sandbox lifecycle events](/docs/sandbox/lifecycle-events-api.md)

If you have a running sandbox, you can connect to it using the `Sandbox.connect()` method and then start controlling it with our SDK.

This is useful if you want to, for example, reuse the same sandbox instance for the same user after a short period of inactivity.

## 1. Get the sandbox ID [#1-get-the-sandbox-id]

To connect to a running sandbox, you first need to retrieve its ID. You can do this by calling the `Sandbox.list()` method.

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```js  
      import { Sandbox } from 'e2b'

      const sbx = await Sandbox.create()

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

      const runningSandboxes = await paginator.nextItems()
      if (runningSandboxes.length === 0) {
        throw new Error('No running sandboxes found')
      }

      // Get the ID of the sandbox you want to connect to
      const sandboxId = runningSandboxes[0].sandboxId
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```python  
      from e2b import Sandbox

      sbx = Sandbox.create()

      # Get all running sandboxes
      paginator = Sandbox.list()

      # Get the ID of the sandbox you want to connect to
      running_sandboxes = paginator.next_items()
      if len(running_sandboxes) == 0:
          raise Exception("No running sandboxes found")

      # Get the ID of the sandbox you want to connect to
      sandbox_id = running_sandboxes[0].sandbox_id
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>

## 2. Connect to the sandbox [#2-connect-to-the-sandbox]

Now that you have the sandbox ID, you can connect to the sandbox using the `Sandbox.connect()` method.

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```js highlight={3}  
      import { Sandbox } from 'e2b'

      const sandbox = await Sandbox.connect(sandboxId)

      // Now you can use the sandbox as usual
      // ...
      const result = await sandbox.commands.run("whoami")
      console.log(`Running in sandbox ${sandbox.sandboxId} as "${result.stdout.trim()}"`)
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```python highlight={3}  
      from e2b import Sandbox

      sandbox = Sandbox.connect(sandbox_id)

      # Now you can use the sandbox as usual
      # ...
      r = sandbox.commands.run("whoami")
      print(f"Running in sandbox {sandbox.sandbox_id} as \"{r.stdout.strip()}\"")
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>
