JavaScript/TypeScript SDK for E2B's code interpreter service with secure sandbox execution
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Create and manage isolated execution contexts for different languages, working directories, and execution environments.
Create new execution contexts with custom configurations.
/**
* Creates a new code execution context
* @param opts - Optional context configuration
* @returns Promise resolving to a Context object
*/
createCodeContext(opts?: CreateCodeContextOpts): Promise<Context>;
interface CreateCodeContextOpts {
/**
* Working directory for the context
* @default '/home/user'
*/
cwd?: string;
/**
* Programming language for the context
* @default 'python'
*/
language?: string;
/**
* Timeout for the request in milliseconds
* @default 30000 (30 seconds)
*/
requestTimeoutMs?: number;
}interface Context {
/**
* Unique identifier for the context
*/
id: string;
/**
* Programming language of the context
*/
language: string;
/**
* Working directory of the context
*/
cwd: string;
}Usage Examples:
import { Sandbox } from "@e2b/code-interpreter";
const sandbox = await Sandbox.create();
// Create a default Python context
const pythonContext = await sandbox.createCodeContext();
console.log(pythonContext); // { id: "ctx_123", language: "python", cwd: "/home/user" }
// Create a context with custom working directory
const customContext = await sandbox.createCodeContext({
cwd: '/tmp',
language: 'python'
});
// Create a Deno/TypeScript context
const denoContext = await sandbox.createCodeContext({
language: 'deno',
cwd: '/home/user/projects'
});Each context maintains its own isolated state and environment:
import { Sandbox } from "@e2b/code-interpreter";
const sandbox = await Sandbox.create();
// Set variable in default context
await sandbox.runCode('x = 1');
// Create new context - variables from other contexts are not accessible
const newContext = await sandbox.createCodeContext();
const result = await sandbox.runCode('x', { context: newContext });
console.log(result.error?.name); // 'NameError' - x is not defined in new context
// Each context maintains its own state
await sandbox.runCode('y = 10', { context: newContext });
const contextResult = await sandbox.runCode('y', { context: newContext });
console.log(contextResult.text); // "10"When executing code, you can specify either a context or a language, but not both:
// Valid: Use language parameter
await sandbox.runCode('console.log("Hello")', { language: 'deno' });
// Valid: Use context
const context = await sandbox.createCodeContext({ language: 'deno' });
await sandbox.runCode('console.log("Hello")', { context });
// Invalid: Cannot specify both context and language
// This will throw an InvalidArgumentError
try {
await sandbox.runCode('code here', {
context: myContext,
language: 'python'
});
} catch (error) {
console.log(error.message); // "You can provide context or language, but not both at the same time."
}Contexts can be created with different working directories:
// Create context in home directory
const homeContext = await sandbox.createCodeContext({
cwd: '/home/user'
});
// Create context in tmp directory
const tmpContext = await sandbox.createCodeContext({
cwd: '/tmp'
});
// Verify working directories
const homeResult = await sandbox.runCode('import os; os.getcwd()', {
context: homeContext
});
console.log(homeResult.text); // "/home/user"
const tmpResult = await sandbox.runCode('import os; os.getcwd()', {
context: tmpContext
});
console.log(tmpResult.text); // "/tmp"Create contexts for different programming languages:
// Python context
const pythonCtx = await sandbox.createCodeContext({
language: 'python'
});
// Deno/TypeScript context
const denoCtx = await sandbox.createCodeContext({
language: 'deno'
});
// Execute Python code
await sandbox.runCode('x = [1, 2, 3]', { context: pythonCtx });
const pyResult = await sandbox.runCode('len(x)', { context: pythonCtx });
// Execute TypeScript code
await sandbox.runCode('const arr = [1, 2, 3]', { context: denoCtx });
const tsResult = await sandbox.runCode('arr.length', { context: denoCtx });Install with Tessl CLI
npx tessl i tessl/npm-e2b--code-interpreter