Learn how to run OpenCode in an isolated Vercel Sandbox MicroVM using the Sandbox SDK, with egress locked down so the agent can only reach the domains you allow.
Running a coding agent on your laptop means it shares your network, your credentials, and your access to everything. The Sandbox SDK lets you spin up a MicroVM programmatically, install OpenCode inside it, and then call sandbox.updateNetworkPolicy() to restrict outbound traffic to only the LLM endpoints the agent needs. If the agent tries to reach anything outside that allowlist, the request is blocked at the network level.
Before you begin, make sure you have:
- Vercel CLI installed on your machine. If you don't have it, install it with
pnpm i -g vercel - Node.js 22 or later installed locally
- A Vercel project to link your sandbox to
Start by creating a new directory and initializing it:
Run vercel link to connect your local directory to a Vercel project. If you don't have a project yet, the CLI will create one for you:
Follow the prompts to select your team and project name.
This pulls a VERCEL_OIDC_TOKEN into a local .env.local file. The SDK uses this token to authenticate when creating sandboxes:
Install the Sandbox SDK along with helpers for running TypeScript and loading environment variables:
OpenCode needs a model provider. AI Gateway gives you a single endpoint for Claude, GPT, Gemini, and other models with built-in spend tracking.
- Go to AI Gateway API Keys
- Create a new API key
- Add it to your
.env.localfile:
Replace vck_xxxxxx with the key you just created.
AI Gateway is not required. OpenCode supports 75+ providers natively. You can use your own Anthropic, OpenAI, or other API keys by adjusting the config in the Configure AI Gateway section.
Create an index.ts file. This is where all the code in the following sections goes. Start with the imports and environment setup:
The dotenv import loads VERCEL_OIDC_TOKEN and AI_GATEWAY_API_KEY from .env.local so the SDK can authenticate. Run your script at any point with:
Use Sandbox.create() to launch a fresh MicroVM. Expose port 4096 for the OpenCode server and set a timeout for the session:
If you want OpenCode to work on an existing codebase, pass a source to clone a Git repository into the sandbox:
This clones the repo into the sandbox's working directory so OpenCode can read and edit the files directly. For private repositories, see Using private GitHub repositories with Vercel Sandbox.
Run the official install script inside the sandbox. The stdout and stderr options stream output to your local terminal:
The installer places the binary at ~/.opencode/bin/opencode. When using runCommand (rather than an interactive shell), the installer's PATH update doesn't apply, so you need the full path (we defined OPENCODE_BIN in the setup):
Write an opencode.json config file directly into the sandbox filesystem using sandbox.writeFiles(). This configures OpenCode to use Vercel AI Gateway as its model provider:
Models follow the provider/model-name format. Browse the models catalog for available options.
Start the OpenCode HTTP server in the background with a password for basic auth:
The server needs a few seconds for its initial database migration. Wait for it, then verify it is healthy:
This is the key step. Once OpenCode is installed and running, call sandbox.updateNetworkPolicy() to restrict outbound traffic to only the domains the agent needs. Everything else is blocked at the network level:
From this point on, if the agent tries to reach your email, internal APIs, or any other service, the request is silently dropped. Only traffic to the allowed domains gets through.
Adjust the allowlist based on your provider:
| Provider | Domain |
|---|---|
| AI Gateway | ai-gateway.vercel.sh |
| Anthropic | api.anthropic.com |
| OpenAI | api.openai.com |
You can verify the policy is working from inside the sandbox:
For even stronger security, you can inject API credentials at the network level so they never enter the sandbox at all. This means the agent can make authenticated requests without ever seeing the API key:
With credential brokering, you can omit the API key from the opencode.json config entirely. The firewall injects it into every outbound request to the allowed domain.
Once the server is running, sandbox.domain(OPENCODE_PORT) returns the public URL (e.g. https://sb-xxxxxxxx.vercel.run). You can connect to it two ways.
Open the public URL in your browser. You'll be prompted for HTTP basic auth credentials:
- Username:
opencode - Password: the value you generated earlier (stored in the
passwordvariable)
You can also connect your local terminal for the full TUI experience. Install OpenCode locally first, then run:
Replace <your_password> with the password your script generated, and the URL with your actual sandbox domain.
Installing OpenCode takes a few seconds. You can capture the installed state as a snapshot and skip that step on future sandboxes:
Create new sandboxes from the snapshot with the egress policy applied from the start:
The sandbox boots with OpenCode already installed and egress locked down. You only need to start the server.
You can interact with OpenCode programmatically through its REST API. This is useful for automated workflows, CI pipelines, or building your own frontend.
First, create a helper for authenticated requests:
Create a session:
Send a message and read the response:
For typed requests and built-in SSE support, you can use the `@opencode-ai/sdk` package. Point `createOpencodeClient()` at your sandbox URL with the same auth headers shown above.
- Egress control: call
sandbox.updateNetworkPolicy()to restrict outbound traffic to only the domains the agent needs. Everything else is blocked at the network level, so the agent cannot reach your email, internal APIs, or anything outside the allowlist. - Credential brokering: inject API keys at the firewall level so they never enter the sandbox. The agent makes authenticated requests without seeing the credentials.
- Sandbox isolation: each sandbox is its own MicroVM with no access to your host machine.
- Password auth: the server is protected with HTTP basic auth, so only you can access the web UI and API.
- Timeouts: sandboxes shut down automatically when the timeout expires, so nothing runs forever by accident.
- Port control: only ports you explicitly declare in
portsare reachable from the internet.