Read, write, list, and delete files inside a Blaxel sandbox using a simple file system interface from the SDK, MCP server, or REST API.
Manage files and directories within sandboxes through the fs module of Blaxel SDK. This module provides essential operations for creating, reading, writing, copying, and deleting files and directories.
Complete code examples demonstrating all operations are available on Blaxel’s GitHub: in TypeScript, in Python, and in Go.
The Blaxel SDK requires two environment variables to authenticate:
Variable
Description
BL_WORKSPACE
Your Blaxel workspace name
BL_API_KEY
Your Blaxel API key
You can create an API key from the Blaxel console. Your workspace name is visible in the URL when you log in to the console (e.g. app.blaxel.ai/{workspace}).Set them as environment variables or add them to a .env file at the root of your project:
The Blaxel SDK does not accept credentials as constructor arguments. Credentials must come from environment variables, a .env file, or a local CLI login session (see below).When developing locally, you can also log in to your workspace with Blaxel CLI (as shown above). This allows you to run Blaxel SDK functions that will automatically connect to your workspace without additional setup. When you deploy on Blaxel, authentication is handled automatically — no environment variables needed.
You can write multiple files or directories simultaneously. The second path parameter in writeTree specifies the base directory for writing the file tree, eliminating the need to repeat the full path for each file.
The watch function monitors all file system changes in the specified directory. You can also watch subdirectories by passing a /my/directory/** pattern.By default (when withContent: false), the events will only include metadata about the changes, not the actual file contents. Here’s what you’ll get in the callback events:
For ALL operations (CREATE, WRITE, DELETE, etc.), you’ll receive:
op: The operation type (e.g., “CREATE”, “WRITE”, “DELETE”)
path: The directory path where the change occurred
name: The name of the file/directory that changed
You will NOT receive:
The actual content of the files
File contents for CREATE or WRITE operations
// You can specify if you want the content of the files or notconst handle = sandbox.fs.watch("/", (fileEvent) => { console.log(fileEvent.op, fileEvent.path, fileEvent.content)}, { withContent: true});// Do file operations// At the end, close the watch handlehandle.close();
def watch_callback(file_event): print(file_event.op, file_event.path, file_event.content)# You can specify if you want the content of the files or nothandle = sandbox.fs.watch("/", watch_callback, { "with_content": True })# Do file operations# At the end, close the watch handlehandle["close"]()
def watch_callback(file_event): print(file_event.op, file_event.path)# You can specify if you want the content of the files or nothandle = sandbox.fs.watch("/folder/**", watch_callback)
def watch_callback(file_event): print(file_event.op, file_event.path)# You can specify if you want the content of the files or nothandle = sandbox.fs.watch("/", watch_callback, { "ignore": ["/folder", "/folder_two/test2.txt"]})
Specify withContent: true so the events include the actual file contents.