# Templates from code

A template is a sandbox prepared once and saved, so every sandbox made from it starts ready.

```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. A failed
build cleans up its sandbox. `template.rebuild()` creates the next version. 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.

`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` and `tools`; `--image` selects an operator-built image. Creating from a ready
template clones its snapshot and never repeats the install. See [interpreters](https://docs.hermitbox.dev/v0.2.0/interpreter.md) and
[native tools](https://docs.hermitbox.dev/v0.2.0/native-tools.md) for their dependencies, host prerequisites and limits.

## Next.js project

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

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

Creation waits for the development server to answer before exposing its port.
Edit `app/page.jsx` to see hot reload at the returned 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.

## Vite projects

Choose `vite-react` for React or `vue` for Vue. Both use the same pinned Node runtime and require a container-capable node. Their first build installs the locked dependencies and tests the production build. Each fork serves its own Vite development preview on port 5173. Edit `src/App.jsx` or `src/App.vue` in the fork to see updates in the browser.

## Node HTTP APIs

Choose `express`, `hono` or `fastify` to fork a working HTTP API on a container-capable node. Each starter uses the same pinned Node image as Next.js. The first build installs its locked dependencies and tests the root JSON route over HTTP. Each fork starts its own server on port 3000 and returns its preview URL once the route answers. Edit `app.mjs` to change the response at that URL. Run `npm test` to exercise the routes and `npm run build` to check the JavaScript entry points.

## SvelteKit

Choose `sveltekit` for a SvelteKit web project. The warm build serves the production Node adapter's output over HTTP to check the home page. Each fork starts Vite on port 5173 and returns the ready preview URL. Edit `src/routes/+page.svelte` to see the page update. Use `npm run build` and `npm start` to run the production server.

## A database or cache alongside a project

Request `hermitbox.sandbox({ template: 'nextjs', services: ['postgres'] })` to start the project with its own PostgreSQL database. Its application receives `DATABASE_URL`. Each fork owns the database files under `/data` and PostgreSQL listens on container loopback. The plain starter has a separate snapshot. Preparing the composed starter verifies the PostgreSQL archive for the project image’s architecture and C library before extracting it.

Choose `services: ['redis']` for a fork with its own loopback cache. Its 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 catalog’s Node and Python projects, Alpine language projects, local model server and desktop. Each composed project warms its own snapshot. 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.

## Start a Git repository

`hermitbox template from <repo>` prepares the current commit of an HTTPS repository or a local
Git checkout and starts a fork. It prints the commit, selected image, install and start commands,
port, sandbox ID and preview URL. A local import uses the committed files. The build runs inside
a sandbox. Node projects use their npm scripts and lockfile. Python projects use their
`pyproject.toml` or `requirements.txt`; Django, FastAPI and Flask have starter commands. Native imports recognize Go modules, Cargo binary projects, Rack applications and PHP public document roots. Cargo honors a checked-in lockfile. Java projects use their committed Maven wrapper or a standalone `Main.java`. A configured Spring Boot plugin and launcher select its development server. .NET imports select a single executable or web project, binding ASP.NET Core to the requested port. Supply `--start` for libraries and ambiguous entry points.

The importer reads development-container image and port settings and a Dockerfile's base,
exposed port and command. Use `--image <reference@sha256:digest>` for a custom built image.
Override individual guesses with `--port`, `--install`, `--start` and `--check`. Python commands run in a virtual environment under `/data`. The project needs
an HTTP entry point and container service. 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.

## Astro

Choose `astro` to fork a static site whose production home page has already passed its build test. Each fork starts Astro on port 4321. Edit `src/pages/index.astro` to reload the page at the preview URL. Run `npm run build` to write the production site to `dist/` and `npm run preview` to serve it.

## Nuxt and React Router

Choose `nuxt` to fork a Nuxt application with a ready preview on port 3000. Edit `app/app.vue` for Vue hot reload. Run `npm test` to build and exercise its Nitro Node server, or use `npm run build` followed by `npm start` for the production site.

Choose `react-router` for server-rendered React Router in framework mode. Edit `app/routes/home.jsx` for Fast Refresh and `app/routes.ts` to declare routes. The build tests the production route handler. Run `npm start` to serve its output.

## Angular

Choose `angular` for an Angular standalone component served with Vite. Edit `src/app.html` or `src/app.ts` for component updates. Run `npm test` to compile and check the site, `npm run build` to write `dist/` and `npm run preview` to serve the production output.

## SolidStart

Choose `solidstart` for a server-rendered Solid application. Edit `src/app.jsx` to update the interactive page. Run `npm test` to build and exercise its Nitro Node server, or `npm run build` followed by `npm start` to serve the production output.

## Expo for web

Choose `expo-web` to start an Expo SDK app for the web. Edit `App.jsx` to change its React Native Web components. Run `npm test` to check the Metro production export and `npm run build` to write the static site to `dist/`.

## Python projects

Use `python` for the standard-library web server, or choose `flask`, `fastapi` or `django` for a running application. Each serves a preview on port 8000 and includes a runnable unittest. Edit `public/index.html` in the Python starter or `app.py` in a framework project. Framework forks include their installed virtual environment and reload Python edits.

## Embedded databases

Choose `sqlite` or `duckdb` for a seeded database and a JSON preview on port 8000. Edit `query.sql` in the fork and refresh the preview to see up to 100 rows. The preview opens a read-only connection. Local project scripts can open the same database for writing, and each fork keeps its own database file.

## Node and standalone data services

Choose `node` for a Node project using the built-in HTTP server, test runner and file watcher. Edit `store.mjs` to update its preview on port 3000. Choose `postgres` or `redis` to start a fork-local database with the same JSON preview. Updating row 1 of PostgreSQL’s `messages` table or Redis’s `message` key changes the next preview response. Each project’s `AGENTS.md` includes its client command.

Choose `bun` or `deno` for a TypeScript HTTP project running on the selected runtime. Edit `app.ts` to update the response. Both projects include native tests and a watched preview on port 3000.

Choose `nestjs` for a TypeScript NestJS API. Run `npm test` to check the API and `npm run build` to compile it. Each fork starts Nest in watch mode.

Choose `go`, `ruby` or `php` for a native HTTP project on a container host. Edit `main.go`, `message.rb` or `public/index.php` and reload the preview. The Go project rebuilds automatically. Each project has tests and an `AGENTS.md` with its run and build commands.

Choose `rust` or `axum` for a Rust HTTP service with a locked Cargo build, source tests and an editable preview.

Choose `java`, `dotnet` or `aspnet` to run the JDK HTTP server, a .NET socket server or an ASP.NET Core API. Edit `Main.java` or `Program.cs` to update the running project.

Choose `spring-boot` for a Java MVC API. Each fork includes a warmed Maven project, a controller you can edit and a development server.

Choose `rails` for a Ruby MVC API. Edit the greeting controller and Rails reloads it on the next request. Its checksummed bundle includes both Linux architectures.

Choose `laravel` for a PHP API. Edit `app/Http/Controllers/GreetingController.php` to change the preview. Run `php .hermitbox/composer.phar --no-plugins test` for the project tests and `php artisan optimize` to build the production configuration.

Choose `phoenix` for an Elixir web project. Edit the controller in `lib/` and reload the preview. Run `mix test` for the suite or `MIX_ENV=prod mix release --overwrite` to build the release.

## JupyterLab

Choose `jupyter` for a running JupyterLab with NumPy, pandas, Matplotlib, SciPy and scikit-learn. Read the fork’s login token with `await box.read('.hermitbox/jupyter/access-token')` and paste it into the preview’s login form. Edit `sales.csv` and rerun `analysis.ipynb` to update the chart. The warm build executes the notebook and checks both the total and the plot.

## Local document search

Choose `langgraph` for a search graph backed by SQLite FTS5, or `llamaindex` for a vector index using local TF–IDF embeddings. Each starts a search page and a JSON endpoint. Edit `documents.json` to change the results, or `pipeline.py` to change how the project retrieves them. Both run without a provider account.

## CPU model server

Choose `llama-cpp` for a local chat preview and an OpenAI-compatible `v1/chat/completions` endpoint. The initial preparation fetches and verifies the 105 MB SmolLM2 model. Forks use the warmed model and run inference on the CPU. Edit `public/index.html` to change the chat page.

## Vector stores

Choose `qdrant` or `chroma` to start a persistent vector database with an editable similarity-search page. The sample compares three coordinates for each point. Edit `vectors.json` to update the starter points on the next request. You can also use the native database API from the fork. Qdrant listens on loopback port 6333 and Chroma on 8001. The preview returns a point’s title when supplied and otherwise its native ID.

Choose `mongodb` on a self-hosted node to pull the pinned upstream MongoDB Community image and prepare a local snapshot. Edit `query.json` to filter, project or sort the fork’s inventory documents. The database listens on a private Unix socket.

Choose `mysql` for a fork-local MySQL database and an editable SQL preview. Edit `query.sql` and refresh to see up to 100 result rows. The MySQL client connects over the private Unix socket described in the project's `AGENTS.md`.

Choose `pgvector` for PostgreSQL’s native vector extension. The JSON preview executes `query.sql` against the fork-local database. Change its coordinates and refresh to see the new nearest point, or connect with the bundled `psql` client to add your own data and indexes.

Choose `ai-sdk-chat`, `mastra`, `openai-agents` or `crewai` for chat through the selected SDK. Each runs the CPU model in the same fork and returns its application’s preview. The project reads `instructions.txt` for each message and serves edits to `public/index.html` on refresh.

Choose `playwright`, `puppeteer` or `browser-use` on a container node that reports the Chromium sandbox guarantee. Edit `page.html` and select **Run browser test** in the preview. The selected SDK drives the sample page in Chromium, clicks its counter and returns the screenshot. The browser and the browser-use controller run inside the fork as an ordinary user.

Choose `desktop` for the editable graphical note and terminal. The project serves the note's contents in its preview, while `box.desktop()` opens the graphical session with an expiring access URL. Edit `notes.txt` through the file API or save it from the graphical editor.

## Coding agents

Run `hermitbox create codex` to open Codex in a sandbox. `hermitbox create --list` prints the agent names.
hermitbox tries VM, gVisor, container and process in that order, reports the tier that accepted the
sandbox, and installs the recipe in that sandbox. When a private cache is available, the CLI saves
the ID of a snapshot taken before the agent starts. Later sessions on this machine can use it for
the same endpoint, project and recipe. A concurrent cold start can install its own copy. The sandbox needs outbound
access to the package registry on its first build. On the process tier hermitbox fetches the checksum-pinned Node or Python runtime into sandbox data.

Sign in at the agent's own prompt, or use `hermitbox create codex --env OPENAI_API_KEY` with the
variable already set in your environment. 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. Agent sign-in files stay in the
sandbox's data. Leaving the agent keeps the sandbox running until its idle deadline.
`hermitbox connect ID` wakes it and opens another agent session. A disconnected terminal can be
reattached with the printed process ID. `hermitbox fork ID` forks a snapshot and `hermitbox sandbox stop ID`
stops the sandbox.

Generic installations accept 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.
