| 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 | // Execute a simple Python code |
| 29 | console.log("\nExecuting simple Python code..."); |
| 30 | // @ts-ignore - Ignore TypeScript errors for API compatibility |
| 31 | const result = await sandbox.runCode("print('Hello from E2B!')"); |
| 32 | |
| 33 | // Access the stdout from the logs property |
| 34 | console.log("Output:", result.logs.stdout[0]); |
| 35 | |
| 36 | // Close the sandbox |
| 37 | console.log("\nClosing sandbox..."); |
| 38 | // @ts-ignore - Ignore TypeScript errors for API compatibility |
| 39 | await sandbox.kill(); |
| 40 | |
| 41 | console.log("Example completed successfully!"); |
| 42 | } catch (error) { |
| 43 | console.error("Error:", error instanceof Error ? error.message : String(error)); |
| 44 | Deno.exit(1); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Run the example |
| 49 | if (import.meta.main) { |