Sandbox Management
ComputeSDK provides a simple interface for creating and managing sandboxed environments.
Quick Start
Section titled “Quick Start”import { createCompute } from 'computesdk'
import { e2b } from '@computesdk/e2b'
// Create a compute instance with the provider configuration
const compute = createCompute({
defaultProvider: e2b({ apiKey: 'your-api-key' })
})
// Create a sandbox with default options
const sandbox = await compute.sandbox.create()
try {
// Execute a command
const result = await sandbox.runCommand('echo', ['Hello, World!'])
console.log(result.stdout) // 'Hello, World!'
} finally {
// Clean up when done
await sandbox.destroy()
}Sandbox Creation
Section titled “Sandbox Creation”Basic Creation
Section titled “Basic Creation”import { createCompute } from 'computesdk'
import { e2b } from '@computesdk/e2b'
// Create a compute instance
const compute = createCompute({
defaultProvider: e2b({ apiKey: 'your-api-key' })
})
// Create a sandbox with default options
const sandbox1 = await compute.sandbox.create()
// Create a sandbox with options
const sandbox2 = await compute.sandbox.create({
runtime: 'python3.9',
timeout: 30000, // 30 seconds
metadata: {
project: 'my-project'
}
})Sandbox Operations
Section titled “Sandbox Operations”Running Commands
Section titled “Running Commands”// Run a simple command
const result = await sandbox.runCommand('echo', ['Hello, World!'])
console.log(result.stdout) // 'Hello, World!'
console.log(result.exitCode) // 0
// Run a command with options
const bgResult = await sandbox.runCommand('long-running-process', [], {
background: true
})
console.log('Process ID:', bgResult.pid)Getting Sandbox Information
Section titled “Getting Sandbox Information”// Get sandbox information
const info = await sandbox.getInfo()
console.log('Sandbox ID:', info.sandboxId)
console.log('Status:', info.status)
console.log('Created at:', info.createdAt)Getting Sandbox URL
Section titled “Getting Sandbox URL”// Get URL for accessing a service on a specific port
const url = await sandbox.getUrl({ port: 3000 })
console.log('Sandbox URL:', url)Error Handling
Section titled “Error Handling”try {
// This will throw if the command fails
await sandbox.runCommand('non-existent-command')
} catch (error) {
if (error instanceof CommandExitError) {
console.error('Command failed with exit code:', error.result.exitCode)
console.error('Error output:', error.result.stderr)
} else {
console.error('Unexpected error:', error)
}
}Cleanup
Section titled “Cleanup”Always clean up sandboxes when you’re done with them to free up resources:
// Destroy the sandbox
await sandbox.destroy()
// Check if sandbox is destroyed
console.log('Is destroyed?', sandbox.isDestroyed) // true