Skip to main content

Guides · August 25, 2025 · Updated November 17, 2026 · 2 min read

Python Guide: Run OpenAI Codex in an E2B Sandbox

Tereza TizkovaGrowth
Python Guide: Run OpenAI Codex in an E2B Sandbox

OpenAI Codex turns natural-language instructions into code edits and files. Run it inside an E2B Sandbox and you get an isolated, long-running environment where the agent can safely create files, install tools, and execute without interactive prompts.

What we’ll build

  • Spin up an E2B sandbox from the prebuilt codex template.
  • Inject your OpenAI API key into the sandbox.
  • Run a Codex prompt that creates a Hello World file.
  • Print the output and shut the sandbox down.

See full example in the E2B Cookbook.

Components we’ll use

  • E2B Python SDK: creates and controls the sandbox.
  • Prebuilt template: codex – Codex CLI is preinstalled.
  • OpenAI API: used by Codex via CODEX_API_KEY, which holds your OpenAI API key.

Prerequisites

Outline

  1. Create the project
  2. Add the files (pyproject.toml, main.py)
  3. Set E2B_API_KEY and CODEX_API_KEY in .env
  4. Install with pip and run

1) Create the project

mkdir openai-codex-in-sandbox-python
cd openai-codex-in-sandbox-python
mkdir -p src/openai_codex_in_sandbox_python
touch src/openai_codex_in_sandbox_python/__init__.py

Create and commit .gitignore before .env so secrets never enter git history.

Target structure:

openai-codex-in-sandbox-python/
├─ src/
│  └─ openai_codex_in_sandbox_python/
│     ├─ __init__.py
│     └─ main.py
├─ pyproject.toml
├─ .gitignore
└─ .env

2) Files to add

File: pyproject.toml

[project]
name = "openai-codex-in-sandbox-python"
version = "0.1.0"
description = "Add your description here"
requires-python = ">=3.11"
dependencies = [
    "e2b",
    "python-dotenv",
]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

File: src/openai_codex_in_sandbox_python/main.py

This script loads .env (so the SDK sees E2B_API_KEY), creates the sandbox from the codex template while injecting your OpenAI key, runs a Codex exec prompt that generates index.html, prints the result, and then shuts the sandbox down.

Load configuration and SDK:

import os

from dotenv import load_dotenv
from e2b import Sandbox

load_dotenv()

Create the sandbox (OpenAI key read from .env and passed in as CODEX_API_KEY) and log its ID:

# Codex reads an OpenAI API key from CODEX_API_KEY.
api_key = os.getenv("CODEX_API_KEY")
if not api_key:
    raise SystemExit("Set CODEX_API_KEY (an OpenAI API key) in .env")

template_name = 'codex'
sbx = Sandbox.create(
    template_name,
    envs={"CODEX_API_KEY": api_key},
    timeout=60 * 5,  # 5 minutes
)
print("Sandbox created", sbx.sandbox_id)

Run a Codex prompt that creates a file (no per-command timeout), and shut the sandbox down even if the run fails:

try:
    # Print help for Codex
    # result = sbx.commands.run('codex --help', request_timeout=0, timeout=0)
    # print(result.stdout)

    # Run a prompt with Codex
    result = sbx.commands.run(
        "codex exec --skip-git-repo-check --dangerously-bypass-approvals-and-sandbox 'Create a hello world index.html'",
        timeout=0,
    )
    print(result.stdout)
finally:
    sbx.kill()

File: .gitignore

.venv/
.env
__pycache__/
*.pyc

File: .env

E2B_API_KEY="YOUR_E2B_API_KEY"
CODEX_API_KEY="YOUR_OPENAI_API_KEY"

3) Set up and install

python -m venv .venv
# macOS / Linux
source .venv/bin/activate
# Windows (PowerShell)
# .venv\Scripts\Activate.ps1

pip install -e .

4) Run the example

python src/openai_codex_in_sandbox_python/main.py

Expected: console output from Codex. The generated index.html lives inside the sandbox filesystem.

See full example in the E2B Cookbook.

Ask AI

Newsletter