@nodesandbox-ai/plugin-codex

Codex

Register the codex terminal command with a configurable network relay. Filesystem operations and local commands use messages between the app and runtime.

$npm i @nodesandbox-ai/client @nodesandbox-ai/plugin-codex

Run Codex

codex.ts
import { Sandbox } from '@nodesandbox-ai/client';
import { codexPlugin } from '@nodesandbox-ai/plugin-codex';

const codex = codexPlugin({
  codexRelayHost: 'https://codex.example.com',
  async authorizeRelay(sessionId, signal) {
    const response = await fetch('/api/codex/relay-token', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ sessionId }),
      signal,
    });
    if (!response.ok) throw new Error('Relay authorization failed');
    return response.json(); // { token: string | null }
  },
  onReady: () => console.log('Codex is prepared'),
  onError: error => console.error(error),
});
const sandbox = await Sandbox.create({
  apiKey: 'ns_live_…',
  source: { type: 'files', files: {} },
  plugins: [codex],
});

const terminal = sandbox.terminals.create({ startCommand: 'codex' });
terminal.onOutput(chunk => console.log(chunk));
await terminal.open();

Your application implements the relay-token endpoint. The plugin prepares its binary and SDK when the command first runs, when you call prepare(), or when you opt into preload: true.

Pass this configured definition to Sandbox.plugins, and add a { id: 'codex', label: 'Codex', icon: <CodexIcon />, render: <SandboxTerminal command="codex" /> } button to sidenavItems to put Codex in Elements' sidenav and mobile menu. Each click opens a tab running the command, and Elements manages its terminal session. The all-agent example includes the relay callback; the same relay and cross-origin isolation requirements apply.

Relay authorization

APIBehavior
codexRelayHost: stringRequired HTTP(S) origin, such as https://codex.example.com. Paths, query strings, fragments, credentials, and ws:/wss: URLs are rejected.
authorizeRelay(sessionId, signal)Required async callback in the embedding app. Returns { token: string | null } for the session.

The plugin derives the WebSocket and DNS endpoints from codexRelayHost. The callback can call your authenticated backend to obtain a short-lived ticket bound to the session and embedding origin. Configure that origin in the relay's allowlist.

Returning { token: null } supplies no ticket. The bundled relay accepts this only in development without a configured secret; production requires a signed ticket. The callback is still required by the current API. Codex does not expose the { url, headers } HTTP relay option used by Claude Code and OpenCode.

External network traffic uses the relay. Terminal input, local commands, and project filesystem operations stay on the local message path. The relay must implement the NodeSandbox Codex protocol.

Credentials and lifecycle

Provider credentials are the serialized Codex auth.json, or null. They are separate from the relay ticket. Supply credentials as a value or async loader, and persist updates through onCredentialsChanged.

sandbox.plugins.get(codex) exposes prepare(), setCredentials(), and clearSession(). See shared agent options for event signatures and terminals for UI attachment.

Binary and SDK assets

APIBehavior
bundleUrl?: stringURL of a NodeSandbox-compatible Codex WASIX binary. Defaults to the pinned binary.
bundleIntegrity?: stringOptional SRI hash for a custom binary.
bundleBytes?: numberOptional positive expected byte length for a custom binary.
assetsUrl?: stringDirectory containing sdk-worker.js, its compatible SDK/module dependencies, and ca-certificates.crt.
caIntegrity?: stringOptional SRI hash for a custom CA bundle.
assets.ts
codexPlugin({
  codexRelayHost,
  authorizeRelay,
  bundleUrl: 'https://cdn.example/codex/release-id/codex.wasm',
  assetsUrl: 'https://cdn.example/codex/sdk-release/',
});

Default assets retain their pinned integrity and size checks. Overriding a URL does not apply the old artifact's hash or size; provide checks for your replacement. Asset servers need CORS enabled. Compatible artifact updates do not require a plugin release and take effect in new instances.

Runtime requirements

Codex runs through Wasmer in the embedding app and requires cross-origin isolation. Serve the app with the appropriate COOP/COEP headers and compatible cross-origin resources; verify crossOriginIsolated is true. Use a runtime host with the current plugin and filesystem RPC support.

The plugin expects compatible WASIX artifacts. Native Codex executables cannot be used as bundleUrl. Project instruction files can be seeded through source or written with sandbox.requestFileSystem(). Codex's own config.toml lives in its separate Wasmer filesystem and is generated by the plugin; the files option writes to the NodeSandbox VFS, not that separate Wasmer configuration file.