Skip to main content
Version: 0.1.22
Last Verified: 2025-01-27
Package: @hopx-ai/sdk on npm

Overview

The Files resource provides file operations for sandboxes. Access it via the files property on a Sandbox instance. All file operations are async and work with both text and binary files.

Access

import { Sandbox } from '@hopx-ai/sdk';

const sandbox = await Sandbox.create({ template: 'code-interpreter' });
const files = sandbox.files; // Lazy-loaded

Methods

read

Read text file contents.
async files.read(path: string): Promise<string>
Parameters:
  • path (string): File path to read
Returns: Promise<string> - File contents as string Example:
const content = await sandbox.files.read('/workspace/script.py');
console.log(content);

readBytes

Read binary file contents.
async files.readBytes(path: string): Promise<Buffer>
Returns: Promise<Buffer> - File contents as Buffer

write

Write text file.
async files.write(
    path: string,
    content: string,
    options?: FileWriteOptions
): Promise<void>
Parameters:
  • path (string): File path to write
  • content (string): Text content
  • options.mode (string, optional): File permissions

writeBytes

Write binary file.
async files.writeBytes(
    path: string,
    content: Buffer,
    options?: FileWriteOptions
): Promise<void>

list

List directory contents.
async files.list(path: string): Promise<EnhancedFileInfo[]>
Returns: Promise<EnhancedFileInfo[]> - Array of file/directory information objects

exists

Check if file/directory exists.
async files.exists(path: string): Promise<boolean>
Returns: Promise<boolean> - true if exists, false otherwise

remove

Remove file or directory.
async files.remove(path: string): Promise<void>

mkdir

Create directory.
async files.mkdir(path: string): Promise<void>

download

Download file (alias for readBytes).
async files.download(path: string): Promise<Buffer>

Examples

Example 1: Basic File Operations

import { Sandbox } from '@hopx-ai/sdk';

async function main() {
    const sandbox = await Sandbox.create({ template: 'code-interpreter' });
    
    // Write a file
    await sandbox.files.write('/workspace/hello.py', "print('Hello, World!')");
    
    // Read it back
    const content = await sandbox.files.read('/workspace/hello.py');
    console.log(content);
    
    // Execute it
    const result = await sandbox.runCode(
        "exec(open('/workspace/hello.py').read())"
    );
    console.log(result.stdout);
    
    await sandbox.kill();
}

main();

  • Sandbox - Main sandbox class
  • Commands - Command execution resource
  • Types - TypeScript type definitions

See Also

Next Steps