# Connect a TypeScript backend

> Let your agent do it: Connect my Node or Bun backend to Hermitbox. Install `hermitbox` with the backend's package manager, and 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 module with the project's TypeScript runner: it uses the install's default tier, adds an image only for containers, prints hello and deletes its sandbox. Show me the output. Follow the exact steps in https://docs.hermitbox.dev/v0.2.1/get-started/typescript.md.

Create a sandbox, run a command, and delete it from a Node or Bun backend with the TypeScript SDK.

1. Install `hermitbox` in your Node backend with its package manager.
2. Set `HERMITBOX_API_URL` and `HERMITBOX_API_KEY` in the backend's private environment.
3. Run this module with your application's normal TypeScript runner or build.

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

// Connect with HERMITBOX_API_URL and HERMITBOX_API_KEY from the environment.
const hb = new Hermitbox()

// Use the install's default tier. A container also needs an image.
const { defaults } = await hb.project()
if (!defaults) throw new Error('The install did not report its default tier')
const { isolation } = defaults
const box = await hb.sandbox(
  isolation === 'container' ? { isolation, image: 'alpine:3.22' } : { isolation },
)

try {
  // Run a command and stop if it failed.
  const result = await box.run('echo hello')
  if (result.code !== 0) throw new Error(result.stderr)
  console.log(result.stdout) // hello
} finally {
  // Delete the sandbox, even when something failed.
  await box.delete()
}
```

Keep this on the server: browser bundles 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.2.1/reference/tiers.md).
[SDK reference](https://docs.hermitbox.dev/v0.2.1/sdk/typescript.md).
