()
| 10 | |
| 11 | // Main function to run the example |
| 12 | async 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 |
| 36 | function greet(name) { |
| 37 | return \`Hello, \${name}!\`; |
| 38 | } |
| 39 | |
| 40 | // Call the function |
| 41 | const greeting = greet('E2B'); |
| 42 | console.log(greeting); |
| 43 | |
| 44 | // Return a value |
| 45 | greeting; |
| 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 |
| 60 | const 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 |
| 69 | const activeUsers = users.filter(user => user.active); |
no test coverage detected