Skip to content
Cloudflare Docs

Files

Read, write, and manage files in the sandbox filesystem. All paths are absolute (e.g., /workspace/app.js).

Methods

writeFile()

Write content to a file.

TypeScript
await sandbox.writeFile(path: string, content: string, options?: WriteFileOptions): Promise<void>

Parameters:

  • path - Absolute path to the file
  • content - Content to write
  • options (optional):
    • encoding - File encoding ("utf-8" or "base64", default: "utf-8")
JavaScript
await sandbox.writeFile("/workspace/app.js", `console.log('Hello!');`);
// Binary data
await sandbox.writeFile("/tmp/image.png", base64Data, { encoding: "base64" });

readFile()

Read a file from the sandbox.

TypeScript
const file = await sandbox.readFile(path: string, options?: ReadFileOptions): Promise<FileInfo>

Parameters:

  • path - Absolute path to the file
  • options (optional):
    • encoding - File encoding ("utf-8" or "base64", default: auto-detected from MIME type)

Returns: Promise<FileInfo> with content and encoding

JavaScript
const file = await sandbox.readFile("/workspace/package.json");
const pkg = JSON.parse(file.content);
// Binary data (auto-detected or forced)
const image = await sandbox.readFile("/tmp/image.png", { encoding: "base64" });
// Force encoding (override MIME detection)
const textAsBase64 = await sandbox.readFile("/workspace/data.txt", {
encoding: "base64",
});

exists()

Check if a file or directory exists.

TypeScript
const result = await sandbox.exists(path: string): Promise<FileExistsResult>

Parameters:

  • path - Absolute path to check

Returns: Promise<FileExistsResult> with exists boolean

JavaScript
const result = await sandbox.exists("/workspace/package.json");
if (result.exists) {
const file = await sandbox.readFile("/workspace/package.json");
// process file
}
// Check directory
const dirResult = await sandbox.exists("/workspace/src");
if (!dirResult.exists) {
await sandbox.mkdir("/workspace/src");
}

mkdir()

Create a directory.

TypeScript
await sandbox.mkdir(path: string, options?: MkdirOptions): Promise<void>

Parameters:

  • path - Absolute path to the directory
  • options (optional):
    • recursive - Create parent directories if needed (default: false)
JavaScript
await sandbox.mkdir("/workspace/src");
// Nested directories
await sandbox.mkdir("/workspace/src/components/ui", { recursive: true });

deleteFile()

Delete a file.

TypeScript
await sandbox.deleteFile(path: string): Promise<void>

Parameters:

  • path - Absolute path to the file
JavaScript
await sandbox.deleteFile("/workspace/temp.txt");

renameFile()

Rename a file.

TypeScript
await sandbox.renameFile(oldPath: string, newPath: string): Promise<void>

Parameters:

  • oldPath - Current file path
  • newPath - New file path
JavaScript
await sandbox.renameFile("/workspace/draft.txt", "/workspace/final.txt");

moveFile()

Move a file to a different directory.

TypeScript
await sandbox.moveFile(sourcePath: string, destinationPath: string): Promise<void>

Parameters:

  • sourcePath - Current file path
  • destinationPath - Destination path
JavaScript
await sandbox.moveFile("/tmp/download.txt", "/workspace/data.txt");

gitCheckout()

Clone a git repository.

TypeScript
await sandbox.gitCheckout(repoUrl: string, options?: GitCheckoutOptions): Promise<void>

Parameters:

  • repoUrl - Git repository URL
  • options (optional):
    • branch - Branch to checkout (default: main branch)
    • targetDir - Directory to clone into (default: repo name)
    • depth - Clone depth for shallow clone
JavaScript
await sandbox.gitCheckout("https://github.com/user/repo");
// Specific branch
await sandbox.gitCheckout("https://github.com/user/repo", {
branch: "develop",
targetDir: "my-project",
});