# Sandbox server API

## Public imports

| Import | Use |
| --- | --- |
| `runSandboxTask` from `@vite-hub/sandbox/task` | Run a task by name. Returns a native `Response`. |
| `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/reference/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 type from the default function and returns a native `Response`. A function with no arguments accepts an `unknown` payload.

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

export default defineEventHandler(async () => {
  const response = await runSandboxTask('release-notes', { notes: 'ship it' }, {
    context: { requestId: 'release-notes-42' },
  })
  if (!response.ok)
    throw new Error(await response.text())
  return await response.json()
})
```

| Option | Type | Description |
| --- | --- | --- |
| `context` | `Record<string, unknown>` | Second argument of the entry point. |
| `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`.

Failures, including timeouts and cleanup failures, return a non-2xx JSON `Response`. A timed-out attempt has the `SANDBOX_TASK_TIMEOUT` code.

<!-- 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)
