> ## 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 a Vercel Sandbox

> Start a Vercel Sandbox from TypeScript or Python with its own OneCLI agent: the agent's settings, a network policy for OneCLI, and no key in the sandbox.

Your code starts a Vercel Sandbox 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.
* Vercel credentials: `VERCEL_OIDC_TOKEN` (run `vercel env pull` in a linked project), or an access token. In TypeScript, pass the token to `Sandbox.create` as `token`, with `teamId` and `projectId`. In Python, set `VERCEL_TOKEN`, `VERCEL_TEAM_ID`, and `VERCEL_PROJECT_ID`.
* 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 @vercel/sandbox
```

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

## 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 "@vercel/sandbox";

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: "vercel",
  ttlSeconds: 3600,
});

// 2. The sandbox: the agent's settings, and a network to OneCLI only.
//    It reads VERCEL_OIDC_TOKEN, or pass token, teamId and projectId.
const sandbox = await Sandbox.create({
  env: agent.env,
  networkPolicy: { allow: agent.allowHosts },
  timeout: 45 * 60 * 1000, // the most on the Hobby plan
});

try {
  // 3. Once: write the certificate files that agent.env points at.
  const setup = await sandbox.runCommand("sh", ["-c", agent.setupCommand]);
  if (setup.exitCode !== 0)
    throw new Error(`setup failed: ${await setup.stderr()}`);

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

```python Python theme={null}
from onecli import OneCLI
from vercel.sandbox import NetworkPolicy
from vercel.sandbox.sync import create_sandbox

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="vercel",
        ttl_seconds=3600,
    ) as agent:
        # 2. The sandbox: the agent's settings, and a network to OneCLI only.
        #    It reads VERCEL_OIDC_TOKEN, or VERCEL_TOKEN, VERCEL_TEAM_ID and
        #    VERCEL_PROJECT_ID. Leaving the block stops and deletes it.
        with create_sandbox(
            env=agent.env,
            network_policy=NetworkPolicy.custom(
                allow={host: () for host in agent.allow_hosts}
            ),
            execution_time_limit=45 * 60,  # the most on the Hobby plan
        ) as sandbox:
            # 3. Once: write the certificate files that agent.env points at.
            #    check=True raises when it fails.
            sandbox.run_command(
                "sh", ["-c", agent.setup_command], capture_output=True, check=True
            )

            # httpbin.org/bearer answers 401 with no key.
            # 200 means the gateway added the Test key.
            sandbox.run_command(
                "curl",
                [
                    "-s",
                    "-o",
                    "/dev/null",
                    "-w",
                    "%{http_code}\n",
                    "https://httpbin.org/bearer",
                ],
            )  # prints 200
```

## What goes where

> [tab] TypeScript
>
> | OneCLI | Vercel Sandbox |
> | - | - |
> | `agent.env` | `env` in `Sandbox.create` |
> | `agent.allowHosts` | `networkPolicy: { allow }` |
> | `agent.setupCommand` | `sandbox.runCommand("sh", ["-c", ...])`, once. Check `exitCode`: it does not throw. |
> | Stop | `sandbox.stop()`, then `agent.disconnect()` |

> [tab] Python
>
> | OneCLI | Vercel Sandbox |
> | - | - |
> | `agent.env` | `env` in `create_sandbox` |
> | `agent.allow_hosts` | `network_policy=NetworkPolicy.custom(allow=...)`, with `()` for each host: no rules |
> | `agent.setup_command` | `sandbox.run_command("sh", ["-c", ...])`, once. With `check=True` it raises an error when the command fails. |
> | Stop | Leaving the `with` blocks stops and deletes 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.** `timeout` in TypeScript, or `execution_time_limit` in seconds in Python, is how long the sandbox runs: at most 45 minutes on Vercel's Hobby plan and 24 hours on Pro. Keep the agent's lifetime at least as long.
* **Plain allow rules only.** For an allowed domain with no rules, Vercel's firewall passes the connection without decrypting it, which is what the gateway needs. Don't add `transform` rules for OneCLI's hosts.
* **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).
* **Vercel Sandbox's own key injection.** Vercel Sandbox can also add a key to a sandbox's calls itself, with credentials brokering in its network policy. See [how OneCLI compares](https://onecli.sh/docs/guides/sandboxes#why-onecli-when-a-provider-injects-keys).

## From a terminal

To connect one Vercel Sandbox 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}
sandbox exec <sandbox-name> -- sh -c '<install_command>'
```

## Versions

Written in December 2026 for `@onecli-sh/sdk` 4.1 and `@vercel/sandbox` 5.1 in TypeScript, and for `onecli-sdk` 0.1 and `vercel-sandbox` 1.3 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.