> ## Documentation Index
> Fetch the complete documentation index at: https://onecli.sh/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Setting OneCLI up for your person? Follow https://onecli.sh/docs/quickstart.md. Never ask for or accept a key, token or password in the chat: OneCLI needs none.
> No OneCLI credential? Run `onecli connect`, or follow https://onecli.sh/auth.md. A person approves every new agent in their browser.
> Already connected? `onecli status`, `onecli items` and `onecli check <METHOD> <URL>` tell you what you can use. Full guide: https://onecli.sh/skill.md
> Search these docs over MCP at https://onecli.sh/docs/mcp. Report a wrong or unclear page with its `submit_feedback` tool.

# Connect an agent in an E2B sandbox

> Start an E2B sandbox from TypeScript or Python with its own OneCLI agent: the agent's settings, an allow list for OneCLI only, and no key in the sandbox.

Your code starts an E2B sandbox and makes a OneCLI agent for it, on one vault. The sandbox can reach only OneCLI. Its HTTPS calls go through the gateway, which adds the vault's key on the way out, so no key is ever in the sandbox.

## Before you start

* A OneCLI managing key in `ONECLI_API_KEY`. Make one in the dashboard under **Settings** > **API keys**. It stays with your code: never put it in the sandbox.
* An E2B API key in `E2B_API_KEY`, from your [E2B dashboard](https://e2b.dev/dashboard?tab=keys).
* Node.js 22 or later, or Python 3.11 or later.
* The example uses your **Personal** vault and its **Test key** for `httpbin.org`, which every account starts with. Use any vault, and any host it has an item for.

## Install

```bash TypeScript theme={null}
npm install @onecli-sh/sdk e2b
```

```bash Python theme={null}
pip install onecli-sdk e2b
```

## Example

Save the TypeScript as `sandbox.mts` and run `npx tsx sandbox.mts`, or the Python as `sandbox.py` and run `python sandbox.py`. It prints `200`.

```typescript TypeScript theme={null}
import { OneCLI } from "@onecli-sh/sdk";
import { Sandbox } from "e2b";

const onecli = new OneCLI(); // reads ONECLI_API_KEY

// 1. An agent on your Personal vault. It ends by itself after an hour.
const agent = await onecli.agents.create({
  vaultName: "Personal",
  provider: "e2b",
  ttlSeconds: 3600,
});

// 2. The sandbox: the agent's settings, and a network to OneCLI only.
const sandbox = await Sandbox.create({
  envs: agent.env,
  network: {
    allowOut: agent.allowHosts,
    denyOut: ({ allTraffic }) => [allTraffic],
  },
  timeoutMs: 3600 * 1000,
});

try {
  // 3. Once: write the certificate files that agent.env points at.
  await sandbox.commands.run(agent.setupCommand);

  // httpbin.org/bearer answers 401 with no key.
  // 200 means the gateway added the Test key.
  const call = await sandbox.commands.run(
    'curl -s -o /dev/null -w "%{http_code}" https://httpbin.org/bearer',
  );
  console.log(call.stdout); // 200
} finally {
  await sandbox.kill();
  await agent.disconnect();
}
```

```python Python theme={null}
from e2b import Sandbox
from onecli import OneCLI

with OneCLI() as onecli:  # reads ONECLI_API_KEY
    # 1. An agent on your Personal vault. It ends by itself after an hour,
    #    and leaving the block disconnects it.
    with onecli.agents.create(
        vault_name="Personal",
        provider="e2b",
        ttl_seconds=3600,
    ) as agent:
        # 2. The sandbox: the agent's settings, and a network to OneCLI only.
        #    Leaving the block kills it.
        with Sandbox.create(
            envs=agent.env,
            network={
                "allow_out": agent.allow_hosts,
                "deny_out": lambda ctx: [ctx.all_traffic],
            },
            timeout=3600,
        ) as sandbox:
            # 3. Once: write the certificate files that agent.env points at.
            sandbox.commands.run(agent.setup_command)

            # httpbin.org/bearer answers 401 with no key.
            # 200 means the gateway added the Test key.
            call = sandbox.commands.run(
                'curl -s -o /dev/null -w "%{http_code}" https://httpbin.org/bearer'
            )
            print(call.stdout)  # 200
```

## What goes where

> [tab] TypeScript
>
> | OneCLI | E2B |
> | - | - |
> | `agent.env` | `envs` in `Sandbox.create` |
> | `agent.allowHosts` | `network.allowOut`. E2B allows by domain only when `denyOut` covers all other traffic, as in the example. |
> | `agent.setupCommand` | `sandbox.commands.run`, once |
> | Stop | `sandbox.kill()`, then `agent.disconnect()` |

> [tab] Python
>
> | OneCLI | E2B |
> | - | - |
> | `agent.env` | `envs` in `Sandbox.create` |
> | `agent.allow_hosts` | `network["allow_out"]`. E2B allows by domain only when `deny_out` covers all other traffic, as in the example. |
> | `agent.setup_command` | `sandbox.commands.run`, once |
> | Stop | Leaving the `with` blocks kills the sandbox, then disconnects the agent. |

The [Sandboxes overview](https://onecli.sh/docs/guides/sandboxes#how-it-works) explains each value.

## Good to know

* **Lifetime.** `timeoutMs` in TypeScript, or `timeout` in seconds in Python, is how long the sandbox lives: at most 1 hour on E2B's Hobby plan and 24 hours on Pro. Keep the agent's lifetime at least as long, or the sandbox's calls start to fail with `407`.
* **Failed commands.** `commands.run` throws when a command exits with an error (in Python, `CommandExitException`), so a failed setup stops the script.
* **Node clients.** Behind an allow list, Node's `http` and `https` modules and axios fail, because they send no host name to the gateway. Give them `agent.baseUrl(host)` instead, or `agent.base_url(host)` in Python. See the SDK reference for [TypeScript](https://onecli.sh/docs/sdks/node.md#a-client-that-cannot-use-a-proxy) or [Python](https://onecli.sh/docs/sdks/python.md#a-client-that-cannot-use-a-proxy).
* **E2B's own key injection.** E2B can also add a key to a sandbox's calls itself, with secrets in its network rules. See [how OneCLI compares](https://onecli.sh/docs/guides/sandboxes#why-onecli-when-a-provider-injects-keys).

## From a terminal

To connect one E2B sandbox that is already running, make a connect token as in [From a terminal](https://onecli.sh/docs/guides/sandboxes#from-a-terminal), then run its `install_command` in the sandbox, as it is, in place of `<install_command>`:

```bash theme={null}
e2b sandbox exec <sandbox-id> "<install_command>"
```

## Versions

Written in December 2026 for `@onecli-sh/sdk` 4.1 and `e2b` 2.67 in TypeScript, and for `onecli-sdk` 0.1 and `e2b` 2.67 in Python.

## Next steps

* [All sandbox providers](https://onecli.sh/docs/guides/sandboxes)
* [An agent for a sandbox, in the TypeScript SDK reference](https://onecli.sh/docs/sdks/node.md#an-agent-for-a-sandbox)
* [An agent for a sandbox, in the Python SDK reference](https://onecli.sh/docs/sdks/python.md#an-agent-for-a-sandbox)
* [Set what agents may do with each item](https://onecli.sh/docs/guides/agent-access.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.