E2B
On this page
For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending
.mdto the page URL.
See the application-managed and webhook-managed examples in the OpenAI Cookbook.
Choose a provisioning mode:
- Application-managed: Your application creates and connects the E2B sandbox directly.
- Webhook-managed: Deploy a handler that starts or reconnects sandboxes from OpenAI webhooks.
See Sandbox lifecycle to compare the two modes.
Before you begin
Set E2B_API_KEY and use OPENAI_API_KEY for application requests. Set OPENAI_EXECUTOR_API_KEY to an environment key, and pass only that key into the sandbox as CODEX_API_KEY.
Webhook-managed
Implement a controller that verifies OpenAI webhooks and provisions a separate E2B worker for each session. Follow Deploy and connect a handler for credentials, endpoint registration, and signature verification.
Persist the session-to-sandbox mapping. On a connection request, resume a paused worker or replace a deleted one. Pausing preserves its files; replacement does not. Set running timeouts for the controller and workers, and remove the OpenAI webhook when you stop using the controller.
Application-managed
Use the E2B SDK or API from your application to manage the sandbox:
- Create a self-hosted session and save its environment ID.
- Create an isolated E2B sandbox with the session's working directory and install the Codex CLI inside it.
- Start the executor in the sandbox using the environment ID and environment key.
- Use Run and continue sessions to send input and check the turn's outcome.
- Delete the session and stop the E2B sandbox when finished.
Configure the sandbox lifetime separately from the timeout for the executor command. A command with no timeout does not keep an expired sandbox running.
References
- Read E2B documentation
- Read E2B Python SDK
- Read E2B TypeScript SDK