Skip to content

Read and write files ​

files.write creates a file or replaces its contents. Missing parent directories are created automatically. JavaScript and Python return text from files.read by default. In Go, use Files.ReadText for text.

Write and read one file ​

mjs
import { Sandbox } from "@abox-dev/sdk";

const sandbox = await Sandbox.create();

try {
  await sandbox.files.write("/tmp/message.txt", "hello from AgentBox");
  const message = await sandbox.files.read("/tmp/message.txt");
  console.log(message);
} finally {
  await sandbox.kill();
}

Pass { format: "bytes" } in JavaScript or format="bytes" in Python for binary data; in Go use ReadBytes. All three SDKs support streamed reads for large files, with Go exposing an io.ReadCloser through Read.

Write several files ​

Use writeFiles in JavaScript, write_files in Python, or WriteBatch in Go to send several files in one operation:

mjs
import { Sandbox } from "@abox-dev/sdk";

const sandbox = await Sandbox.create();

try {
  await sandbox.files.writeFiles([
    { path: "/tmp/notes/first.txt", data: "First note" },
    { path: "/tmp/notes/second.txt", data: "Second note" },
  ]);

  const entries = await sandbox.files.list("/tmp/notes");
  console.log(entries.map((entry) => entry.name).join(", "));
} finally {
  await sandbox.kill();
}

files.list returns the entries in a directory. Use makeDir / make_dir, rename, and remove for the other common filesystem operations.

Select a user ​

Set user to resolve ~ in that user's home directory and create files owned by that user. Use the same user for writing, reading, and looking up the file.

mjs
import { Sandbox } from "@abox-dev/sdk";

const sandbox = await Sandbox.create();

try {
  await sandbox.files.write("~/message.txt", "hello from AgentBox", {
    user: "root",
  });
  const message = await sandbox.files.read("~/message.txt", { user: "root" });
  console.log(message);
  const info = await sandbox.files.getInfo("~/message.txt", { user: "root" });
  console.log(info.path, info.owner);
} finally {
  await sandbox.kill();
}