import { 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();
Reading Files
// Read as string
const text = await sandbox.files.read("/app/config.json");
// Read as bytes
const bytes = await sandbox.files.readBytes("/app/image.png");
Writing Files
Content can be a string or binary data:
// 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);
Listing Directories
const entries = await sandbox.files.list("/app");
for (const entry of entries) {
console.log(entry.isDir ? "๐" : "๐", entry.name, entry.size);
}
EntryInfo
| 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 |
Managing Files
Create Directory
await sandbox.files.makeDir("/app/data");
Deletes a file or directory:
await sandbox.files.remove("/app/temp");
Check Existence
if (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.
Examples
Upload and Run a Script
await 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");