Last Verified: 2025-01-27
Package:
@hopx-ai/sdk on npm
Overview
TheSandbox 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
Static Methods
create
Create a new sandbox from a template.
options.template(string, optional): Template nameoptions.templateId(string, optional): Template ID (alternative to template)options.region(string, optional): Preferred regionoptions.timeoutSeconds(number, optional): Auto-kill timeout in secondsoptions.internetAccess(boolean, optional): Enable internet (default:true)options.envVars(Record<string, string>, optional): Environment variablesoptions.apiKey(string, optional): API key (or useHOPX_API_KEYenv var)options.baseURL(string, optional): API base URL
Promise<Sandbox> - New sandbox instance
Example:
connect
Connect to an existing sandbox by ID.
sandboxId(string): Existing sandbox IDoptions.apiKey(string, optional): API keyoptions.baseURL(string, optional): API base URL
Promise<Sandbox> - Connected sandbox instance
If VM is paused, resumes it and refreshes JWT token. If stopped, throws error.
list
List all sandboxes.
Promise<SandboxInfo[]> - Array of sandbox information objects
Example:
listTemplates
List available templates.
Promise<TemplateInfo[]> - Array of template information objects
Example:
getTemplate
Get template details by name.
Promise<TemplateInfo> - Template information object
Example:
Instance Methods
Lifecycle Methods
getInfo
Get current sandbox information.
Promise<SandboxInfo> - Sandbox information object
kill
Destroy the sandbox immediately.
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.
code(string): Code to executeoptions.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
Promise<ExecutionResult> - Execution result
Example:
runCodeAsync
Execute code asynchronously with webhook callback.
code(string): Code to executeoptions.callbackUrl(string): URL to POST results tooptions.language(string, optional): Languageoptions.timeout(number, optional): Timeout in secondsoptions.workingDir(string, optional): Working directoryoptions.env(Record<string, string>, optional): Environment variablesoptions.callbackHeaders(Record<string, string>, optional): Callback headersoptions.callbackSignatureSecret(string, optional): Signature secret
Promise<AsyncExecuteResponse> - Response with executionId and status
runCodeBackground
Execute code in background and return immediately.
Promise<BackgroundExecuteResponse> - Response with processId and executionId
runCodeStream
Execute code with real-time output streaming via WebSocket.
AsyncIterableIterator<StreamMessage> - Async generator of stream messages
Example:
listProcesses
List all background execution processes.
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:
commands
Command execution resource (lazy-loaded).
Type: Commands
Example:
env
Environment variables resource (lazy-loaded).
Type: EnvironmentVariables
Example:
cache
Cache management resource (lazy-loaded).
Type: Cache
Example:
desktop
Desktop automation resource (lazy-loaded).
Type: Desktop
Example:
terminal
Interactive terminal resource via WebSocket (lazy-loaded).
Type: Terminal
sandboxId
Read-only sandbox ID.
Type: string (read-only)
Examples
Example 1: Basic Usage
Example 2: File Operations
Example 3: Streaming Output
Related Classes
- Files - File operations resource
- Commands - Command execution resource
- Environment Variables - Environment variable management
- Types - TypeScript type definitions
See Also
- Quickstart Guide - Get started with the SDK
- Installation - Install and configure the SDK
- Core Concepts - Learn about sandboxes
Next Steps
- Learn about Creating Sandboxes with different templates
- Explore Code Execution to run code in sandboxes
- Review File Operations for managing files
- Check out API Reference for direct API access
- CLI Sandbox Commands - Manage sandboxes from CLI

