CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-e2b--code-interpreter

JavaScript/TypeScript SDK for E2B's code interpreter service with secure sandbox execution

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

execution.mddocs/

Code Execution

Execute code in various programming languages with comprehensive result handling, real-time streaming, and stateful execution.

Capabilities

Code Execution Method

Execute code with optional language specification or context targeting.

/**
 * Execute code as Python (default language)
 * @param code - Code to execute
 * @param opts - Execution options with optional Python language specification
 * @returns Promise resolving to Execution result
 */
runCode(
  code: string,
  opts?: RunCodeOpts & { language?: 'python' }
): Promise<Execution>;

/**
 * Execute code in specified language
 * @param code - Code to execute
 * @param opts - Execution options with language specification
 * @returns Promise resolving to Execution result
 */
runCode(
  code: string,
  opts?: RunCodeOpts & { language?: string }
): Promise<Execution>;

/**
 * Execute code in specific context
 * @param code - Code to execute
 * @param opts - Execution options with context specification
 * @returns Promise resolving to Execution result
 */
runCode(
  code: string,
  opts?: RunCodeOpts & { context?: Context }
): Promise<Execution>;

Execution Options

interface RunCodeOpts {
  /**
   * Callback for handling stdout messages during execution
   */
  onStdout?: (output: OutputMessage) => Promise<any> | any;
  
  /**
   * Callback for handling stderr messages during execution
   */
  onStderr?: (output: OutputMessage) => Promise<any> | any;
  
  /**
   * Callback for handling execution results as they become available
   */
  onResult?: (data: Result) => Promise<any> | any;
  
  /**
   * Callback for handling execution errors
   */
  onError?: (error: ExecutionError) => Promise<any> | any;
  
  /**
   * Custom environment variables for code execution
   * @default {}
   */
  envs?: Record<string, string>;
  
  /**
   * Timeout for code execution in milliseconds
   * @default 60000 (60 seconds)
   */
  timeoutMs?: number;
  
  /**
   * Timeout for the request in milliseconds
   * @default 30000 (30 seconds)
   */
  requestTimeoutMs?: number;
}

Usage Examples:

import { Sandbox } from "@e2b/code-interpreter";

const sandbox = await Sandbox.create();

// Basic Python execution
const result = await sandbox.runCode('print("Hello, World!")');
console.log(result.logs.stdout); // ['Hello, World!\n']

// Execute with callbacks for real-time feedback
const execution = await sandbox.runCode(`
import time
for i in range(3):
    print(f"Step {i}")
    time.sleep(1)
`, {
  onStdout: (output) => {
    console.log('Stdout:', output.line);
  },
  onStderr: (output) => {
    console.log('Stderr:', output.line);
  }
});

// Execute with custom environment variables
const envResult = await sandbox.runCode('import os; print(os.getenv("MY_VAR"))', {
  envs: { MY_VAR: 'custom_value' }
});

// Execute JavaScript/TypeScript with Deno
const jsResult = await sandbox.runCode('console.log("JS execution")', {
  language: 'deno'
});

// Execute with timeout
const timeoutResult = await sandbox.runCode('import time; time.sleep(5)', {
  timeoutMs: 2000 // Will timeout after 2 seconds
});

Error Handling

import { Sandbox } from "@e2b/code-interpreter";

const sandbox = await Sandbox.create();

// Handle execution errors
const result = await sandbox.runCode('undefined_variable');
if (result.error) {
  console.log('Error name:', result.error.name); // 'NameError'
  console.log('Error value:', result.error.value); // "name 'undefined_variable' is not defined"
  console.log('Traceback:', result.error.traceback);
}

// Use error callback
await sandbox.runCode('invalid syntax here', {
  onError: (error) => {
    console.log(`Execution failed: ${error.name} - ${error.value}`);
  }
});

Stateful Execution

Code execution maintains state between calls within the same context:

// Variables persist between executions
await sandbox.runCode('x = 10');
await sandbox.runCode('y = 20');
const result = await sandbox.runCode('x + y');
console.log(result.text); // "30"

// Imports persist
await sandbox.runCode('import numpy as np');
const npResult = await sandbox.runCode('np.array([1, 2, 3])');

Language Support

The code interpreter supports multiple programming languages:

  • Python (default): Full Python environment with common libraries
  • Deno/TypeScript: JavaScript and TypeScript execution
  • Other languages: Extensible through the language parameter
// Python (default)
await sandbox.runCode('print("Python")');

// Deno/TypeScript
await sandbox.runCode('console.log("TypeScript")', { language: 'deno' });

// Custom language context
await sandbox.runCode('const x = 5; x * 2', { language: 'deno' });

Install with Tessl CLI

npx tessl i tessl/npm-e2b--code-interpreter

docs

charts.md

contexts.md

execution.md

index.md

results.md

sandbox.md

tile.json