File Reading
Read file contents with support for line ranges, line numbers, and encoding detection.
Read Entire File
const file = await sandbox.files.get({
filePath: '/opt/app/src/index.js'
});
console.log(file.content);
console.log(`Size: ${file.size} bytes`);
console.log(`Lines: ${file.lines}`);const response = await fetch(`${sandbox.url}/files/get`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${sandbox.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
filePath: '/opt/app/src/index.js'
})
});
const file = await response.json();curl -X POST https://sandbox.oblien.com/files/get \
-H "Authorization: Bearer YOUR_SANDBOX_TOKEN" \
-H "Content-Type: application/json"Response:
{
"success": true,
"content": "import express from 'express';\n\nconst app = express();",
"path": "/opt/app/src/index.js",
"size": 1245,
"lines": 42
}Read with Line Numbers
const file = await sandbox.files.get({
filePath: '/opt/app/src/index.js',
withLineNumbers: true
});
console.log(file.content);
// Output:
// 1| import express from 'express';
// 2|
// 3| const app = express();const response = await fetch(`${sandbox.url}/files/get`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${sandbox.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
filePath: '/opt/app/src/index.js',
withLineNumbers: true
})
});
const file = await response.json();curl -X POST https://sandbox.oblien.com/files/get \
-H "Authorization: Bearer YOUR_SANDBOX_TOKEN" \
-H "Content-Type: application/json"Read Line Range
Read specific lines from a file:
// Read lines 10-20
const file = await sandbox.files.get({
filePath: '/opt/app/src/server.js',
range: {
start: 10,
end: 20
},
withLineNumbers: true
});const response = await fetch(`${sandbox.url}/files/get`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${sandbox.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
filePath: '/opt/app/src/server.js',
range: { start: 10, end: 20 },
withLineNumbers: true
})
});curl -X POST https://sandbox.oblien.com/files/get \
-H "Authorization: Bearer YOUR_SANDBOX_TOKEN" \
-H "Content-Type: application/json"Response:
{
"success": true,
"content": " 10| function handleRequest(req, res) {\n 11| // Handler logic\n 12| }",
"path": "/opt/app/src/server.js",
"range": { "start": 10, "end": 20 },
"linesReturned": 11
}Reading Large Files
For large files, read in chunks using line ranges:
// Read first 1000 lines
const chunk1 = await sandbox.files.get({
filePath: '/opt/app/large-file.log',
range: { start: 1, end: 1000 }
});
// Read next 1000 lines
const chunk2 = await sandbox.files.get({
filePath: '/opt/app/large-file.log',
range: { start: 1001, end: 2000 }
});Complete Example
// Check if file exists
const exists = await sandbox.files.exists({
filePath: '/opt/app/config.json'
});
if (exists.exists) {
// Read file content
const file = await sandbox.files.get({
filePath: '/opt/app/config.json'
});
// Parse JSON
const config = JSON.parse(file.content);
console.log('Config:', config);
}// Check if file exists
let response = await fetch(`${sandbox.url}/files/exists`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${sandbox.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
filePath: '/opt/app/config.json'
})
});
const exists = await response.json();
if (exists.exists) {
// Read file content
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();
const config = JSON.parse(file.content);
}curl -X POST https://sandbox.oblien.com/files/exists \
-H "Authorization: Bearer YOUR_SANDBOX_TOKEN" \
-H "Content-Type: application/json"Parameters
| Parameter | Type | Description |
|---|---|---|
filePath | string | Full path to file (required) |
range | object | Line range { start, end } |
withLineNumbers | boolean | Include line numbers in output |
Best Practices
-
Check file exists first:
const exists = await sandbox.files.exists({ filePath }); if (exists.exists) { const file = await sandbox.files.get({ filePath }); } -
Use ranges for large files: Don't read entire multi-megabyte files at once
-
Enable line numbers for debugging: Makes it easier to reference specific lines
-
Handle encoding properly: The API auto-detects encoding
Error Handling
try {
const file = await sandbox.files.get({
filePath: '/opt/app/missing.js'
});
} catch (error) {
if (error.message.includes('not found')) {
console.log('File does not exist');
} else {
console.error('Error reading file:', error);
}
}Next Steps
- File Writing - Create and edit files
- File Listing - List directory contents
- File Operations - All file operations