Generate Code Using OpenAI Codex in a Koyeb Sandbox
Codex is OpenAI's cloud-based software engineering agent that can work on many tasks in parallel. It's designed to write features, answer questions about your codebase, fix bugs, and propose pull requests for review.
Running OpenAPI Codex in a sandboxed environment is ideal because Codex dynamically generates and executes code, which can include untrusted or unpredictable logic that must be isolated from your core systems for safety, security, and resource control. A sandbox provides a controlled environment with strict boundaries, preventing generated code from accessing sensitive files, affecting host infrastructure, or consuming unbounded compute. Koyeb Sandboxes are an ideal choice for running Codex-generated code because they offer fast, ephemeral, fully isolated execution environments with built-in GPU/CPU resources, secure network and filesystem isolation, and an API-driven workflow that lets you spin up, run, and tear down environments in seconds. This makes it easy to safely test, iterate, and orchestrate Codex workflows at scale without managing infrastructure or compromising security.
You will need:
- A Koyeb account - it's free to get started!
- An OpenAI account on a plan that enables Codex. See Codex website for details
Steps
Set up the environment
Create a new folder for your project and change to the directory:
mkdir example-codex
cd example-codex
In the example-codex folder, create a new virtual environment folder using the following command:
python -m venv venv
Activate and load your virtual environment:
. venv/bin/activate
Install the required packages:
pip install koyeb-sdk
Set your Koyeb access token in the environment. You can generate an access token in the Koyeb control panel settings.
export KOYEB_API_TOKEN=your-api-token
Create a Koyeb Sandbox with Codex
Create a file called main.py and add the following code, replacing the placeholder with your OpenAI API key:
from koyeb import Sandbox
# Create a new sandbox environment
sandbox = Sandbox.create(
image="ubuntu",
instance_type="small",
name="codex-sandbox",
wait_ready=True,
env={"OPENAI_API_KEY": "your-openai-api-key-here"},
)
# Set up Node.js and install OpenAI Codex CLI
setup_command = """
apt-get update && \
apt-get install -y nodejs npm && \
npm install -g @openai/codex
"""
result = sandbox.exec(setup_command, timeout=600, on_stdout=lambda data: print(data.strip()))
# View available OpenAI Codex CLI commands
result = sandbox.exec("codex --help", timeout=30, on_stdout=lambda data: print(data.strip()))
# Log in to OpenAI Codex CLI using the API key from environment variable
result = sandbox.exec('printenv OPENAI_API_KEY | codex login --with-api-key', on_stdout=lambda data: print(data.strip()))
# Use OpenAI Codex CLI to create a Python app and run it
result = sandbox.exec("codex exec --skip-git-repo-check --dangerously-bypass-approvals-and-sandbox 'Create a simple Python script that writes \"Hello, world!\" to a file /workspace/hello-world.txt and execute it without asking'", on_stdout=lambda data: print(data.strip()))
# Clean up by deleting the sandbox
sandbox.delete()
Run the code
Run your code using the following command:
python main.py
The output will list all of the available commands of the Codex CLI, and then generate the "Hello World" page.
Created `/workspace/write_hello.py:1` to write the greeting via `Path.write_text`, then ran it with `python3` to generate `/workspace/hello-world.txt` containing `Hello, world!`. You can rerun `python3 /workspace/write_hello.py` anytime to refresh the file if needed.
OpenAI Codex v0.63.0 (research preview)
--------
workdir: /
model: gpt-5.1-codex
provider: openai
approval: never
sandbox: danger-full-access
reasoning effort: none
reasoning summaries: auto
session id: 019ac4ab-c777-7773-ab07-1754a0299469
--------
user
Create a simple Python script that writes "Hello, world!" to a file /workspace/hello-world.txt and execute it without asking
mcp startup: no servers
exec
/bin/bash -lc "cat <<'EOF' > /workspace/write_hello.py
from pathlib import Path
Path('/workspace/hello-world.txt').write_text('Hello, world"'!'"\\n', encoding='utf-8')
EOF" in / exited 1 in 4ms:
/bin/bash: line 1: /workspace/write_hello.py: No such file or directory
exec
/bin/bash -lc ls in / succeeded in 3ms:
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
exec
/bin/bash -lc 'mkdir -p /workspace' in / succeeded in 3ms:
exec
/bin/bash -lc "cat <<'EOF' > /workspace/write_hello.py
from pathlib import Path
Path('/workspace/hello-world.txt').write_text('Hello, world"'!'"\\n', encoding='utf-8')
EOF" in / succeeded in 3ms:
exec
/bin/bash -lc 'python /workspace/write_hello.py' in / exited 127 in 2ms:
/bin/bash: line 1: python: command not found
exec
/bin/bash -lc 'python3 /workspace/write_hello.py' in / succeeded in 25ms:
exec
/bin/bash -lc 'cat /workspace/hello-world.txt' in / succeeded in 2ms:
Hello, world!
codex
Created `/workspace/write_hello.py:1` to write the greeting via `Path.write_text`, then ran it with `python3` to generate `/workspace/hello-world.txt` containing `Hello, world!`. You can rerun `python3 /workspace/write_hello.py` anytime to refresh the file if needed.
tokens used
8,492
That's it!
You've successfully used the Koyeb Python SDK to generate a sandbox environment to run OpenAI Codex code generation. Koyeb spun up a CPU Instance, added the OpenAI Codex CLI, and used the Codex CLI's commands to create a Python file, add code, run it, and read the result. The Koyeb Python SDK can be used to run commands, create and edit files, expose ports, add and update directories, upload and download files, and more, giving you full control over your sandbox environments.
Next steps
- Learn more about Koyeb Sandboxes
- Complete the Sandbox Quickstart to get familiar with the basics of the Koyeb Python SDK for sandboxes

