Skip to main content
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.

Basic file system operations

The Blaxel SDK requires two environment variables to authenticate: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.

Create directory

Create a new directory at a specific path in the sandbox:

List files

List files in a specific path:

Address file paths over HTTP

Filesystem paths are part of the URL when you call the sandbox API directly over HTTP. The API resolves a path without a leading slash relative to the filesystem service’s working directory. To address an absolute path, URL-encode the leading slash as %2F. For example, if the filesystem working directory is /app: The CLI’s double-slash form, such as --path /filesystem//workspace, is the CLI equivalent for an absolute path. For a direct HTTP request, use %2F for the leading slash:
The same rule applies when initiating a multipart upload. The following example starts an upload to the absolute path /workspace/archive.tar, uploads one part, and completes the upload. Repeat the part request with increasing partNumber values for additional parts, then include every returned etag in the completion request.
This URL-encoding requirement applies only when the filesystem path is embedded in an HTTP URL. SDK filesystem methods accept normal paths such as /workspace.

Find files and directories

Blaxel’s Sandbox API uses an optimized find() method, which is faster than using the native find tool.
Find files and directories matching specified patterns:

Search for text content within files

Blaxel’s Sandbox API uses an optimized grep() method, which is faster than using the native grep tool.
Find files containing specified text content:

Read file

Read a file from a specific filepath:

Read many files

Discover relative file paths with fs.find, then read text files in batches of four. This returns flat path/content pairs with one discovery call plus one read call per file, not a single bulk-read request. find defaults to 20 results, so do not assume its default response includes every file. Request one more than your chosen file cap and reject the result if that extra match is returned. excludeDirs (exclude_dirs in Python) matches directory names, not path globs; apply file exclusions by filtering paths separately.

Export a whole directory as one archive

Create an archive on disk with tar, then download it with one binary read. This uses two calls for creation and download, plus a cleanup call. Your sandbox image must include tar. Keep the temporary archive outside the directory you are exporting.
Do not pipe base64-encoded file contents through process logs. Output can be truncated, and file contents are exposed in retained logs. Use the binary filesystem read instead.
Neither recipe guarantees a consistent snapshot if files change during the operation.

Write file

Create a file in a specific path:
See down below for how to upload/write a binary, or multiple files at once.

Write multiple files

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.

Read binary file

Read a binary file from the sandbox filesystem:
The binary content is returned as a Web API Blob object.

Write binary file

Write binary content to a file in the sandbox filesystem:
The binary content to write can be provided as:
  • Buffer: Node.js Buffer object
  • Blob: Web API Blob object
  • File: Web API File object
  • Uint8Array: Typed array containing binary data
If you’re calling the sandbox filesystem API directly over HTTP instead of using an SDK, a single request is capped at 5 MB. Requests larger than this are rejected with an HTTP 413 before they reach the sandbox. Therefore, if using HTTP, larger files must be uploaded in parts via the sandbox’s multipart upload endpoints rather than in a single request. Blaxel SDKs handle this automatically: SDK functions like write(), writeBinary(), and write_binary() automatically split files over 5 MB and send via multipart upload in 5 MB chunks.

Download file to host

Download a file from the sandbox filesystem to the host:

Copy file

Copy a file from a path to another path:

Delete file or directory

Delete a file or directory by specifying its path:

Watch filesystem for events

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:
  1. For ALL operations (CREATE, WRITE, DELETE, etc.), you’ll receive:
    1. op: The operation type (e.g., “CREATE”, “WRITE”, “DELETE”)
    2. path: The directory path where the change occurred
    3. name: The name of the file/directory that changed
  2. You will NOT receive:
    1. The actual content of the files
    2. File contents for CREATE or WRITE operations

Watch sub-directories

Watch all sub-directories recursively with /**:

Ignore files or directories

You can ignore changes in certain files or directories by providing an array of filepaths to ignore:
Specify withContent: true so the events include the actual file contents.
Last modified on October 5, 2026