()
| 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 | // ===== File Operations Using Python ===== |
| 29 | console.log("\n===== File Operations Using Python ====="); |
| 30 | |
| 31 | // Write a file using Python |
| 32 | console.log("\nWriting file using Python..."); |
| 33 | // @ts-ignore - Ignore TypeScript errors for API compatibility |
| 34 | const writeResult = await sandbox.runCode(` |
| 35 | # Write a file |
| 36 | with open('/tmp/data.txt', 'w') as f: |
| 37 | f.write('Hello, E2B!\\nThis is a test file.') |
| 38 | print('File written successfully') |
| 39 | `); |
| 40 | console.log("Output:", writeResult.logs.stdout[0]); |
| 41 | |
| 42 | // Read the file using Python |
| 43 | console.log("\nReading file using Python..."); |
| 44 | // @ts-ignore - Ignore TypeScript errors for API compatibility |
| 45 | const readResult = await sandbox.runCode(` |
| 46 | # Read the file |
| 47 | with open('/tmp/data.txt', 'r') as f: |
| 48 | content = f.read() |
| 49 | print('File content:', content) |
| 50 | `); |
| 51 | console.log("Output:", readResult.logs.stdout[0]); |
| 52 | |
| 53 | // List files using Python |
| 54 | console.log("\nListing files using Python..."); |
| 55 | // @ts-ignore - Ignore TypeScript errors for API compatibility |
| 56 | const listResult = await sandbox.runCode(` |
| 57 | # List files in directory |
| 58 | import os |
| 59 | files = os.listdir('/tmp') |
| 60 | print('Files in /tmp:', files) |
| 61 | `); |
| 62 | console.log("Output:", listResult.logs.stdout[0]); |
| 63 | |
| 64 | // Create a directory using Python |
| 65 | console.log("\nCreating directory using Python..."); |
| 66 | // @ts-ignore - Ignore TypeScript errors for API compatibility |
| 67 | const mkdirResult = await sandbox.runCode(` |
| 68 | # Create a directory |
| 69 | import os |
no test coverage detected