Skip to main content
The Filesystem module provides full file I/O within a sandbox. Access it via sandbox.files.

Reading Files

sandbox.files.read(path)

Reads a file as a UTF-8 string.
const content = await sandbox.files.read('/app/config.json');
const config = JSON.parse(content);
path
string
required
Absolute path to the file.
Returns: Promise<string>

sandbox.files.readBytes(path)

Reads a file as raw bytes.
const bytes = await sandbox.files.readBytes('/app/image.png');
console.log(`File size: ${bytes.length} bytes`);
path
string
required
Absolute path to the file.
Returns: Promise<Uint8Array>

Writing Files

sandbox.files.write(path, content)

Writes a string or binary content to a file. Creates parent directories if needed.
// Write text
await sandbox.files.write('/app/hello.txt', 'Hello, World!');

// Write binary
const encoder = new TextEncoder();
await sandbox.files.write('/app/data.bin', encoder.encode('binary data'));
path
string
required
Absolute path to the file.
content
string | Uint8Array
required
File content to write.
Returns: Promise<void>

Listing Directories

sandbox.files.list(path?)

Lists the contents of a directory.
const entries = await sandbox.files.list('/app');

for (const entry of entries) {
  console.log(`${entry.isDir ? 'DIR ' : 'FILE'} ${entry.name} (${entry.size} bytes)`);
}
path
string
default:"/"
Directory path to list.
Returns: Promise<EntryInfo[]>

EntryInfo

FieldTypeDescription
namestringFile or directory name
isDirbooleantrue if this entry is a directory
pathstringFull path to the entry
sizenumberSize in bytes (0 for directories)

Managing Files and Directories

sandbox.files.makeDir(path)

Creates a directory and any necessary parent directories.
await sandbox.files.makeDir('/app/src/components');
path
string
required
Directory path to create.
Returns: Promise<void>

sandbox.files.remove(path)

Deletes a file or directory.
await sandbox.files.remove('/app/temp.txt');
path
string
required
Path to remove.
Returns: Promise<void>

sandbox.files.exists(path)

Checks whether a file or directory exists.
if (await sandbox.files.exists('/app/package.json')) {
  console.log('Node.js project detected');
}
path
string
required
Path to check.
Returns: Promise<boolean>

Examples

Upload and Run a Script

await sandbox.files.write('/tmp/script.sh', `#!/bin/bash
echo "Running setup..."
apt-get update -qq
echo "Done!"
`);

const result = await sandbox.commands.run('bash /tmp/script.sh');
console.log(result.stdout);

Copy Files Between Paths

const content = await sandbox.files.readBytes('/app/original.dat');
await sandbox.files.write('/backup/original.dat', content);