Skip to main content
Version: 0.1.22
Last Verified: 2025-01-27
Package: @hopx-ai/sdk on npm

Overview

The Sandbox class is the main entry point for the HopX JavaScript/TypeScript SDK. It provides an async/await interface for creating, managing, and interacting with cloud sandboxes (microVMs). All methods are async and must be called with await. The Sandbox class handles authentication, request formatting, error translation, and response parsing automatically, giving you a clean, Promise-based API for sandbox management.

Import

Or with CommonJS:

Static Methods

create

Create a new sandbox from a template.
Parameters:
  • options.template (string, optional): Template name
  • options.templateId (string, optional): Template ID (alternative to template)
  • options.region (string, optional): Preferred region
  • options.timeoutSeconds (number, optional): Auto-kill timeout in seconds
  • options.internetAccess (boolean, optional): Enable internet (default: true)
  • options.envVars (Record<string, string>, optional): Environment variables
  • options.apiKey (string, optional): API key (or use HOPX_API_KEY env var)
  • options.baseURL (string, optional): API base URL
Returns: Promise<Sandbox> - New sandbox instance Example:
Expected Output:

connect

Connect to an existing sandbox by ID.
Parameters:
  • sandboxId (string): Existing sandbox ID
  • options.apiKey (string, optional): API key
  • options.baseURL (string, optional): API base URL
Returns: Promise<Sandbox> - Connected sandbox instance
If VM is paused, resumes it and refreshes JWT token. If stopped, throws error.
Example:
Expected Output:

list

List all sandboxes.
Returns: Promise<SandboxInfo[]> - Array of sandbox information objects Example:
Expected Output:

listTemplates

List available templates.
Returns: Promise<TemplateInfo[]> - Array of template information objects Example:
Expected Output:

getTemplate

Get template details by name.
Returns: Promise<TemplateInfo> - Template information object Example:
Expected Output:

Instance Methods

Lifecycle Methods

getInfo

Get current sandbox information.
Returns: Promise<SandboxInfo> - Sandbox information object

kill

Destroy the sandbox immediately.
This action is irreversible. The sandbox and all its data will be permanently deleted.

start

Start a stopped sandbox.

stop

Stop a running sandbox.

pause

Pause a running sandbox.

resume

Resume a paused sandbox.

Execution Methods

runCode

Execute code synchronously with rich output capture.
Parameters:
  • code (string): Code to execute
  • options.language (string, optional): Language (default: 'python')
  • options.timeout (number, optional): Timeout in seconds (default: 60)
  • options.workingDir (string, optional): Working directory (default: '/workspace')
  • options.env (Record<string, string>, optional): Environment variables
Returns: Promise<ExecutionResult> - Execution result Example:
Expected Output:

runCodeAsync

Execute code asynchronously with webhook callback.
Parameters:
  • code (string): Code to execute
  • options.callbackUrl (string): URL to POST results to
  • options.language (string, optional): Language
  • options.timeout (number, optional): Timeout in seconds
  • options.workingDir (string, optional): Working directory
  • options.env (Record<string, string>, optional): Environment variables
  • options.callbackHeaders (Record<string, string>, optional): Callback headers
  • options.callbackSignatureSecret (string, optional): Signature secret
Returns: Promise<AsyncExecuteResponse> - Response with executionId and status

runCodeBackground

Execute code in background and return immediately.
Returns: Promise<BackgroundExecuteResponse> - Response with processId and executionId

runCodeStream

Execute code with real-time output streaming via WebSocket.
Returns: AsyncIterableIterator<StreamMessage> - Async generator of stream messages Example:
Expected Output:

listProcesses

List all background execution processes.
Returns: Promise<ProcessInfo[]> - Array of process information objects

killProcess

Kill a background execution process.

Information Methods

getMetricsSnapshot

Get current system metrics snapshot.

getAgentInfo

Get agent information and capabilities.

refreshToken

Refresh JWT token for agent authentication.

getToken

Get current JWT token.

Properties

files

File operations resource (lazy-loaded). Type: Files Example:
Expected Output:
See Files Resource for complete documentation.

commands

Command execution resource (lazy-loaded). Type: Commands Example:
Expected Output:

env

Environment variables resource (lazy-loaded). Type: EnvironmentVariables Example:
Expected Output:

cache

Cache management resource (lazy-loaded). Type: Cache Example:
Expected Output:

desktop

Desktop automation resource (lazy-loaded). Type: Desktop Example:
Expected Output:

terminal

Interactive terminal resource via WebSocket (lazy-loaded). Type: Terminal

sandboxId

Read-only sandbox ID. Type: string (read-only)

Examples

Example 1: Basic Usage

Expected Output:

Example 2: File Operations

Expected Output:

Example 3: Streaming Output

Expected Output:

See Also

Next Steps