Manage files
This guide shows you how to read, write, organize, and synchronize files in the sandbox filesystem.
File operations support both absolute and relative paths:
- /workspace- Default working directory for application files
- /tmp- Temporary files (may be cleared)
- /home- User home directory
// Absolute pathsawait sandbox.writeFile("/workspace/app.js", code);
// Relative paths (session-aware)const session = await sandbox.createSession();await session.exec("cd /workspace/my-project");await session.writeFile("app.js", code); // Writes to /workspace/my-project/app.jsawait session.writeFile("src/index.js", code); // Writes to /workspace/my-project/src/index.js// Absolute pathsawait sandbox.writeFile('/workspace/app.js', code);
// Relative paths (session-aware)const session = await sandbox.createSession();await session.exec('cd /workspace/my-project');await session.writeFile('app.js', code);  // Writes to /workspace/my-project/app.jsawait session.writeFile('src/index.js', code);  // Writes to /workspace/my-project/src/index.jsimport { getSandbox } from "@cloudflare/sandbox";
const sandbox = getSandbox(env.Sandbox, "my-sandbox");
// Write text fileawait sandbox.writeFile(  "/workspace/app.js",  `console.log('Hello from sandbox!');`,);
// Write JSONconst config = { name: "my-app", version: "1.0.0" };await sandbox.writeFile(  "/workspace/config.json",  JSON.stringify(config, null, 2),);
// Write binary file (base64)const buffer = await fetch(imageUrl).then((r) => r.arrayBuffer());const base64 = btoa(String.fromCharCode(...new Uint8Array(buffer)));await sandbox.writeFile("/workspace/image.png", base64, { encoding: "base64" });import { getSandbox } from '@cloudflare/sandbox';
const sandbox = getSandbox(env.Sandbox, 'my-sandbox');
// Write text fileawait sandbox.writeFile('/workspace/app.js', `console.log('Hello from sandbox!');`);
// Write JSONconst config = { name: 'my-app', version: '1.0.0' };await sandbox.writeFile('/workspace/config.json', JSON.stringify(config, null, 2));
// Write binary file (base64)const buffer = await fetch(imageUrl).then(r => r.arrayBuffer());const base64 = btoa(String.fromCharCode(...new Uint8Array(buffer)));await sandbox.writeFile('/workspace/image.png', base64, { encoding: 'base64' });// Read text fileconst file = await sandbox.readFile("/workspace/app.js");console.log(file.content);
// Read and parse JSONconst configFile = await sandbox.readFile("/workspace/config.json");const config = JSON.parse(configFile.content);
// Read binary fileconst imageFile = await sandbox.readFile("/workspace/image.png", {  encoding: "base64",});return new Response(atob(imageFile.content), {  headers: { "Content-Type": "image/png" },});// Read text fileconst file = await sandbox.readFile('/workspace/app.js');console.log(file.content);
// Read and parse JSONconst configFile = await sandbox.readFile('/workspace/config.json');const config = JSON.parse(configFile.content);
// Read binary fileconst imageFile = await sandbox.readFile('/workspace/image.png', { encoding: 'base64' });return new Response(atob(imageFile.content), {  headers: { 'Content-Type': 'image/png' }});// Create directoriesawait sandbox.mkdir("/workspace/src", { recursive: true });await sandbox.mkdir("/workspace/tests", { recursive: true });
// Rename fileawait sandbox.renameFile("/workspace/draft.txt", "/workspace/final.txt");
// Move fileawait sandbox.moveFile("/tmp/download.txt", "/workspace/data.txt");
// Delete fileawait sandbox.deleteFile("/workspace/temp.txt");// Create directoriesawait sandbox.mkdir('/workspace/src', { recursive: true });await sandbox.mkdir('/workspace/tests', { recursive: true });
// Rename fileawait sandbox.renameFile('/workspace/draft.txt', '/workspace/final.txt');
// Move fileawait sandbox.moveFile('/tmp/download.txt', '/workspace/data.txt');
// Delete fileawait sandbox.deleteFile('/workspace/temp.txt');Write multiple files in parallel:
const files = {  "/workspace/src/app.js": 'console.log("app");',  "/workspace/src/utils.js": 'console.log("utils");',  "/workspace/README.md": "# My Project",};
await Promise.all(  Object.entries(files).map(([path, content]) =>    sandbox.writeFile(path, content),  ),);const files = {  '/workspace/src/app.js': 'console.log("app");',  '/workspace/src/utils.js': 'console.log("utils");',  '/workspace/README.md': '# My Project'};
await Promise.all(  Object.entries(files).map(([path, content]) =>    sandbox.writeFile(path, content)  ));try {  await sandbox.readFile("/workspace/config.json");  console.log("File exists");} catch (error) {  if (error.code === "FILE_NOT_FOUND") {    // Create default config    await sandbox.writeFile("/workspace/config.json", "{}");  }}try {  await sandbox.readFile('/workspace/config.json');  console.log('File exists');} catch (error) {  if (error.code === 'FILE_NOT_FOUND') {    // Create default config    await sandbox.writeFile('/workspace/config.json', '{}');  }}- Use /workspace- Default working directory for app files
- Use absolute paths - Always use full paths like /workspace/file.txt
- Batch operations - Use Promise.all()for multiple independent file writes
- Create parent directories - Use recursive: truewhen creating nested paths
- Handle errors - Check for FILE_NOT_FOUNDerrors gracefully
Create parent directories first:
// Create directory, then write fileawait sandbox.mkdir("/workspace/data", { recursive: true });await sandbox.writeFile("/workspace/data/file.txt", content);// Create directory, then write fileawait sandbox.mkdir('/workspace/data', { recursive: true });await sandbox.writeFile('/workspace/data/file.txt', content);Use base64 for binary files:
// Write binaryawait sandbox.writeFile("/workspace/image.png", base64Data, {  encoding: "base64",});
// Read binaryconst file = await sandbox.readFile("/workspace/image.png", {  encoding: "base64",});// Write binaryawait sandbox.writeFile('/workspace/image.png', base64Data, {  encoding: 'base64'});
// Read binaryconst file = await sandbox.readFile('/workspace/image.png', {  encoding: 'base64'});- Files API reference - Complete method documentation
- Execute commands guide - Run file operations with commands
- Git workflows guide - Clone and manage repositories
- Code Interpreter guide - Generate and execute code files
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