# Sandbox server API

## Public imports

| Import | Use |
| --- | --- |
| `runSandboxTask` from `vite-hub/sandbox/task` | Run a task by name. Returns `[error, data]`. |
| `defineSandboxTask` from `vite-hub/sandbox/task` | Declare a free-form `<path>.sandbox-task.ts` task. |
| `resolveSandboxTaskRunner` from `vite-hub/sandbox/task` | Get a runner and read its `executionAuthority` before execution. |
| `readRequestPayload`, `readValidatedPayload` from `vite-hub/sandbox/task` | Read the JSON body of a request event with [Standard Context](https://vitehub.dev/docs/runtime/context#standard-context) properties, then validate it with a Standard Schema or a function. |
| `hubSandboxTask` from `@vite-hub/sandbox/task/vite` | Add discovery, types, package setup, and Provider Output (generated host files). |

With the `vite-hub` package, import the same runtime APIs from `vite-hub/sandbox/task`.

## Run a Sandbox Task

`runSandboxTask(name, payload, options?)` takes its payload and result types from the default function and returns `[error, data]`. `data` is the decoded result. Objects with `toJSON()` use the serialized shape: a `Date` becomes an ISO string, or `null` if invalid. A native `Response` is reconstructed as a `Response`. The payload is required when the registered handler requires it. Handlers with an optional payload or no parameters can be invoked without a payload. A zero-argument function accepts an `unknown` payload.

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

export default defineEventHandler(async () => {
  const [error, notes] = await runSandboxTask('release-notes', { notes: 'ship it' }, {
    context: { requestId: 'release-notes-42' },
  })
  if (error)
    return toResponse(error)
  return notes
})
```

| Option | Type | Description |
| --- | --- | --- |
| `context` | `Record<string, unknown>` | Second argument of the entry point. Defaults to `{}` when omitted. |
| `signal` | `AbortSignal` | Cancel the run. Keeps your abort reason and stops provider work that can stop. |
| `sandboxId` | `string` | Cloudflare only. Reuse a named Sandbox instead of a new one per run. |

Payloads, context, and results are JSON. Nested `Blob` and `Uint8Array` values travel as files in the Sandbox, so you do not convert them to base64. Node.js `Buffer` values stay `Buffer`. Each run has limits on the size and depth of these values, see [Transfer limits](https://vitehub.dev/docs/sandbox/configure#transfer-limits).

Failures, including timeouts and cleanup failures, return [`SANDBOX_TASK_RUN_FAILED`](https://vitehub.dev/docs/sandbox/diagnostics/sandbox_task_run_failed). `error.data.code` names the failure, such as `SANDBOX_TASK_TIMEOUT`, and `error.cause` has its message and details. `toResponse(error)` answers with the status of the failure, such as 504 for a timeout, and leaves out the stack and process output. See [Error responses](https://vitehub.dev/docs/sandbox/limits-and-errors#error-responses).

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

- [Overview](https://vitehub.dev/raw/docs/sandbox.md)
- [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) (this page)
- [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: [Agent capability](https://vitehub.dev/raw/docs/sandbox/agent-capability.md)

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