Headless bindings
React
Headless React bindings that own one runtime, start its dev server, and expose files, commands, logs, terminals, and preview state through context.
npm i @nodesandbox-ai/react @nodesandbox-ai/client reactRender a preview
SandboxPreview is the shortest path from source files to a hosted preview iframe. It creates the runtime, installs dependencies, starts the dev script, and renders the listener.
import { SandboxPreview } from '@nodesandbox-ai/react';
export function Preview() {
return (
<SandboxPreview
apiKey={import.meta.env.VITE_NODESANDBOX_API_KEY}
source={{
type: 'files',
files: {
'package.json': JSON.stringify({
scripts: { dev: 'vite --host 0.0.0.0' },
dependencies: { vite: '8.1.3' },
}),
'index.html': '<h1>Hello from NodeSandbox</h1>',
},
}}
iframeProps={{
title: 'Project preview',
style: { width: '100%', height: 600, border: 0 },
}}
/>
);
}Compose with a provider
Lift SandboxProvider when multiple parts of your interface need the same runtime. Children can read the runtime with useSandbox(); the iframe can live anywhere below it.
import {
SandboxProvider,
SandboxIFrame,
useSandbox,
} from '@nodesandbox-ai/react';
function Status() {
const { status, error, port } = useSandbox();
if (status === 'error') return <p>{error?.message}</p>;
if (status === 'booting') return <p>Installing dependencies…</p>;
if (status === 'starting') return <p>Starting dev server…</p>;
if (status === 'running') return <p>Listening on {port}</p>;
return null;
}
export function Workspace({ apiKey, source }) {
return (
<SandboxProvider apiKey={apiKey} source={source}>
<Status />
<SandboxIFrame iframeProps={{ title: 'Preview' }} />
</SandboxProvider>
);
}Runtime state
sandbox, commands, and terminals become available at shell readiness, while status can still be booting. Basic commands can run as soon as the handle exists; gate project-dependent work on status leaving booting.
statusbooting (connecting, starting the runtime, getting dependencies), starting (dev server starting), running, stopped (after stopServer()), idle (nothing to serve: no package.json and no start command), or error (the sandbox failed and was disposed; restart() creates a new one).bootProgressWhile booting: phase (resolving, restoring, installing, complete), completed, total (null when it cannot count), and message. Null before the runtime reports and outside booting.errorThe last failure. With status error, the sandbox is gone; with stopped, starting the server again failed and startServer() can retry.portThe dev server's port, when status is running.sandboxThe early sandbox handle, including its typed plugin instances.terminalsCreate and control interactive terminals after shell readiness.fsRead and edit the mirrored virtual filesystem.commandsRun finite or background commands.installOutputStreamed ANSI output from dependency installation.serverOutputRaw output from the running dev process.previewUrlPer-sandbox URL for the active listener.restart()Clear the npm cache and recreate the runtime.stopServer()Stop the dev server: its process and workers exit, freeing their memory, status is stopped, and previewUrl is null. Files, node_modules and terminals stay.startServer()Start the stopped dev server again with the same command, without reinstalling.Custom lifecycle commands
The provider defaults to npm install followed by npm run dev. Override either command tuple for another package manager, script, or entry point.
A bare package-manager install is skipped when boot already installed the project dependencies. Custom install arguments run as an additional step.
<SandboxProvider
apiKey={apiKey}
source={source}
installCommand={['pnpm', ['install']]}
startCommand={['pnpm', ['run', 'preview']]}
port={4173}
>
<App />
</SandboxProvider>Plugins
Pass compute plugins through plugins on SandboxProvider, SandboxPreview, or useCreateSandbox(). Use the sandbox returned by useSandbox() to retrieve a typed plugin instance.
'use client';
import { useMemo } from 'react';
import { SandboxProvider, SandboxIFrame, useSandbox } from '@nodesandbox-ai/react';
import { opencodePlugin } from '@nodesandbox-ai/plugin-opencode';
function PrepareAgent({ definition }) {
const { sandbox } = useSandbox();
return (
<button disabled={!sandbox} onClick={() => {
void sandbox?.plugins.get(definition).prepare().catch(console.error);
}}>
Prepare OpenCode
</button>
);
}
export function Workspace({ apiKey, source }) {
const opencode = useMemo(() => opencodePlugin({ preload: true }), []);
return (
<SandboxProvider apiKey={apiKey} source={source} plugins={[opencode]}>
<PrepareAgent definition={opencode} />
<SandboxIFrame iframeProps={{ title: 'Preview' }} />
</SandboxProvider>
);
}Preparation may run while dependencies install and does not launch the CLI. Open an agent terminal when your workflow is ready; wait for status to leave booting if it needs project dependencies. Keep definitions stable across renders. Use the provider's deps array when a changed source or plugin selection should recreate the sandbox.
onReady on the React provider is the sandbox's early readiness callback. Agent callbacks such as onCredentialsChanged belong to the plugin definition.
For an agent sidenav, agent tabs that each keep their own session, and mobile agent/preview tabs, use Elements with the same plugin definitions. Pass sidenavItems with a button per agent, render: <SandboxTerminal command="claude" />, to Sandbox, or to SandboxShell beneath a shared provider.