---
title: "OpenAI Codex integration"
description: "Run OpenAI's Codex CLI headless in an E2B sandbox and collect the diff when it's done. Starts from the prebuilt codex template."
url: "https://e2b.dev/integrations/codex"
---

# 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.

Coding agents · [All integrations](https://e2b.dev/integrations.md)

## About this integration

- **Maintained by:** E2B
- **How it works:** Prebuilt codex sandbox template
- **Package:** e2b
- **Language:** TypeScript, Python
- **Upstream:** [openai/codex · ★ 136k](https://github.com/openai/codex)
- **Example:** [e2b-cookbook/openai-codex-in-sandbox-js](https://github.com/e2b-dev/e2b-cookbook/tree/main/examples/openai-codex-in-sandbox-js)

## Get started

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

### Paste this into your coding agent

```text
Run OpenAI Codex in an E2B sandbox from this project.

1. Install the e2b SDK using the project's package manager.
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. 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. Run one task and show me the output that came from the sandbox.
```

## Or set it up yourself

### 1. Install the E2B SDK

```bash
npm init -y && npm pkg set type=module
npm install e2b
npm install -D tsx
```

### 2. Set your API keys

```bash
export E2B_API_KEY="e2b_..."
export CODEX_API_KEY="..."
```

### 3. Run Codex headless

```typescript
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. Work on a repository

```typescript
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)
```

## What runs in the sandbox

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

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

## More coding agents

Other coding agents that run in E2B sandboxes.

- [Claude Code](https://e2b.dev/integrations/claude-code.md): Run Claude Code in its own sandbox with a real filesystem, terminal, and git, so it can work unattended.
- [OpenCode](https://e2b.dev/integrations/opencode.md): Run OpenCode in a sandbox, or keep it local and point its workspaces at E2B with the @e2b/opencode plugin.
- [GitHub Copilot CLI](https://e2b.dev/integrations/copilot-cli.md): Run GitHub Copilot CLI in a sandbox with full filesystem, terminal, and git access.

## Links

- [Get API key](https://console.e2b.dev/?tab=keys)
- [Read the docs](https://docs.e2b.dev/agents/codex)
- [E2B documentation](https://docs.e2b.dev)
