# Sandbox

Run code in an isolated cloud machine on Vercel or Cloudflare, and get the result in your route.

Hosts: Vercel, Cloudflare.

:hero-figure{name="sandbox"}

```ts [server/sandbox-tasks/pixels/index.ts]
type ImageSize = { width: number, height: number }

// This runs in its own isolated machine.
// It is a normal function, with no special imports.
export default async function count(
  { width, height }: ImageSize,
) {
  return { pixels: width * height }
}
```

```json [server/sandbox-tasks/pixels/package.json]
{
  "name": "pixels",
  "private": true,
  "type": "module",
  "exports": "./index.ts",
  "vitehub": {
    "sandboxTask": { "timeout": 30000 }
  }
}
```

```ts [server/api/pixels.post.ts]
import { defineEventHandler, readBody } from 'h3'
import { toResponse } from 'vite-hub/runtime'
import { runSandboxTask } from 'vite-hub/sandbox/task'

type ImageSize = { width: number, height: number }

// Run the pixels package and return its result
export default defineEventHandler(async (event) => {
  const input = await readBody<ImageSize>(event)
  const [error, pixels] = await runSandboxTask('pixels', input)

  // A failed or timed-out run answers with an error status
  if (error)
    return toResponse(error)
  return pixels
})
```

:::framework{name="Nuxt"}
```ts [nuxt.config.ts]
// The Vercel host uses Vercel Sandbox.
// Use host 'cloudflare' for Cloudflare Sandbox.

export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    host: 'vercel',
    // Each run starts a Vercel Sandbox
    sandboxTask: true,
  },
})
```
:::
:::framework{name="Nitro"}
```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { nitro } from 'nitro/vite'
import { vitehub } from 'vite-hub'

// The Vercel host uses Vercel Sandbox.
// Use host 'cloudflare' for Cloudflare Sandbox.
export default defineConfig({
  plugins: [
    vitehub({
      host: 'vercel',
      // Each run starts a Vercel Sandbox
      sandboxTask: true,
    }),
    nitro(),
  ],
})
```
:::
:::framework{name="Vite"}
```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [
    vitehub({
      host: 'vercel',
      // Each run starts a Vercel Sandbox
      sandboxTask: true,
    }),
  ],
})
```
:::

### [A folder is a Sandbox Task](https://vitehub.dev/docs/sandbox/configure#package-projects)

Put a `package.json` and `index.ts` in a folder under `server/sandbox-tasks`. The folder name is the task name.

### [Plain code and npm packages](https://vitehub.dev/docs/sandbox/configure#package-entry-point)

The entry point is a standard async function. Its dependencies install in the Sandbox, not in your app.

### [Call it from a route](https://vitehub.dev/docs/sandbox/server-api#run-a-sandbox-task)

`runSandboxTask()` sends the input and returns `[error, data]`. Your function sets the input and result types.

### [Time limits and clear errors](https://vitehub.dev/docs/sandbox/limits-and-errors#limits)

Set a timeout in `package.json`. Failed or timed-out runs return an error status.

### [Vercel or Cloudflare](https://vitehub.dev/docs/sandbox/hosts#drivers)

Choose the Driver in your config. The task code stays the same.

### [Know what the code can reach](https://vitehub.dev/docs/sandbox/limits-and-errors#production-checks)

Task code can use the network and files the provider allows. Limit them in provider settings.

Use a Sandbox Task to run a named project inside an isolated Cloudflare or Vercel Sandbox. A Sandbox Task run is an execution boundary, not a general-purpose local shell.

Keep images, mounts, network access, timeouts, and resource bounds explicit. Pass only the files and secrets the run needs.

Prove successful execution, cancellation, timeout, and cleanup on the selected provider. Verify result-size limits only when the provider documents them. Inspect generated bindings and provider output.

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

- [Overview](https://vitehub.dev/raw/docs/sandbox.md) (this page)
- [Get started](https://vitehub.dev/raw/docs/sandbox/get-started.md)
- [Configure](https://vitehub.dev/raw/docs/sandbox/configure.md)
- [Server API](https://vitehub.dev/raw/docs/sandbox/server-api.md)
- [Agent capability](https://vitehub.dev/raw/docs/sandbox/agent-capability.md)
- [Hosts](https://vitehub.dev/raw/docs/sandbox/hosts.md)
- [Limits and errors](https://vitehub.dev/raw/docs/sandbox/limits-and-errors.md)
- [Concepts](https://vitehub.dev/raw/docs/sandbox/concepts.md)

Next: [SANDBOX_R0020](https://vitehub.dev/raw/docs/sandbox/diagnostics/sandbox_r0020.md)

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