# Templates from code

> Let your agent do it: Make a Hermitbox template from my project, so new sandboxes start with it ready. Write a `hermitbox.template.ts` that calls `hb.template()` with a name, a base image, a copy of the project and its install command, as the page's `hb.template()` example does, or use a built-in starter from the built-in templates reference if one fits. Build it with `hermitbox template build -f hermitbox.template.ts`, then create a sandbox from the template and show me that the project is there. Follow the exact steps in https://docs.hermitbox.dev/v0.3.2/guides/templates-from-code.md.

Get a sandbox that starts with your project installed and running: from any Git repository, from
one of 54 starters, or from a few lines of code. Or open a coding agent in a sandbox of its own,
with one command.

```sh
# Your repository, installed and running, with a preview URL.
hermitbox template from <repo>

# A coding agent in its own sandbox.
hermitbox create codex
```

Each of these makes a template: a sandbox prepared once and saved. Every sandbox made from it
clones that saved copy, so the install never runs again.

## Start a Git repository

Point Hermitbox at a repository and get it running in a sandbox, with a URL you can open.
`hermitbox template from <repo>` takes the current commit of an HTTPS repository or a local Git
checkout, builds a template from it and starts a fork. It prints the commit, the image it chose, the
install and start commands, the port, the sandbox ID and the preview URL. A local import uses only
the committed files, and the build runs inside a sandbox.

The importer recognizes:

- Node projects, through their npm scripts and lockfile.
- Python projects, through their `pyproject.toml` or `requirements.txt`; Django, FastAPI and Flask
  have starter commands.
- Go modules, Rack applications and PHP public document roots.
- Cargo binary projects, honouring a checked-in lockfile.
- Java projects, through their committed Maven wrapper or a standalone `Main.java`. A configured
  Spring Boot plugin and launcher select its development server.
- .NET projects, selecting a single executable or web project and binding ASP.NET Core to the
  requested port.

It also reads a development container's image and port settings, and a Dockerfile's base, exposed
port and command. For a library, or a project with more than one entry point, say what to run with
`--start`.

Every guess can be overridden: `--port`, `--install`, `--start` and `--check`. Use
`--image <reference@sha256:digest>` for a custom built image. Python commands run in a virtual
environment under `/data`.

The project needs an HTTP entry point and a host serving containers, and the node's image policy
must admit the selected image. Imports follow the project's resource defaults; use `--units` to
request a specific CPU and memory allocation.

## Coding agents

Give a coding agent a sandbox to work in, with one command:

```sh
hermitbox create codex
```

`hermitbox create --list` prints the agents Hermitbox knows. Hermitbox tries the tiers in order,
`vm`, then `gvisor`, `container` and `process`, tells you which one accepted the sandbox, and
installs the agent's recipe there. The first build needs outbound access to the package registry.
On the process tier, Hermitbox fetches the checksum-pinned Node or Python runtime into sandbox data.

Where a private cache is available, the CLI saves the ID of a snapshot taken before the agent
starts. Later sessions on this machine can start from it for the same endpoint, project and recipe.
A concurrent cold start can install its own copy.

### Sign in

Sign in at the agent's own prompt, or pass a key you already have set in your environment:
`hermitbox create codex --env OPENAI_API_KEY`. Hermitbox forwards its value directly to the
interactive process.

With forwarded keys, creation chooses gVisor, container or process, so a VM memory snapshot cannot
retain a key. Pass fresh keys at creation, or sign in interactively inside the sandbox. Reattaching
a running process keeps its environment.

### Come back to it

Agent sign-in files stay in the sandbox's data. Leaving the agent keeps the sandbox running until
its idle deadline (15 minutes idle, then it sleeps):

- `hermitbox connect <sandbox-id>` wakes it and opens another agent session. A disconnected
  terminal can be reattached with the printed process ID.
- `hermitbox fork <sandbox-id>` forks a snapshot.
- `hermitbox sandbox stop <sandbox-id>` stops the sandbox.

### Any other agent

An agent Hermitbox doesn't list installs from a pinned npm or pip package and its executable, such
as `hermitbox create npm:opencode-ai@1.19.6 --bin opencode`. An HTTPS release takes the URL of a
Linux executable, `--bin <name>` and `--sha256 <digest>` for the sandbox architecture.

## Built-in starters

Start from a working project instead of an empty sandbox. Request any of Hermitbox's 54 built-in
starters by name on a host serving containers:

```ts
const box = await hb.sandbox({ template: 'nextjs' })
console.log(box.previewUrl)
```

Creation waits for the development server to answer before exposing its port, so the URL works as
soon as you have it. Edit `app/page.jsx` to see hot reload at that URL. Python clients expose the
same URL as `box.preview_url`. If readiness fails, the error identifies the sandbox so you can
inspect its logs or delete it.

The first request prepares a project snapshot on an eligible node and waits for its locked
dependencies and tests. Concurrent requests share that preparation. Each sandbox then forks a copy
from an eligible node in its requested region and starts its own development server, and the fleet
warms copies on other eligible nodes. The node's image policy must admit the pinned image. To select
a different pinned image or build explicitly, use `hermitbox template create <name>`.

[Built-in templates](https://docs.hermitbox.dev/v0.3.2/reference/templates.md#starter-projects) lists the full catalogue, with
ports, editable files and build notes: web frameworks, JS/TS and Python backends, compiled
languages, databases, vector stores, notebooks, local AI, browser automation and desktop.

## Databases and caches

Give a project its own database or cache by naming it when you create the sandbox. Each fork gets
its own copy.

```ts
const box = await hb.sandbox({ template: 'nextjs', services: ['postgres'] })
```

- **`postgres`** starts the project with its own PostgreSQL database, and the application receives
  `DATABASE_URL`. Each fork owns the database files under `/data`, and PostgreSQL listens on
  container loopback. Preparing the composed starter verifies the PostgreSQL archive for the
  project image's architecture and C library before extracting it.
- **`redis`** gives a fork its own loopback cache, and the application receives `REDIS_URL`. The
  server waits for that fork's Redis process to answer before launching the application. Its
  append-only log lives in the fork's persistent data directory.

The same field works with the catalogue's Node and Python projects, Alpine language projects, local
model server and desktop. Each composed project warms its own snapshot, separate from the plain
starter's. An explicit application entrypoint still starts after its service is ready.

Repositories imported on one of these pinned images can request the same service by template name
or ID. The service uses the project's published snapshot and start command. Publishing a new
version warms a separate composed snapshot on first use. Keep the reserved service directory at
`/data/.hermitbox/services` available for the installer.

## Write a template in code

When you want to choose the image, the files and the commands yourself, describe the template in
code:

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

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

`hb.template({ name, from, copy, run, env, user, require, workdir, ready, start, sparePorts })`
prepares a sandbox with the files and process APIs, snapshots it, and registers an immutable
version. `copy` stays local: only the build plan and the finished snapshot reach control. The Python
sync and async clients accept the same fields.

Each `run` and `ready` command has up to ten minutes, and a failed one says how it ended. A failed
build cleans up its sandbox, and `template.rebuild()` creates the next version.

`hermitbox template build -f hermitbox.template.ts` runs that builder from the CLI.
`hermitbox template create` supplies built-in plans for `python-interpreter`, `node-interpreter`,
`desktop`, `browser`, `tools` and `sandbox-agent`; `--image` selects an operator-built image.
Creating from a ready template clones its snapshot and never repeats the install.

See [Built-in templates](https://docs.hermitbox.dev/v0.3.2/reference/templates.md) for every runtime, tool and starter template
Hermitbox ships, and [interpreters](https://docs.hermitbox.dev/v0.3.2/guides/code-interpreter.md) or [native tools](https://docs.hermitbox.dev/v0.3.2/guides/browser-and-desktop.md)
for their dependencies, host prerequisites and limits.
