# Connect a TypeScript backend

> Let your agent do it: Connect my Node or Bun backend to Hermitbox. Install the `hermitbox` package from npm with the project's package manager. On this machine `new Hermitbox()` reads the saved credentials automatically; on another machine, set `HERMITBOX_API_URL` and `HERMITBOX_API_KEY` in the backend's private environment, never in a browser bundle, a file or this chat. Run the page's quickstart and snapshot-and-fork examples with the project's TypeScript runner and show me their output. Follow the exact steps in https://docs.hermitbox.dev/v0.3.2/get-started/typescript.md.

Give your Node or Bun backend sandboxes: create one, run a command in it and delete it with the
TypeScript SDK.

Install the SDK from npm:

```sh
npm install hermitbox
```

## Quickstart

```ts
import { Hermitbox } from 'hermitbox'

const hb = new Hermitbox()
const box = await hb.sandbox()
console.log((await box.run('echo hello')).stdout.trim())
await box.delete()
```

On the machine where you installed Hermitbox, `new Hermitbox()` connects automatically. From
another machine, set `HERMITBOX_API_URL` and `HERMITBOX_API_KEY` in the backend's private
environment.

## Snapshot and fork

Try a risky change without touching the original. Save a sandbox at a clean checkpoint, fork an
independent copy, change the fork and throw it away:

```ts
import { Hermitbox } from 'hermitbox'

const hb = new Hermitbox()
const box = await hb.sandbox()

try {
  await box.write('config.json', '{"mode":"safe"}')
  const snapshot = await box.snapshot()
  const fork = await snapshot.fork()

  try {
    await fork.write('config.json', '{"mode":"experimental"}')
    console.log('original:', await box.read('config.json'))
    console.log('fork:', await fork.read('config.json'))
  } finally {
    await fork.delete()
  }
} finally {
  await box.delete()
}
```

Output:

```txt
original: {"mode":"safe"}
fork: {"mode":"experimental"}
```

Keep this code on the server. A browser bundle must never contain a project key. On serverless
platforms, await work inside the request lifetime or use a supported background worker: the
platform can stop an unawaited promise when the request ends.

Long streams need a runtime that permits their duration. The host tier still controls
[capabilities and isolation](https://docs.hermitbox.dev/v0.3.2/reference/tiers.md).
[SDK reference](https://docs.hermitbox.dev/v0.3.2/sdk/typescript.md).
