Start, stop, pause, and resume sandboxes to control their lifecycle. Learn how to manage sandbox state transitions, free resources when not in use, pause execution temporarily, and resume paused sandboxes. Essential for resource management and workflow control. Includes Python and JavaScript SDK examples and REST API endpoints.
Control the lifecycle of your sandboxes by starting, stopping, pausing, and resuming them. This allows you to manage resources efficiently and maintain state across sessions.
When you pause a sandbox, its state is preserved. When you resume it, everything continues from where it left off. Stopped sandboxes lose their runtime state but can be restarted.
Stop a running sandbox (preserves filesystem but loses runtime state):
Python
JavaScript
Copy
from hopx_ai import Sandboxsandbox = Sandbox.create(template="code-interpreter")# Stop the sandboxsandbox.stop()# Verify it's stoppedinfo = sandbox.get_info()print(f"Status: {info.status}") # "stopped"# Can start it again latersandbox.start()
Expected Output:
Copy
Status: stopped
Copy
import { Sandbox } from '@hopx-ai/sdk';const sandbox = await Sandbox.create({ template: 'code-interpreter' });// Stop the sandboxawait sandbox.stop();// Verify it's stoppedlet info = await sandbox.getInfo();console.log(`Status: ${info.status}`); // "stopped"// Can start it again laterawait sandbox.start();
Pause: Preserves full state (memory, variables, running processes). Fast resume.
Stop: Preserves filesystem only. Runtime state is lost. Requires full restart.
When to Use
Pause: When you need to temporarily suspend work but keep everything in memory.
Stop: When you want to free resources but keep files for later.
from hopx_ai import Sandboxfrom hopx_ai.errors import APIErrorsandbox = Sandbox.create(template="code-interpreter")try: # Try to pause sandbox.pause()except APIError as e: print(f"Failed to pause: {e}")try: # Try to start (might already be running) sandbox.start()except APIError as e: print(f"Failed to start: {e}")
Copy
import { Sandbox } from '@hopx-ai/sdk';import { APIError } from '@hopx-ai/sdk';const sandbox = await Sandbox.create({ template: 'code-interpreter' });try { // Try to pause await sandbox.pause();} catch (error) { if (error instanceof APIError) { console.error(`Failed to pause: ${error.message}`); }}try { // Try to start (might already be running) await sandbox.start();} catch (error) { if (error instanceof APIError) { console.error(`Failed to start: ${error.message}`); }}