Examples and guides for using the OpenAI API
DigitalOcean webhook-managed sandbox
The endpoint verifies the signature and provisions a DigitalOcean Managed Agents
Runtime Service (MARS) sandbox for the session that needs one. MARS boots a
Firecracker microVM from a published image that already contains the Codex CLI
and starts the executor itself, so this handler never runs codex exec-server
and never manages a container.
The sandbox is named after the OpenAI session ID, and the controller checks all matching pages before provisioning. A process-local lock serializes webhook reconciliation. Run one controller worker and one replica; multiple replicas or overlapping deployments need distributed coordination or provider-side idempotency. A restart reuses sandboxes visible in DigitalOcean's listing, but an ambiguous create timeout still needs reconciliation before retrying. Idle sandboxes are paused automatically and resumed on reconnect.
Deploy
Use Python 3.11+ and a sandbox-enabled DigitalOcean account with access to
codex-agentapi. DigitalOcean Managed Agents
is in public preview. Create the agent
using the shared setup with this provider's
webhook_managed/client.py.
Use PyDo 0.41.0 or later with async support. The script declares this dependency inline. To install it separately:
pip install "pydo[aio]>=0.41.0"
App Platform deploys from a Git repository. Push a copy of these examples
to your own repository, preserving the examples/agents_api/ paths. Set
github.repo in app.yaml to it, then create the app. The
Dockerfile packages the handler and shared environment.yaml,
builds from the repository root, and installs dependencies
at build time; credentials are available only at runtime.
This recipe uses OpenAI Python SDK 3.13.0 or later with client.beta.agents.
Do not add API keys or Git tokens to the Dockerfile or build context.
Install doctl 1.170.0 or later
and authenticate with doctl auth init to run the deployment and cleanup
commands below. The CLI is not required to run the Python controller locally.
doctl apps create --spec examples/agents_api/sandboxes/digitalocean/webhook_managed/app.yaml
Configure these variables on the service:
OPENAI_API_KEY: controller/session-read credential.OPENAI_AGENT_ID: agent ID from the shared setup.OPENAI_EXECUTOR_API_KEY: restricted executor credential, scoped toapi.agents.environments.connect.OPENAI_WEBHOOK_SECRET: generated by OpenAI when registering the endpoint.DIGITALOCEAN_TOKEN: API token that can manage MARS sessions.
For the first deployment only, use pending-webhook-registration as the signing
value; the receiver rejects requests until you configure the signing secret.
doctl apps list prints the deployed URL. The webhook URL is
https://YOUR_APP.ondigitalocean.app/webhook, and OpenAI must be able to reach
it without a login. Register the webhook, enable agent.session.action_required
and agent.session.failed, replace the signing value with the real secret, and
redeploy. Then follow run and reconnect:
uv run examples/agents_api/sandboxes/digitalocean/webhook_managed/client.py --agent-id "$OPENAI_AGENT_ID"
uv run examples/agents_api/sandboxes/digitalocean/webhook_managed/client.py --session-id "$SESSION_ID" --input "Run another shell command."
Environment-variable changes require a new deployment.
PyDo 0.41.0 can raise StreamConsumedError after a successful empty resume
response. If it does, the controller checks for SESSION_STATUS_READY within
ten seconds before acknowledging the
webhook. An unconfirmed resume or any other SDK error still fails the delivery.
Run the controller locally
uv run examples/agents_api/sandboxes/digitalocean/webhook_managed/handler.py
Expose port 8080 with a tunnel and register that URL. On macOS python.org
builds, requests to api.digitalocean.com can fail with
CERTIFICATE_VERIFY_FAILED; run Install Certificates.command, or export
SSL_CERT_FILE="$(python -c 'import certifi; print(certifi.where())')".
Configure the sandbox
The shared environment.yaml is the unit of configuration. Two fields matter most:
agent: codex-agentapiselects the image that runs the executor.configis passed through to OpenAI when the session is created, and is required.
Credentials belong under secrets, not env; the create API rejects a
credential-shaped env value. egress accepts a host allowlist as shown, or
unrestricted to leave destinations open. To size the sandbox or pin a region,
add the corresponding manifest fields — see doctl harness-runtime sizes.
Stop or clean up
The sandbox name is mars- followed by the API session ID.
doctl harness-runtime remove mars-SESSION_ID
Sandboxes persist files between turns; a destroyed sandbox starts fresh. There is no session-deletion webhook, so deleting the API session does not release the sandbox — delete both. Remove the OpenAI webhook before deleting the App Platform app.