Use codex exec for non-interactive mode and --full-auto to auto-approve tool calls (safe inside E2B sandboxes). Pass --skip-git-repo-check to bypass git directory ownership checks inside the sandbox. Pass CODEX_API_KEY as an environment variable.
import { Sandbox } from 'e2b'const sandbox = await Sandbox.create('codex', { envs: { CODEX_API_KEY: process.env.CODEX_API_KEY },})const result = await sandbox.commands.run( `codex exec --full-auto --skip-git-repo-check "Create a hello world HTTP server in Go"`)console.log(result.stdout)await sandbox.kill()
Use --output-schema to constrain the agent’s final response to a JSON Schema. This ensures the output conforms to a specific structure — useful for building reliable pipelines.
Use --json to get a JSONL event stream. Each line is a JSON object representing an agent event (tool calls, file changes, messages). Progress goes to stderr; events go to stdout.
import { Sandbox } from 'e2b'const sandbox = await Sandbox.create('codex', { envs: { CODEX_API_KEY: process.env.CODEX_API_KEY },})const result = await sandbox.commands.run( `codex exec --full-auto --skip-git-repo-check --json -C /home/user/repo "Refactor the utils module into separate files"`, { onStdout: (data) => { for (const line of data.split('\n').filter(Boolean)) { const event = JSON.parse(line) console.log(`[${event.type}]`, event) } }, })await sandbox.kill()
If you need to customize the environment (e.g. pre-install dependencies, add config files), build your own template on top of the pre-built codex template.