Ctrl + K

File Writing

Create new files, edit existing ones, and merge changes intelligently.

Create File

await sandbox.files.create({
  fullPath: '/opt/app/new-file.js',
  content: 'export default function hello() {\n  console.log("Hello!");\n}'
});
await fetch(`${sandbox.url}/files/create`, {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${sandbox.token}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    fullPath: '/opt/app/new-file.js',
    content: 'export default function hello() {\n  console.log("Hello!");\n}'
  })
});
curl -X POST https://sandbox.oblien.com/files/create \
  -H "Authorization: Bearer YOUR_SANDBOX_TOKEN" \
  -H "Content-Type: application/json"

Response:

{
  "success": true,
  "message": "File created successfully",
  "path": "/opt/app/new-file.js",
  "size": 62
}

Create Directory

await sandbox.files.create({
  fullPath: '/opt/app/new-folder',
  isFolder: true
});
await fetch(`${sandbox.url}/files/create`, {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${sandbox.token}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    fullPath: '/opt/app/new-folder',
    isFolder: true
  })
});
curl -X POST https://sandbox.oblien.com/files/create \
  -H "Authorization: Bearer YOUR_SANDBOX_TOKEN" \
  -H "Content-Type: application/json"

Edit File

Replace entire file content:

await sandbox.files.edit({
  filePath: '/opt/app/config.json',
  content: JSON.stringify({
    port: 3000,
    env: 'production'
  }, null, 2)
});
await fetch(`${sandbox.url}/files/edit`, {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${sandbox.token}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    filePath: '/opt/app/config.json',
    content: JSON.stringify({ port: 3000, env: 'production' }, null, 2)
  })
});
curl -X POST https://sandbox.oblien.com/files/edit \
  -H "Authorization: Bearer YOUR_SANDBOX_TOKEN" \
  -H "Content-Type: application/json"

Merge Changes

Intelligently merge content into existing files:

await sandbox.files.merge({
  filePath: '/opt/app/README.md',
  content: '\n## New Section\n\nAdditional documentation here.',
  options: {
    silent: false
  }
});
await fetch(`${sandbox.url}/files/merge`, {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${sandbox.token}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    filePath: '/opt/app/README.md',
    content: '\n## New Section\n\nAdditional documentation here.',
    options: { silent: false }
  })
});
curl -X POST https://sandbox.oblien.com/files/merge \
  -H "Authorization: Bearer YOUR_SANDBOX_TOKEN" \
  -H "Content-Type: application/json"

Upload Binary Files

For binary files (images, PDFs, etc.), use base64 encoding:

const imageBase64 = '...'; // base64 encoded image

await sandbox.files.upload({
  filePath: '/opt/app/logo.png',
  content: imageBase64
});
await fetch(`${sandbox.url}/files/upload`, {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${sandbox.token}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    filePath: '/opt/app/logo.png',
    content: imageBase64
  })
});
curl -X POST https://sandbox.oblien.com/files/upload \
  -H "Authorization: Bearer YOUR_SANDBOX_TOKEN" \
  -H "Content-Type: application/json"

Complete Example: Update Config

// Read existing config
const file = await sandbox.files.get({
  filePath: '/opt/app/config.json'
});

// Parse and update
const config = JSON.parse(file.content);
config.apiUrl = 'https://api.example.com';
config.timeout = 5000;

// Write updated config
await sandbox.files.edit({
  filePath: '/opt/app/config.json',
  content: JSON.stringify(config, null, 2)
});

console.log('Config updated successfully');
// Read existing config
let response = await fetch(`${sandbox.url}/files/get`, {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${sandbox.token}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ filePath: '/opt/app/config.json' })
});

const file = await response.json();

// Parse and update
const config = JSON.parse(file.content);
config.apiUrl = 'https://api.example.com';
config.timeout = 5000;

// Write updated config
await fetch(`${sandbox.url}/files/edit`, {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${sandbox.token}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    filePath: '/opt/app/config.json',
    content: JSON.stringify(config, null, 2)
  })
});
curl -X POST https://sandbox.oblien.com/files/get \
  -H "Authorization: Bearer YOUR_SANDBOX_TOKEN" \
  -H "Content-Type: application/json"

File Watcher Integration

Control whether file changes trigger WebSocket events:

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

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

Parameters

Create/Upload

ParameterTypeDefaultDescription
fullPathstring-Full file path
parentPathstring-Parent directory
fileNamestring-File name
contentstring-File content
isFolderbooleanfalseCreate directory
withWatcherbooleantrueTrigger file watcher

Edit/Merge

ParameterTypeDefaultDescription
filePathstringrequiredPath to file
contentstringrequiredNew content
optionsobject-Merge options
withWatcherbooleantrueTrigger file watcher

Best Practices

  1. Check if file exists before creating:

    const exists = await sandbox.files.exists({ filePath });
    if (!exists.exists) {
      await sandbox.files.create({ fullPath: filePath, content });
    }
  2. Use edit for complete replacements: Use edit() when replacing entire file content

  3. Use merge for partial updates: Use merge() when adding to existing content

  4. Disable watcher for batch operations: Set withWatcher: false for multiple operations

  5. Validate content before writing: Ensure content is valid before saving

Error Handling

try {
  await sandbox.files.create({
    fullPath: '/opt/app/new-file.js',
    content: 'console.log("test");'
  });
} catch (error) {
  if (error.message.includes('already exists')) {
    // File exists, use edit instead
    await sandbox.files.edit({
      filePath: '/opt/app/new-file.js',
      content: 'console.log("test");'
    });
  } else {
    console.error('Error:', error);
  }
}

Next Steps