Oblien Docs

File Operations

The Sandbox provides comprehensive file system operations for managing files and directories within your isolated environment.

Base Endpoint

All file operations use the /files endpoint:

POST https://sandbox.oblien.com:55872/files/{operation}

Core Operations

List Files

List files and directories in a path.

// Using SDK
const result = await sandbox.files.list({
  dirPath: '/opt/app',
  recursive: false,
  includeHidden: false
});
POST /files/list HTTP/1.1
Authorization: Bearer YOUR_TOKEN
Content-Type: application/json

{
  "dirPath": "/opt/app"
}

Response:

{
  "success": true,
  "files": [
    {
      "name": "src",
      "path": "/opt/app/src",
      "type": "directory",
      "size": 4096,
      "modified": "2025-10-16T10:30:00Z"
    },
    {
      "name": "package.json",
      "path": "/opt/app/package.json",
      "type": "file",
      "size": 512,
      "modified": "2025-10-16T10:25:00Z"
    }
  ],
  "path": "/opt/app"
}

Parameters:

ParameterTypeRequiredDescription
dirPathstringYesPath to directory to list
recursivebooleanNoInclude subdirectories
includeHiddenbooleanNoInclude hidden files (starting with .)

Get File Content

Read the contents of a file.

// Using SDK
const result = await sandbox.files.get({
  filePath: '/opt/app/src/index.js',
  range: { start: 1, end: 50 },
  withLineNumbers: true
});
POST /files/get HTTP/1.1
Authorization: Bearer YOUR_TOKEN
Content-Type: application/json

{
  "filePath": "/opt/app/src/index.js",
  "range": { "start": 1, "end": 50 },
  "withLineNumbers": true
}

Response:

{
  "success": true,
  "content": "import express from 'express';\n\nconst app = express();",
  "path": "/opt/app/src/index.js",
  "size": 1245,
  "lines": 42
}

Parameters:

ParameterTypeRequiredDescription
filePathstringYesFull path to file
rangeobjectNoLine range {start, end}
withLineNumbersbooleanNoInclude line numbers in output

Create File or Directory

Create a new file or directory.

// Using SDK - Create file
await sandbox.files.create({
  filePath: '/opt/app/newfile.js',
  content: 'console.log("New file");',
  isFolder: false
});

// Create directory
await sandbox.files.create({
  filePath: '/opt/app/newfolder',
  isFolder: true
});
POST /files/create HTTP/1.1
Authorization: Bearer YOUR_TOKEN
Content-Type: application/json

{
  "filePath": "/opt/app/newfile.js",
  "content": "console.log(\"New file\");",
  "isFolder": false
}

Response:

{
  "success": true,
  "message": "File created successfully",
  "path": "/opt/app/newfile.js"
}

Parameters:

ParameterTypeRequiredDescription
filePathstringYesFull path for new file/directory
contentstringNoFile content (only for files)
isFolderbooleanNoCreate directory instead of file
withWatcherbooleanNoTrigger file watcher (default: true)

Delete File or Directory

Delete a file or directory.

// Using SDK
await sandbox.files.delete({
  filePath: '/opt/app/oldfile.js'
});
POST /files/delete HTTP/1.1
Authorization: Bearer YOUR_TOKEN
Content-Type: application/json

{
  "filePath": "/opt/app/oldfile.js"
}

Response:

HTTP/1.1 204 No Content

Or on error:

{
  "success": false,
  "error": "File not found"
}

Parameters:

ParameterTypeRequiredDescription
filePathstringYesPath to file/directory to delete
withWatcherbooleanNoTrigger file watcher (default: true)

Rename/Move File

Rename or move a file or directory.

// Using SDK
await sandbox.files.rename({
  sourcePath: '/opt/app/old-name.js',
  destinationPath: '/opt/app/new-name.js'
});
POST /files/rename HTTP/1.1
Authorization: Bearer YOUR_TOKEN
Content-Type: application/json

{
  "sourcePath": "/opt/app/old-name.js",
  "destinationPath": "/opt/app/new-name.js"
}

Response:

{
  "success": true,
  "message": "File renamed successfully",
  "from": "/opt/app/old-name.js",
  "to": "/opt/app/new-name.js"
}

Parameters:

ParameterTypeRequiredDescription
sourcePathstringYesCurrent path
destinationPathstringYesNew path
withWatcherbooleanNoTrigger file watcher (default: true)

Edit File

Replace file content or perform advanced edits.

// Using SDK
await sandbox.files.edit({
  filePath: '/opt/app/config.json',
  content: JSON.stringify({ port: 3000, env: 'production' }, null, 2)
});
POST /files/edit HTTP/1.1
Authorization: Bearer YOUR_TOKEN
Content-Type: application/json

{
  "filePath": "/opt/app/config.json",
  "content": "{\n  \"port\": 3000,\n  \"env\": \"production\"\n}"
}

Response:

{
  "success": true,
  "message": "File edited successfully",
  "path": "/opt/app/config.json"
}

Parameters:

ParameterTypeRequiredDescription
filePathstringYesPath to file to edit
contentstringYesNew file content
withWatcherbooleanNoTrigger file watcher (default: true)

Merge File Changes

Intelligently merge content into a file.

// Using SDK
await sandbox.files.merge({
  filePath: '/opt/app/README.md',
  content: '## New Section\n\nAdditional content',
  options: {
    strategy: 'append', // or 'prepend', 'replace'
    silent: false
  }
});
POST /files/merge HTTP/1.1
Authorization: Bearer YOUR_TOKEN
Content-Type: application/json

{
  "filePath": "/opt/app/README.md",
  "content": "## New Section\n\nAdditional content",
  "options": {
    "strategy": "append"
  }
}

Response:

{
  "success": true,
  "message": "File merged successfully",
  "path": "/opt/app/README.md",
  "linesAdded": 3
}

Parameters:

ParameterTypeRequiredDescription
filePathstringYesPath to file
contentstringYesContent to merge
optionsobjectNoMerge options
options.strategystringNoappend, prepend, or replace
options.silentbooleanNoDon't trigger file watcher

Check File Exists

Check if a file or directory exists.

// Using SDK
const exists = await sandbox.files.exists({
  filePath: '/opt/app/package.json'
});

if (exists.exists) {
  console.log('File exists!');
}
POST /files/exists HTTP/1.1
Authorization: Bearer YOUR_TOKEN
Content-Type: application/json

{
  "filePath": "/opt/app/package.json"
}

Response:

{
  "success": true,
  "exists": true,
  "path": "/opt/app/package.json",
  "type": "file"
}

Upload File

Upload file content (useful for binary files or large files).

// Using SDK
await sandbox.files.upload({
  filePath: '/opt/app/image.png',
  content: base64EncodedContent
});
POST /files/upload HTTP/1.1
Authorization: Bearer YOUR_TOKEN
Content-Type: application/json

{
  "filePath": "/opt/app/image.png",
  "content": "iVBORw0KGgoAAAANSUhEUgAA..."
}

Response:

{
  "success": true,
  "message": "File uploaded successfully",
  "path": "/opt/app/image.png",
  "size": 15360
}

Download File

Download file content (returns base64 for binary files).

// Using SDK
const file = await sandbox.files.download({
  filePath: '/opt/app/image.png'
});

console.log(file.content); // base64 or text
POST /files/download HTTP/1.1
Authorization: Bearer YOUR_TOKEN
Content-Type: application/json

{
  "filePath": "/opt/app/image.png"
}

Response:

{
  "success": true,
  "content": "iVBORw0KGgoAAAANSUhEUgAA...",
  "encoding": "base64",
  "path": "/opt/app/image.png",
  "size": 15360,
  "mimeType": "image/png"
}

Advanced Usage

Working with File Watcher

The withWatcher parameter controls whether file changes trigger WebSocket events:

// Silent operation - no WebSocket notification
await sandbox.files.create({
  filePath: '/opt/app/temp.txt',
  content: 'temporary file',
  withWatcher: false
});

// Normal operation - triggers WebSocket event
await sandbox.files.create({
  filePath: '/opt/app/important.txt',
  content: 'important file',
  withWatcher: true // default
});

ℹ️ Info: Use withWatcher: false for batch operations to avoid flooding WebSocket listeners.

Batch Operations

Perform multiple file operations efficiently:

// Disable watcher for batch
const files = ['file1.js', 'file2.js', 'file3.js'];

for (const file of files) {
  await sandbox.files.create({
    filePath: `/opt/app/${file}`,
    content: '// Generated file',
    withWatcher: false
  });
}

// Re-enable watcher and notify once
await sandbox.files.create({
  filePath: '/opt/app/.batch-complete',
  content: '',
  withWatcher: true
});

Reading Large Files

Use range parameter for large files:

// Read first 100 lines
const chunk1 = await sandbox.files.get({
  filePath: '/opt/app/large-file.log',
  range: { start: 1, end: 100 }
});

// Read next 100 lines
const chunk2 = await sandbox.files.get({
  filePath: '/opt/app/large-file.log',
  range: { start: 101, end: 200 }
});

Error Handling

Common error scenarios:

try {
  await sandbox.files.get({ filePath: '/opt/app/missing.js' });
} catch (error) {
  if (error.status === 404) {
    console.log('File not found');
  } else if (error.status === 403) {
    console.log('Permission denied');
  } else {
    console.log('Error:', error.message);
  }
}

Best Practices

  1. Check existence before operations:

    const exists = await sandbox.files.exists({ filePath: path });
    if (!exists.exists) {
      await sandbox.files.create({ filePath: path, content: '' });
    }
  2. Use line ranges for large files: Don't read entire large files at once

  3. Disable watcher for batch operations: Improves performance for multiple file operations

  4. Handle errors gracefully: Always wrap file operations in try-catch

Next Steps