MCPcopy Create free account
hub / github.com/agenticsorg/edge-agents / main

Function main

scripts/e2b/e2b-javascript-example.ts:12–235  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

10
11// Main function to run the example
12async function main() {
13 try {
14 // Get API key from environment variable
15 const apiKey = Deno.env.get("E2B_API_KEY");
16 if (!apiKey) {
17 console.error("E2B_API_KEY environment variable is required");
18 Deno.exit(1);
19 }
20
21 console.log("Creating code interpreter sandbox...");
22
23 // Create a new sandbox instance using the factory method
24 // @ts-ignore - Ignore TypeScript errors for API compatibility
25 const sandbox = await Sandbox.create({ apiKey });
26 console.log("Sandbox created successfully");
27
28 // ===== Basic JavaScript Execution =====
29 console.log("\n===== Basic JavaScript Execution =====");
30
31 // Execute simple JavaScript code
32 console.log("\nExecuting simple JavaScript code...");
33 // @ts-ignore - Ignore TypeScript errors for API compatibility
34 const basicResult = await sandbox.runCode(`
35// Define a simple function
36function greet(name) {
37 return \`Hello, \${name}!\`;
38}
39
40// Call the function
41const greeting = greet('E2B');
42console.log(greeting);
43
44// Return a value
45greeting;
46 `, { language: "javascript" });
47
48 console.log("Output:");
49 basicResult.logs.stdout.forEach(line => console.log(` ${line}`));
50 console.log("Return value:", basicResult.text);
51
52 // ===== JavaScript Data Manipulation =====
53 console.log("\n===== JavaScript Data Manipulation =====");
54
55 // Execute JavaScript code with data manipulation
56 console.log("\nExecuting JavaScript code with data manipulation...");
57 // @ts-ignore - Ignore TypeScript errors for API compatibility
58 const dataResult = await sandbox.runCode(`
59// Sample data
60const users = [
61 { id: 1, name: 'Alice', age: 25, active: true },
62 { id: 2, name: 'Bob', age: 30, active: false },
63 { id: 3, name: 'Charlie', age: 35, active: true },
64 { id: 4, name: 'David', age: 40, active: false },
65 { id: 5, name: 'Eve', age: 45, active: true }
66];
67
68// Filter active users
69const activeUsers = users.filter(user => user.active);

Callers 1

Calls 1

getMethod · 0.65

Tested by

no test coverage detected