API Reference
Complete reference documentation for ComputeSDK’s APIs and interfaces.
Overview
Section titled “Overview”ComputeSDK provides a unified abstraction layer for executing code in secure, isolated sandboxed environments across multiple cloud providers. The API is designed to be consistent across all providers while allowing for provider-specific features.
Basic Usage Pattern
Section titled “Basic Usage Pattern”import { compute } from 'computesdk';
import { e2b } from '@computesdk/e2b';
// 1. Configure provider
compute.setConfig({
provider: e2b({ apiKey: process.env.E2B_API_KEY })
});
// 2. Create sandbox
const sandbox = await compute.sandbox.create({});
// 3. Execute operations
const result = await sandbox.runCode('print("Hello World!")');
console.log(result.stdout);
// 4. Clean up
await compute.sandbox.destroy(sandbox.sandboxId);
Available Methods
Section titled “Available Methods”Configuration
Section titled “Configuration”compute.setConfig(config)
- Set global configurationcompute.getConfig()
- Get current configurationcompute.clearConfig()
- Clear configuration
Sandbox Management
Section titled “Sandbox Management”compute.sandbox.create(options)
- Create new sandboxcompute.sandbox.getById(id)
- Get existing sandboxcompute.sandbox.list()
- List all sandboxescompute.sandbox.destroy(id)
- Destroy sandbox
Code Execution
Section titled “Code Execution”sandbox.runCode(code, runtime?)
- Execute codesandbox.runCommand(command, args?)
- Run shell command
Filesystem Operations
Section titled “Filesystem Operations”sandbox.filesystem.writeFile(path, content)
- Write filesandbox.filesystem.readFile(path)
- Read filesandbox.filesystem.mkdir(path)
- Create directorysandbox.filesystem.readdir(path)
- List directorysandbox.filesystem.exists(path)
- Check if existssandbox.filesystem.remove(path)
- Remove file/directory
Terminal Operations (E2B only)
Section titled “Terminal Operations (E2B only)”sandbox.terminal.create(options)
- Create terminalsandbox.terminal.list()
- List terminalssandbox.terminal.getById(id)
- Get terminal by IDterminal.write(data)
- Write to terminalterminal.resize(cols, rows)
- Resize terminalterminal.kill()
- Kill terminal
API Integration
Section titled “API Integration”handleComputeRequest(options)
- Process web requests
Supported Runtimes
Section titled “Supported Runtimes”- Python - Python 3.x with data science libraries
- Node.js - Node.js runtime with npm packages
Error Handling
Section titled “Error Handling”All methods can throw errors. Always wrap calls in try-catch blocks:
try {
const result = await sandbox.runCode('invalid code');
} catch (error) {
console.error('Execution failed:', error.message);
}
TypeScript Support
Section titled “TypeScript Support”ComputeSDK is fully typed. Import types as needed:
import type {
Sandbox,
Provider,
ExecutionResult,
ComputeConfig,
Runtime
} from 'computesdk';