# Templates

A template is a sandbox prepared once and saved as a snapshot, so every sandbox made from it starts
in the same state. Installs and builds run once, when the template is made, never again per sandbox.

| 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.

```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')
```

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

`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',
)
```

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

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

`hermitbox template create` builds the plans Hermitbox ships for `python-interpreter`,
`node-interpreter`, `desktop`, `browser` and `tools`. Starter projects such as `nextjs`, `vite-react`
and `fastapi` are prepared on a container-capable node the first time a sandbox asks for one.
`hermitbox template from <repo>` prepares a Git repository the same way.

## Next steps

- [Templates from code](https://docs.hermitbox.dev/v0.2.1/guides/templates-from-code.md): every builder field and starter.
- [Code interpreters](https://docs.hermitbox.dev/v0.2.1/guides/code-interpreter.md) and [Native tools](https://docs.hermitbox.dev/v0.2.1/guides/browser-and-desktop.md): the built-in plans.
- [Lifecycle](https://docs.hermitbox.dev/v0.2.1/concepts/lifecycle.md): how snapshots and forks work.
