Skip to content

Your first template

A template defines the image, files, packages, and processes in a sandbox. Build it once, then start identically prepared sandboxes by name or tag.

This template copies an HTML file, sets the working directory, and starts an HTTP server. AgentBox waits for the server to respond, builds version v1, and starts a sandbox from it.

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

const templateName = process.env.TEMPLATE_NAME ?? "web-template";

const template = Template({ fileContextPath: "." })
  .fromBaseImage()
  .setWorkdir("/home/user/app")
  .copy("index.html", "/home/user/app/index.html")
  .setStartCmd(
    "python3 -m http.server 8080",
    waitForURL("http://localhost:8080"),
  );

const build = await Template.build(template, templateName, {
  tags: ["v1"],
});

console.log(`Built ${build.name}:v1`);

const sandbox = await Sandbox.create(`${templateName}:v1`);

try {
  const result = await sandbox.commands.run(
    "whoami && pwd && curl --silent http://localhost:8080",
  );
  console.log(result.stdout);
} finally {
  await sandbox.kill();
}

Place an index.html file containing Hello from an AgentBox template next to the template source file.

What the example does

Template() creates the template definition. The chained methods select a base image, add its contents, and define the process that must be running in a ready sandbox.

Template.build sends the definition to the builder. The name groups all builds of a template, while the tag identifies a specific version.

Sandbox.create accepts the tagged name. The working directory, file, and HTTP process are already present in the running sandbox.

You need an API key to build the template. See the SDK reference for every method.