Skip to main content

Coding agents

OpenAI Codex

Run OpenAI's Codex CLI headless in a sandbox with its own filesystem, terminal and git. Point it at a repo, let it work, and collect the diff.

Maintained by
E2B
How it works
Prebuilt codex sandbox template
Package
e2b
Language
TypeScript, Python

Set it up

Get started

Paste the prompt into your coding agent, or follow the steps yourself.

Paste this into your coding agent

Run OpenAI Codex in an E2B sandbox from this project.

  1. 1.Install the e2b SDK using the project's package manager.
  2. 2.The SDK needs the E2B_API_KEY environment variable, and Codex needs CODEX_API_KEY (an OpenAI API key). If E2B_API_KEY isn't set, ask me for my API key from https://console.e2b.dev/?tab=keys. Never hardcode either key.
  3. 3.Follow the Codex guide at https://docs.e2b.dev/agents/codex: create a sandbox from the codex template, pass CODEX_API_KEY in envs, and run codex exec with --dangerously-bypass-approvals-and-sandbox --skip-git-repo-check and a command timeout of 0. Kill the sandbox in a finally block.
  4. 4.Run one task and show me the output that came from the sandbox.

Or set it up yourself

  1. 1. Install the E2B SDK

    npm init -y && npm pkg set type=module
    npm install e2b
    npm install -D tsx
  2. 2. Set your API keys

    export E2B_API_KEY="e2b_..."
    export CODEX_API_KEY="..."
  3. 3. Run Codex headless

    import { Sandbox } from 'e2b'
    
    const apiKey = process.env.CODEX_API_KEY
    if (!apiKey) throw new Error('Set CODEX_API_KEY to your OpenAI API key')
    
    const sandbox = await Sandbox.create('codex', {
      envs: { CODEX_API_KEY: apiKey },
    })
    
    try {
      const result = await sandbox.commands.run(
        `codex exec --dangerously-bypass-approvals-and-sandbox --skip-git-repo-check "Create a hello world HTTP server in Go"`,
        { timeoutMs: 0 }
      )
      console.log(result.stdout)
    } finally {
      await sandbox.kill()
    }
  4. 4. Work on a repository

    await sandbox.commands.run(
      'git clone --depth 1 https://github.com/e2b-dev/claude-code-fastapi.git /home/user/repo'
    )
    
    await sandbox.commands.run(
      `codex exec --dangerously-bypass-approvals-and-sandbox --skip-git-repo-check -C /home/user/repo "Add error handling to all API endpoints"`,
      { timeoutMs: 0 }
    )
    
    const diff = await sandbox.commands.run('git -C /home/user/repo diff')
    console.log(diff.stdout)

Security

What runs in the sandbox

Codex and every command it runs live in the sandbox, so its own command sandbox can stay off.

  • Agentcodex exec with approvals and Codex's own sandbox bypassed. E2B is the isolation boundary instead.
  • FilesThe repo you clone and every change Codex makes. Read results back with files.read() or git diff.
  • SessionsRollout files live under ~/.codex/sessions. codex exec resume continues a thread with a follow-up task.
  • NetworkInternet access is on by default. Restrict outbound traffic with network rules for untrusted input or secrets.

3B+ sandboxes started

Give every agent a machine