Run Code
Code Interpreter runs code inside the sandbox through run_code() / runCode(). Python is the default language. Other languages are selected through the language parameter.
Create a sandbox
Create a sandbox with the code-interpreter-v1 template:
import os
from e2b_code_interpreter import Sandbox
sandbox = Sandbox.create(
template="code-interpreter-v1",
api_key=os.environ["E2B_API_KEY"],
api_url=os.environ["E2B_API_URL"],
domain=os.environ["E2B_DOMAIN"],
)TypeScript example:
import { Sandbox } from "@e2b/code-interpreter";
const sandbox = await Sandbox.create("code-interpreter-v1", {
apiKey: process.env.E2B_API_KEY,
apiUrl: process.env.E2B_API_URL,
domain: process.env.E2B_DOMAIN,
});Execute Python code
execution = sandbox.run_code("""
import pandas as pd
df = pd.DataFrame({"value": [1, 2, 3]})
print(df)
df["value"].sum()
""")
stdout = "".join(execution.logs.stdout or [])
print(stdout.strip())
print(execution.text)TypeScript example:
const execution = await sandbox.runCode(`
import pandas as pd
df = pd.DataFrame({"value": [1, 2, 3]})
print(df)
df["value"].sum()
`);
const stdout = execution.logs.stdout.join("");
console.log(stdout.trim());
console.log(execution.text);print() writes to stdout. The final bare expression is written to execution.text and may also generate a structured entry in execution.results.
Set timeouts
For long-running tasks, set both execution timeout and request timeout explicitly:
execution = sandbox.run_code(
"import time; time.sleep(5); 42",
timeout=30,
request_timeout=60,
)TypeScript example:
const execution = await sandbox.runCode(
"import time; time.sleep(5); 42",
{
timeoutMs: 30_000,
requestTimeoutMs: 60_000,
},
);timeout limits code execution time inside the sandbox. request_timeout limits how long the SDK waits for the request. Follow the parameter names exposed by your current language SDK.
Execute other languages
FC Agent Sandbox Code Interpreter supports JavaScript, TypeScript, and Bash through the language parameter. Java and R are not supported. Before production use, validate the target language with a minimal script first.
execution = sandbox.run_code(
'console.log("hello from js")',
language="javascript",
)TypeScript example:
const execution = await sandbox.runCode('console.log("hello from js")', {
language: "javascript",
});For more language examples, see Supported Languages.
Output behavior
execution.logs.stdout/execution.logs.stderrreturn standard output and standard error.stdout and stderr chunks include timestamps.
execution.execution_count/execution.executionCountis the execution number inside the current context.execution.textreturns the text view of the final bare expression.execution.resultsreturns rich results such as text and tables. For charts, write files and retrieve them through Filesystem.execution.errorreturns runtime errors.