E2B Code Interpreter is a JavaScript/TypeScript SDK that provides secure, stateful code execution in cloud sandboxes. It enables AI agents and applications to run dynamic code safely in isolated environments, supporting multiple programming languages with real-time streaming and comprehensive result handling.
npm install @e2b/code-interpreterimport { Sandbox } from "@e2b/code-interpreter";Import specific types and interfaces:
import {
Sandbox,
RunCodeOpts,
Context,
CreateCodeContextOpts,
Result,
Execution,
ExecutionError,
OutputMessage,
Logs,
MIMEType,
RawData,
ChartType,
ScaleType,
ChartTypes,
Chart,
BarChart,
BarData,
LineChart,
ScatterChart,
BoxAndWhiskerChart,
BoxAndWhiskerData,
PieChart,
PieData,
SuperChart,
PointData,
deserializeChart
} from "@e2b/code-interpreter";Default import:
import Sandbox from "@e2b/code-interpreter";For CommonJS:
const { Sandbox } = require("@e2b/code-interpreter");Note: This package re-exports all functionality from the base e2b SDK, providing access to file system operations, process management, and other sandbox capabilities in addition to code interpretation features.
import { Sandbox } from "@e2b/code-interpreter";
// Create a sandbox instance
const sandbox = await Sandbox.create();
// Execute Python code with state persistence
await sandbox.runCode('x = 1');
const result = await sandbox.runCode('x += 1; x');
console.log(result.text); // "2"
// Handle multiple output formats
const plotResult = await sandbox.runCode(`
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()
`);
console.log(plotResult.results[0].formats()); // Available formats: ['png', 'text', ...]
console.log(plotResult.results[0].png); // Base64 encoded PNG imageE2B Code Interpreter is built around several key components:
Core sandbox functionality for creating and managing secure code execution environments.
class Sandbox {
static create(options?: any): Promise<Sandbox>;
}Execute code in various programming languages with comprehensive result handling and real-time streaming.
interface RunCodeOpts {
onStdout?: (output: OutputMessage) => Promise<any> | any;
onStderr?: (output: OutputMessage) => Promise<any> | any;
onResult?: (data: Result) => Promise<any> | any;
onError?: (error: ExecutionError) => Promise<any> | any;
envs?: Record<string, string>;
timeoutMs?: number;
requestTimeoutMs?: number;
}
runCode(
code: string,
opts?: RunCodeOpts & { language?: string }
): Promise<Execution>;
runCode(
code: string,
opts?: RunCodeOpts & { context?: Context }
): Promise<Execution>;Create and manage isolated execution contexts for different languages and working directories.
interface CreateCodeContextOpts {
cwd?: string;
language?: string;
requestTimeoutMs?: number;
}
interface Context {
id: string;
language: string;
cwd: string;
}
createCodeContext(opts?: CreateCodeContextOpts): Promise<Context>;Comprehensive result handling with support for multiple output formats and data types.
class Result {
readonly text?: string;
readonly html?: string;
readonly markdown?: string;
readonly svg?: string;
readonly png?: string;
readonly jpeg?: string;
readonly pdf?: string;
readonly latex?: string;
readonly json?: string;
readonly javascript?: string;
readonly data?: Record<string, unknown>;
readonly chart?: ChartTypes;
readonly extra?: any;
readonly raw: RawData;
readonly isMainResult: boolean;
formats(): string[];
toJSON(): object;
}
class Execution {
results: Result[];
logs: Logs;
error?: ExecutionError;
executionCount?: number;
get text(): string | undefined;
toJSON(): object;
}Built-in support for chart visualization with structured data types for various chart formats.
enum ChartType {
LINE = 'line',
SCATTER = 'scatter',
BAR = 'bar',
PIE = 'pie',
BOX_AND_WHISKER = 'box_and_whisker',
SUPERCHART = 'superchart',
UNKNOWN = 'unknown'
}
enum ScaleType {
LINEAR = "linear",
DATETIME = "datetime",
CATEGORICAL = "categorical",
LOG = "log",
SYMLOG = "symlog",
LOGIT = "logit",
FUNCTION = "function",
FUNCTIONLOG = "functionlog",
ASINH = "asinh"
}
type ChartTypes = LineChart | ScatterChart | BarChart | PieChart | BoxAndWhiskerChart | SuperChart;
function deserializeChart(data: any): Chart;This package includes all functionality from the base e2b SDK through re-exports.
// Re-exported from 'e2b' package - all base sandbox functionality available
// Including file operations, process management, networking, etc.
// See e2b package documentation for complete base SDK APIclass ExecutionError {
constructor(name: string, value: string, traceback: string);
name: string;
value: string;
traceback: string;
}
class OutputMessage {
constructor(line: string, timestamp: number, error: boolean);
readonly line: string;
readonly timestamp: number;
readonly error: boolean;
toString(): string;
}type MIMEType = string;
type RawData = {
[key: MIMEType]: string;
} & {
data: Record<string, unknown>;
chart: ChartTypes;
};
type Logs = {
stdout: string[];
stderr: string[];
};type Chart = {
type: ChartType;
title: string;
elements: any[];
};
type PointData = {
label: string;
points: [number | string, number | string][];
};
type BarData = {
label: string;
value: string;
group: string;
};
type PieData = {
label: string;
angle: number;
radius: number;
};
type BoxAndWhiskerData = {
label: string;
min: number;
first_quartile: number;
median: number;
third_quartile: number;
max: number;
outliers: number[];
};
type LineChart = Chart & {
type: ChartType.LINE;
x_label?: string;
y_label?: string;
x_unit?: string;
y_unit?: string;
x_ticks: (number | string)[];
x_scale: ScaleType;
x_tick_labels: string[];
y_ticks: (number | string)[];
y_scale: ScaleType;
y_tick_labels: string[];
elements: PointData[];
};
type ScatterChart = Chart & {
type: ChartType.SCATTER;
x_label?: string;
y_label?: string;
x_unit?: string;
y_unit?: string;
x_ticks: (number | string)[];
x_scale: ScaleType;
x_tick_labels: string[];
y_ticks: (number | string)[];
y_scale: ScaleType;
y_tick_labels: string[];
elements: PointData[];
};
type BarChart = Chart & {
type: ChartType.BAR;
x_label?: string;
y_label?: string;
x_unit?: string;
y_unit?: string;
elements: BarData[];
};
type PieChart = Chart & {
type: ChartType.PIE;
elements: PieData[];
};
type BoxAndWhiskerChart = Chart & {
type: ChartType.BOX_AND_WHISKER;
x_label?: string;
y_label?: string;
x_unit?: string;
y_unit?: string;
elements: BoxAndWhiskerData[];
};
type SuperChart = Chart & {
type: ChartType.SUPERCHART;
elements: Chart[];
};