Appearance
Secure Code Execution Environment
In AI Agent development, executing code generated by LLMs is a common requirement, but running it directly poses serious security risks, including malicious code injection, environmental contamination, and resource abuse. The sandbox service addresses these security concerns by providing an isolated execution environment in the cloud.
Why Code Isolation is Necessary
Running untrusted code directly on the host environment presents the following risks:
- Malicious Code Injection – LLMs may generate code containing malicious logic.
- Environmental Contamination – Code execution could modify system configurations or install conflicting dependencies.
- Resource Abuse – Infinite loops or large memory allocations may cause system crashes.
- Multi-Tenant Security – One user's code could affect other users.
The sandbox service provides a fully isolated cloud execution environment, ensuring security and reliability.
Supported Languages
The sandbox supports multiple mainstream programming languages:
Need support for other languages? You can achieve this through custom templates.
Python
javascript
import { Sandbox } from '@e2b/code-interpreter'
async function main() {
const sandbox = await Sandbox.create()
const execution = await sandbox.runCode(
`
import requests
response = requests.get("https://httpbin.org/json")
print(response.json())
`,
{ language: 'python' }
)
console.log(execution.logs.stdout)
await sandbox.kill()
}
main()JavaScript & TypeScript
javascript
import { Sandbox } from '@e2b/code-interpreter'
async function main() {
const sandbox = await Sandbox.create()
const execution = await sandbox.runCode(
`
const https = require('https');
https.get('https://httpbin.org/json', (res) => {
let data = '';
res.on('data', (chunk) => { data += chunk; });
res.on('end', () => { console.log(JSON.parse(data)); });
});
`,
{ language: 'js' }
)
console.log(execution.logs.stdout)
await sandbox.kill()
}
main()
## R
```javascript
import { Sandbox } from '@e2b/code-interpreter'
async function main() {
const sandbox = await Sandbox.create()
const execution = await sandbox.runCode(
`
library(httr)
response <- GET("https://httpbin.org/json")
content <- content(response, "parsed")
print(content)
`,
{ language: 'r' }
)
console.log(execution.logs.stdout)
await sandbox.kill()
}
main()Java
javascript
import { Sandbox } from '@e2b/code-interpreter'
async function main() {
const sandbox = await Sandbox.create()
const execution = await sandbox.runCode(
`
import java.net.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
URL url = new URL("https://httpbin.org/json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
}
}
`,
{ language: 'java' }
)
console.log(execution.logs.stdout)
await sandbox.kill()
}
main()Bash
javascript
import { Sandbox } from '@e2b/code-interpreter'
async function main() {
const sandbox = await Sandbox.create()
const execution = await sandbox.runCode(
`
curl -s https://httpbin.org/json
`,
{ language: 'bash' }
)
console.log(execution.logs.stdout)
await sandbox.kill()
}
main()Streaming Output
The Code Interpreter supports real-time streaming of standard output, error output, and execution results during code execution.
Streaming stdout/stderr
javascript
import { Sandbox } from '@e2b/code-interpreter'
async function main() {
const sandbox = await Sandbox.create()
await sandbox.runCode('print("hello"); import time; time.sleep(1); print("world")', {
onStdout: (data) => {
console.log(data)
},
onStderr: (data) => {
console.log(data)
},
})
await sandbox.kill()
}
main()Streaming Execution Results
javascript
import { Sandbox } from '@e2b/code-interpreter'
async function main() {
const sandbox = await Sandbox.create()
await sandbox.runCode('x = 1', {
onResult: (result) => {
console.log(result)
},
})
await sandbox.kill()
}
main()