123456789import { Sandbox } from "@opencomputer/sdk"; const sandbox = await Sandbox.create(); await sandbox.files.write("/app/hello.txt", "Hello, World!"); const content = await sandbox.files.read("/app/hello.txt"); console.log(content); // "Hello, World!" await sandbox.kill();
12345// Read as string const text = await sandbox.files.read("/app/config.json"); // Read as bytes const bytes = await sandbox.files.readBytes("/app/image.png");
123456// Write text await sandbox.files.write("/app/config.json", JSON.stringify({ port: 3000 })); // Write binary const imageData = new Uint8Array([...]); await sandbox.files.write("/app/logo.png", imageData);
1234const entries = await sandbox.files.list("/app"); for (const entry of entries) { console.log(entry.isDir ? "📁" : "📄", entry.name, entry.size); }
| Field | TypeScript | Python | Type |
| Name | name | name | string |
| Is directory | isDir | is_dir | boolean |
| Full path | path | path | string |
| Size in bytes | size | size | number |
1await sandbox.files.makeDir("/app/data");
1await sandbox.files.remove("/app/temp");
123if (await sandbox.files.exists("/app/config.json")) { const config = await sandbox.files.read("/app/config.json"); }
exists() is a client-side convenience — it attempts a file read and returns false on error. There is no dedicated HTTP endpoint for existence checks.read(), readBytes(), and write() methods work for large files, but they buffer the full content in memory on the client side. For very large files, use the streaming methods to avoid high memory usage:1234567891011121314151617// Stream a large file download — returns a ReadableStream, not a buffer const stream = await sandbox.files.readStream("/data/model.bin"); // Pipe to a file, or process chunks incrementally const reader = stream.getReader(); while (true) { const { done, value } = await reader.read(); if (done) break; // process each Uint8Array chunk... } // Stream a large file upload — accepts ReadableStream or Uint8Array const fileStream = someReadableStream; // e.g. from fs.createReadStream await sandbox.files.writeStream("/data/model.bin", fileStream); // Uint8Array also works (sent without buffering a copy) await sandbox.files.writeStream("/data/output.bin", largeUint8Array);
12345await sandbox.files.write("/tmp/setup.sh", `#!/bin/bash apt-get update && apt-get install -y redis-server redis-server --daemonize yes `); await sandbox.exec.run("chmod +x /tmp/setup.sh && /tmp/setup.sh");