# Connect to the running sandbox
<subtitle>Connect to an existing sandbox by its sandbox ID and continue to control it using the SDK. </subtitle>

If you already have a running sandbox, you can connect to it using the `Sandbox.connect()` method and continue to control the sandbox through the SDK.

This is suitable for scenarios where you need to reuse the same sandbox instance after a short period of idleness, such as the same user session, the same remote development environment, or the same long-term task.

## 1. Get the sandbox ID

To connect to a running sandbox, you first need to get its sandbox ID. It can be queried through the `Sandbox.list()` method.

```python
from ucloud_sandbox import Sandbox

sbx = Sandbox.create()

# Get the sandbox list
paginator = Sandbox.list()

running_sandboxes = paginator.next_items()
if len(running_sandboxes) == 0:
    raise Exception("No connectable sandbox found")

# Get the sandbox ID to connect to
sandbox_id = running_sandboxes[0].sandbox_id
```

For more list query methods, please refer to [Sandbox List](/docs/agent-sandbox/sandbox/list.md).

## 2. Connect to the sandbox

Once you have the sandbox ID, you can use `Sandbox.connect()` to connect to the sandbox.

```python
from ucloud_sandbox import Sandbox

sandbox = Sandbox.connect(sandbox_id)

# After connecting, you can use the sandbox as usual
result = sandbox.commands.run("whoami")
print(f"Running in sandbox {sandbox.sandbox_id} as \"{result.stdout.strip()}\"")
```
