# Run your first Sandbox

In this tutorial you open a Sandbox on your own Node host, write a file, and run Node to read it back. A Sandbox is a session for running commands, with a private Home, a set environment, and boot checks.

You need Node.js 22.22 or newer, pnpm, and a Vite server app. Run the commands from its root.

> [!WARNING]
> A trusted-host Sandbox shares your host's files and process rights. For untrusted code, use [Sandbox](https://vitehub.dev/docs/sandbox) with provider isolation.

::tutorial-step{title="Install the Sandbox package"}
## Install the Sandbox package

Install Sandbox and Nitro:

```bash [commands/install]
pnpm add @vite-hub/sandbox nitro h3
```

The package gives you `resolveSandbox()` and the portable session types. It does
not install a provider SDK. Add Nitro to your Vite config so the Vite dev server
also serves `server/api` routes:

```ts [vite.config.ts]
import { nitro } from 'nitro/vite'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [nitro()],
})
```
::

::tutorial-step{title="Open a session"}
## Open a session

Create a route that opens a Sandbox, writes one file, and runs Node to read it.
`requires: ['node']` checks that Node exists before the session opens.

```ts [server/api/sandbox-check.get.ts]
import { defineEventHandler } from "h3";
import { resolveSandbox } from "@vite-hub/sandbox";

export default defineEventHandler(async () => {
  const sandbox = await resolveSandbox(
    {
      runtime: "trusted-host",
      cwd: process.cwd(),
      requires: ["node"],
    },
    {},
  );

  const session = await sandbox.open();
  try {
    await session.files.write(
      "workspace/.vitehub/sandbox-check.txt",
      new TextEncoder().encode("Sandbox is ready\n"),
    );
    const result = await session.exec(
      "node",
      ["-e", "process.stdout.write(require('fs').readFileSync('.vitehub/sandbox-check.txt', 'utf8'))"],
      { cwd: session.cwd },
    );

    if (!result.ok) throw new Error(result.stderr);
    return { runtime: sandbox.plan.runtime, output: result.stdout };
  } finally {
    await session.close();
  }
});
```

Keep `close()` in `finally`. The session then frees its processes and temporary
state also when the command fails.
::

::tutorial-step{title="Run and check the result"}
## Run and check the result

Start the server:

```bash [commands/start]
pnpm vite dev
```

In another terminal, call the route:

```bash [commands/request]
curl http://localhost:5173/api/sandbox-check
```

The response contains:

```json [output/response.json]
{
  "runtime": "trusted-host",
  "output": "Sandbox is ready\n"
}
```

Read [Sandboxes](https://vitehub.dev/docs/agents/sandboxes) for checkouts, private Home files, durable
state, toolchains, and remote runtimes. A Sandbox runs commands. A
[Workspace](https://vitehub.dev/docs/agents/workspace-context) holds the files an Agent sees.
::

<!-- vitehub-docs:section-links -->
## In this section

- [Start](https://vitehub.dev/raw/docs/agents.md)
- [Define and run](https://vitehub.dev/raw/docs/agents/agent-definitions.md)
- [Instructions and tools](https://vitehub.dev/raw/docs/agents/instructions.md)
- [History and context](https://vitehub.dev/raw/docs/agents/chat-history-sessions.md)
- [Channels](https://vitehub.dev/raw/docs/agents/channels.md)
- [Inspect and evaluate](https://vitehub.dev/raw/docs/agents/observability.md)
- Advanced
  - [Agent presets and inheritance](https://vitehub.dev/raw/docs/agents/agent-presets.md)
  - [Invocation storage](https://vitehub.dev/raw/docs/agents/invocation-storage.md)
  - [Runtime policy, approvals, and traces](https://vitehub.dev/raw/docs/agents/runtime-policy.md)
  - [Custom capabilities](https://vitehub.dev/raw/docs/agents/capabilities/custom.md)
  - [Child invocations](https://vitehub.dev/raw/docs/agents/controlled-child-invocations.md)
  - [Sandboxes](https://vitehub.dev/raw/docs/agents/sandboxes.md)
  - [Sandbox tutorial](https://vitehub.dev/raw/docs/agents/sandbox-tutorial.md) (this page)
  - [Process hosts](https://vitehub.dev/raw/docs/agents/process-hosts.md)
- [Recipes](https://vitehub.dev/raw/docs/agents/support-agent.md)
- [Presets](https://vitehub.dev/raw/docs/agents/presets.md)
- [Capabilities](https://vitehub.dev/raw/docs/agents/capabilities/access.md)

Next: [AGENT_R0077](https://vitehub.dev/raw/docs/agents/diagnostics/agent_r0077.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
