Learn how to connect to running HopX sandboxes and establish communication channels. Connect to existing sandboxes by ID, retrieve sandbox information, and resume work with previously created sandboxes. Essential for managing long-running sandboxes and resuming interrupted sessions. Includes Python and JavaScript SDK examples and REST API endpoints.
Connect to sandboxes that are already running. This is useful when you need to reconnect to a sandbox after your application restarts, or when working with sandboxes created by other processes.
from hopx_ai import Sandbox# Connect to existing sandboxsandbox = Sandbox.connect("1763309903rt5vs4lm")# Get current informationinfo = sandbox.get_info()print(f"Status: {info.status}")print(f"URL: {info.public_host}")
import { Sandbox } from '@hopx-ai/sdk';// Connect to existing sandboxconst sandbox = await Sandbox.connect('1763309903rt5vs4lm');// Get current informationconst info = await sandbox.getInfo();console.log(`Status: ${info.status}`);console.log(`URL: ${info.publicHost}`);
You can find sandbox IDs by listing your sandboxes:
Python
JavaScript
from hopx_ai import Sandbox# List sandboxes to find IDssandboxes = Sandbox.list(status="running")for sandbox in sandboxes: info = sandbox.get_info() print(f"ID: {sandbox.sandbox_id}, Status: {info.status}")# Connect to the first oneif sandboxes: sandbox = Sandbox.connect(sandboxes[0].sandbox_id) print(f"Connected to: {sandbox.sandbox_id}")
Expected Output:
ID: 1763309903rt5vs4lm, Status: runningID: 1763309904rt5vs4ln, Status: runningConnected to: 1763309903rt5vs4lm
import { Sandbox } from '@hopx-ai/sdk';// List sandboxes to find IDsconst sandboxes = await Sandbox.list({ status: 'running' });for (const sandbox of sandboxes) { const info = await sandbox.getInfo(); console.log(`ID: ${sandbox.sandboxId}, Status: ${info.status}`);}// Connect to the first oneif (sandboxes.length > 0) { const sandbox = await Sandbox.connect(sandboxes[0].sandboxId); console.log(`Connected to: ${sandbox.sandboxId}`);}
The connect() method handles different sandbox states automatically:
Python
JavaScript
from hopx_ai import Sandboxfrom hopx_ai.errors import HopxErrortry: sandbox = Sandbox.connect("sandbox_id") info = sandbox.get_info() if info.status == "paused": print("Sandbox was paused - automatically resumed") elif info.status == "running": print("Sandbox is running - ready to use")except HopxError as e: if "stopped" in str(e).lower(): print("Cannot connect to stopped sandbox") print("Use sandbox.start() first, or create a new sandbox")
import { Sandbox } from '@hopx-ai/sdk';try { const sandbox = await Sandbox.connect('sandbox_id'); const info = await sandbox.getInfo(); if (info.status === 'paused') { console.log('Sandbox was paused - automatically resumed'); } else if (info.status === 'running') { console.log('Sandbox is running - ready to use'); }} catch (error) { if (error.message.includes('stopped')) { console.error('Cannot connect to stopped sandbox'); console.error('Use sandbox.start() first, or create a new sandbox'); }}
from hopx_ai import Sandboximport os# Store sandbox ID (e.g., in environment variable or database)sandbox_id = os.getenv("SANDBOX_ID")if sandbox_id: # Reconnect to existing sandbox sandbox = Sandbox.connect(sandbox_id) print(f"Reconnected to: {sandbox.sandbox_id}")else: # Create new sandbox sandbox = Sandbox.create(template="code-interpreter") print(f"Created new: {sandbox.sandbox_id}") # Store ID for later os.environ["SANDBOX_ID"] = sandbox.sandbox_id
import { Sandbox } from '@hopx-ai/sdk';// Store sandbox ID (e.g., in environment variable or database)const sandboxId = process.env.SANDBOX_ID;if (sandboxId) { // Reconnect to existing sandbox const sandbox = await Sandbox.connect(sandboxId); console.log(`Reconnected to: ${sandbox.sandboxId}`);} else { // Create new sandbox const sandbox = await Sandbox.create({ template: 'code-interpreter' }); console.log(`Created new: ${sandbox.sandboxId}`); // Store ID for later process.env.SANDBOX_ID = sandbox.sandboxId;}
from hopx_ai import Sandbox# Multiple processes can connect to the same sandbox# Process 1: Creates sandboxsandbox1 = Sandbox.create(template="code-interpreter")sandbox_id = sandbox1.sandbox_idprint(f"Created: {sandbox_id}")# Process 2: Connects to same sandboxsandbox2 = Sandbox.connect(sandbox_id)print(f"Connected: {sandbox2.sandbox_id}")# Both can use the same sandboxresult1 = sandbox1.run_code('x = 1')result2 = sandbox2.run_code('print(x)') # Can access shared state
import { Sandbox } from '@hopx-ai/sdk';// Multiple processes can connect to the same sandbox// Process 1: Creates sandboxconst sandbox1 = await Sandbox.create({ template: 'code-interpreter' });const sandboxId = sandbox1.sandboxId;console.log(`Created: ${sandboxId}`);// Process 2: Connects to same sandboxconst sandbox2 = await Sandbox.connect(sandboxId);console.log(`Connected: ${sandbox2.sandboxId}`);// Both can use the same sandboxawait sandbox1.runCode('x = 1');await sandbox2.runCode('print(x)'); // Can access shared state
When you connect to a sandbox, the SDK automatically:
Refreshes the JWT token for agent authentication
Stores the token for subsequent agent operations
Handles token expiration automatically
You don’t need to manage tokens manually - the SDK handles this for you.
Python
JavaScript
from hopx_ai import Sandbox# Connect - token is automatically refreshedsandbox = Sandbox.connect("sandbox_id")# Token is stored and used automatically for agent callsresult = sandbox.run_code('print("Hello")') # Uses stored token# Manually refresh if neededsandbox.refresh_token()
import { Sandbox } from '@hopx-ai/sdk';// Connect - token is automatically refreshedconst sandbox = await Sandbox.connect('sandbox_id');// Token is stored and used automatically for agent callsconst result = await sandbox.runCode('print("Hello")'); // Uses stored token// Manually refresh if neededawait sandbox.refreshToken();