# Templates

A template makes new sandboxes start ready to work, with dependencies installed and builds done.
You prepare a sandbox once and save it as a snapshot. Every sandbox made from it starts in that
state, and installs and builds never run again per sandbox.

Hermitbox also ships templates for interpreters, a browser, a desktop and 54 starter projects, and
makes one from a Git repository with one command ([Built-in templates](#built-in-templates)).

| Part | What it holds |
| --- | --- |
| Source | An image on `container`, `gvisor` or `vm`, or no image on `process` or `wasm` |
| Build | Files copied in, commands run, and an optional readiness check |
| Snapshot | The prepared data directory, saved as an immutable version |
| Start command | What runs in each new sandbox, in its data directory unless `workdir` says otherwise |
| Tier | The tier the snapshot was made on; every sandbox from it runs there too |

## Start from a template

Pass the template's name or ID when you create a sandbox. Hermitbox forks the newest version's
snapshot and runs its start command, so the sandbox starts with the template's work already done.

```ts
// Each sandbox from it starts with the dependencies installed.
const box = await hb.sandbox({ template: 'my-api' })
```

```python
# Each sandbox from it starts with the dependencies installed.
box = hb.sandbox(template='my-api')
```

```rust
use hermitbox::SandboxOptions;

// Each sandbox from it starts with the dependencies installed.
let sandbox = hb.create_sandbox(SandboxOptions::new().template("my-api")).await?;
```

A create takes exactly one source: an `image`, a `snapshot` or a `template`. A sandbox from a
template is an ordinary fork: it has its own ID and data, and changes never reach the template.

## Build one from code

Describe a template in your own code. `hb.template` starts a sandbox from `from`, copies your
files in, runs each `run` command, and snapshots the result. The copy happens from your machine;
only the build plan and the finished snapshot reach control.

```ts
// Copy the project in, install its dependencies, and save the result.
const template = await hb.template({
  name: 'my-api',
  from: 'node:22',
  copy: [['./', 'app']],
  run: ['cd /data/app && npm ci'],
  start: 'cd app && npm start',
})
```

```python
# Copy the project in, install its dependencies, and save the result.
template = hb.template(
    name='my-api',
    from_='node:22',
    copy=[('./', 'app')],
    run=['cd /data/app && npm ci'],
    start='cd app && npm start',
)
```

```rust
use hermitbox::TemplateOptions;

// Copy the project in, install its dependencies, and save the result.
let template = hb
    .template(
        TemplateOptions::new("my-api")
            .from_image("node:22")
            .copy_path("./", "app")
            .run_step("cd /data/app && npm ci")
            .start("cd app && npm start"),
    )
    .await?;
```

Each `run` and `ready` command has up to ten minutes. A failed step says how it ended (its exit
code, a signal or a timeout), and the build deletes its sandbox. The CLI runs the same builder from
a file: `hermitbox template build -f hermitbox.template.ts`.

## Template versions

Rebuild a template when its project changes. Each build registers a new, numbered version, and a
version never changes. `template.rebuild()` builds the next one from the same local files. New
sandboxes start from the newest version, and `hb.templates()` lists them.

A snapshot that a version uses can't be deleted, and neither can the sandbox that built it, while
that version exists.

## Templates from a spec

`POST /v1/templates` takes a `name` and a `spec`: the same fields as a create, such as `image`,
`isolation`, `entrypoint` and `env`, with no snapshot or template as its source. Control prepares
it once on each active node that serves the spec's tier and keeps a stopped snapshot there. A
sandbox from it forks a ready copy in its region.

Bucket mounts carry credentials, so a template never stores them. Pass them when you create each
sandbox.

## Built-in templates

Common runtimes, tools and full starter stacks work without writing a build plan first. Hermitbox
ships three kinds of built-in templates for the `container` tier.

### Runtime and tool plans

`hermitbox template create <name>` prepares Hermitbox's released container images (or an
operator-built image with `--image`):

| Template | What it provides |
| --- | --- |
| [`python-interpreter`](https://docs.hermitbox.dev/v0.3.2/reference/templates.md#python-interpreter) | CPython 3.12 with a prestarted IPython kernel and scientific libraries (`box.python()`) |
| [`node-interpreter`](https://docs.hermitbox.dev/v0.3.2/reference/templates.md#node-interpreter) | Node.js 24 with a prestarted IJavascript kernel and Chart.js (`box.javascript()`) |
| [`browser`](https://docs.hermitbox.dev/v0.3.2/reference/templates.md#browser) | Headless Chromium and Playwright with screenshots and expiring CDP URLs (`box.browser()`) |
| [`desktop`](https://docs.hermitbox.dev/v0.3.2/reference/templates.md#desktop) | X11 desktop (Xvfb, JWM, noVNC) with computer-use actions and note preview (`box.desktop()`) |
| [`tools`](https://docs.hermitbox.dev/v0.3.2/reference/templates.md#tools) | OpenSSH and `rclone` for interactive SSH sessions and S3/R2 bucket mounts (`box.ssh()`, `box.mount()`) |
| [`sandbox-agent`](https://docs.hermitbox.dev/v0.3.2/reference/templates.md#sandbox-agent) | Rivet's `sandbox-agent` server on private port 3000 for runner harnesses |

### Starter projects

Passing a starter name such as `nextjs`, `vite-react` or `fastapi` to `hb.sandbox({ template })`
prepares a warm, tested snapshot on a container-capable node the first time a sandbox asks for it,
then forks a copy with a ready preview URL.

Hermitbox ships 54 starters across
[frontend and full-stack web](https://docs.hermitbox.dev/v0.3.2/reference/templates.md#frontend-and-full-stack-web),
[JS/TS backends](https://docs.hermitbox.dev/v0.3.2/reference/templates.md#javascript-and-typescript-backends),
[Python web](https://docs.hermitbox.dev/v0.3.2/reference/templates.md#python-web),
[native and compiled backends](https://docs.hermitbox.dev/v0.3.2/reference/templates.md#native-and-compiled-backends),
[databases and caches](https://docs.hermitbox.dev/v0.3.2/reference/templates.md#databases-and-caches),
[vector stores](https://docs.hermitbox.dev/v0.3.2/reference/templates.md#vector-stores),
[notebooks, local search and AI](https://docs.hermitbox.dev/v0.3.2/reference/templates.md#notebooks-local-search-and-ai), and
[browser automation and desktop](https://docs.hermitbox.dev/v0.3.2/reference/templates.md#browser-automation-and-desktop).
Compatible starters also accept `services: ['postgres']` or `services: ['redis']` to warm a
fork-local database or cache alongside the app.

### Git repositories

Point Hermitbox at a repository and get a running copy with a preview URL.
`hermitbox template from <repo>` inspects an HTTPS repository or local Git checkout, infers its
image, install command, start command and port, builds a template from the committed files, and
launches a fork with a preview URL.

## Next steps

- [Built-in templates](https://docs.hermitbox.dev/v0.3.2/reference/templates.md): every runtime, tool and starter template Hermitbox ships.
- [Templates from code](https://docs.hermitbox.dev/v0.3.2/guides/templates-from-code.md): build custom templates, compose services and import Git repositories.
- [Code interpreters](https://docs.hermitbox.dev/v0.3.2/guides/code-interpreter.md) and [Native tools](https://docs.hermitbox.dev/v0.3.2/guides/browser-and-desktop.md): deep dives on interpreters, browsers, desktops, SSH and mounts.
- [Lifecycle](https://docs.hermitbox.dev/v0.3.2/concepts/lifecycle.md): how snapshots and forks work.
