Files
Read, write, and manage files in the sandbox filesystem. All paths are absolute (e.g., /workspace/app.js
).
Write content to a file.
await sandbox.writeFile(path: string, content: string, options?: WriteFileOptions): Promise<void>
Parameters:
path
- Absolute path to the filecontent
- Content to writeoptions
(optional):encoding
- File encoding (default:"utf-8"
)
await sandbox.writeFile("/workspace/app.js", `console.log('Hello!');`);
// Binary dataawait sandbox.writeFile("/tmp/image.png", base64Data, { encoding: "base64" });
await sandbox.writeFile('/workspace/app.js', `console.log('Hello!');`);
// Binary dataawait sandbox.writeFile('/tmp/image.png', base64Data, { encoding: 'base64' });
Read a file from the sandbox.
const file = await sandbox.readFile(path: string, options?: ReadFileOptions): Promise<FileInfo>
Parameters:
path
- Absolute path to the fileoptions
(optional):encoding
- File encoding (default:"utf-8"
)
Returns: Promise<FileInfo>
with content
and encoding
const file = await sandbox.readFile("/workspace/package.json");const pkg = JSON.parse(file.content);
// Binary dataconst image = await sandbox.readFile("/tmp/image.png", { encoding: "base64" });
const file = await sandbox.readFile('/workspace/package.json');const pkg = JSON.parse(file.content);
// Binary dataconst image = await sandbox.readFile('/tmp/image.png', { encoding: 'base64' });
Create a directory.
await sandbox.mkdir(path: string, options?: MkdirOptions): Promise<void>
Parameters:
path
- Absolute path to the directoryoptions
(optional):recursive
- Create parent directories if needed (default:false
)
await sandbox.mkdir("/workspace/src");
// Nested directoriesawait sandbox.mkdir("/workspace/src/components/ui", { recursive: true });
await sandbox.mkdir('/workspace/src');
// Nested directoriesawait sandbox.mkdir('/workspace/src/components/ui', { recursive: true });
Delete a file.
await sandbox.deleteFile(path: string): Promise<void>
Parameters:
path
- Absolute path to the file
await sandbox.deleteFile("/workspace/temp.txt");
await sandbox.deleteFile('/workspace/temp.txt');
Rename a file.
await sandbox.renameFile(oldPath: string, newPath: string): Promise<void>
Parameters:
oldPath
- Current file pathnewPath
- New file path
await sandbox.renameFile("/workspace/draft.txt", "/workspace/final.txt");
await sandbox.renameFile('/workspace/draft.txt', '/workspace/final.txt');
Move a file to a different directory.
await sandbox.moveFile(sourcePath: string, destinationPath: string): Promise<void>
Parameters:
sourcePath
- Current file pathdestinationPath
- Destination path
await sandbox.moveFile("/tmp/download.txt", "/workspace/data.txt");
await sandbox.moveFile('/tmp/download.txt', '/workspace/data.txt');
Clone a git repository.
await sandbox.gitCheckout(repoUrl: string, options?: GitCheckoutOptions): Promise<void>
Parameters:
repoUrl
- Git repository URLoptions
(optional):branch
- Branch to checkout (default: main branch)targetDir
- Directory to clone into (default: repo name)depth
- Clone depth for shallow clone
await sandbox.gitCheckout("https://github.com/user/repo");
// Specific branchawait sandbox.gitCheckout("https://github.com/user/repo", { branch: "develop", targetDir: "my-project",});
await sandbox.gitCheckout('https://github.com/user/repo');
// Specific branchawait sandbox.gitCheckout('https://github.com/user/repo', { branch: 'develop', targetDir: 'my-project'});
- Manage files guide - Detailed guide with best practices
- Commands API - Execute commands
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark
-