Skip to content

Network & secrets

A sandbox’s traffic leaves through E2B’s host, outside the microVM. That’s where E2B applies the sandbox’s network rules: which hosts it may call, and what to add to requests on their way out. Code in the sandbox can’t change either.

So a secret never has to enter the sandbox. The sandbox sends a placeholder, and the host swaps in the real value, only on requests to the hosts you name.

placeholderreal tokenblockednot in allowOutsandboxallowOut api.github.comapi.github.comallowedexample.comany other hostE2B hostadds Authorizationagent codesends a placeholderplaceholderreal tokenblockednot in allowOutsandboxallowOut api.github.comapi.github.comallowedexample.comany other hostE2B hostadds Authorizationagent codesends a placeholder
Requests leave the sandbox through E2B's host. It lets through only the hosts in allowOut, and swaps the secret's real value in for the placeholder on the way to api.github.com.
const sandbox = yield* sandboxes.create({
network: { allowOut: ["github.com", "*.github.com", "registry.npmjs.org"] },
});

Once allowOut has entries, everything else is blocked. network: { allowOut: [] } blocks everything. denyOut blocks CIDR ranges.

yield* sandbox.updateNetwork({ allowOut: ["api.openai.com"] });

The update replaces the sandbox’s rules.

To send a sandbox’s traffic through a network you control, for a fixed IP address or your own inspection, give it a SOCKS5 egress proxy. E2B’s host connects to the proxy, so the sandbox never holds its password:

export const MySandbox = E2B.Sandbox("MySandbox", {
template: MyTemplate,
network: {
egressProxy: {
address: "proxy.example.com:1080",
username: "sandbox",
password: Config.Redacted("PROXY_PASSWORD"),
tls: { enabled: true },
},
},
});

A changed proxy updates the sandbox in place.

The proxy speaks SOCKS5 over TCP, so it runs on a host that accepts TCP connections, such as a Fly Machine or a container behind a network load balancer. A Cloudflare Worker accepts only HTTP, so it can’t be one. The proxy carries the sandbox’s TLS without opening it; to add a credential to requests, use rules.

src/Secrets.ts
import * as E2B from "@alchemy.run/e2b";
import * as Config from "effect/Config";
export const GitHubToken = E2B.Secret("GitHubToken", { value: Config.Redacted("GITHUB_TOKEN") });

E2B never returns a secret’s value. Changing value stores a new version.

.env is only the default. A Stack’s secrets option reads the same values from a secrets manager instead, like Doppler or Infisical, so production keys never sit in a file on anyone’s machine. Pick the source per stage:

alchemy.run.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Doppler from "@alchemy.run/doppler";
import * as E2B from "@alchemy.run/e2b";
import * as Alchemy from "alchemy";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import Api from "./src/Api.ts";
import { GitHubToken } from "./src/Secrets.ts";
export default Alchemy.Stack(
"MyApp",
{
providers: Layer.mergeAll(Cloudflare.providers(), E2B.providers()),
state: Cloudflare.state(),
secrets: ({ stage }) =>
stage === "prod"
? Doppler.Secrets({ project: "my-app", config: "prd" })
: Alchemy.Secrets.DotEnv(),
},
Effect.gen(function* () {
yield* GitHubToken;
const api = yield* Api;
return { url: api.url.as<string>() };
}),
);

Your code doesn’t change: Config.Redacted("GITHUB_TOKEN") reads from whichever source the stage uses. See Secret providers.

const github = yield* GitHubToken;
const sandbox = yield* sandboxes.create({
network: {
allowOut: ["api.github.com"],
rules: {
"api.github.com": [{ transform: { headers: { Authorization: `Bearer ${github.ref}` } } }],
},
},
});

github.ref is a placeholder (${e2b.secrets.<name>}). E2B swaps in the value on the host, only for requests to api.github.com. Code in the sandbox can’t read it.

const secrets = yield* E2B.CreateSecret();
// in fetch:
const token = yield* secrets.put(`github-${user}`, Redacted.make(userToken));

Provide E2B.CreateSecretHttp. Objects created at run time are tagged with the stack, stage and binding (alchemy::stack, alchemy::stage, alchemy::id) and are not deleted when the stack is destroyed. List them by those tags to clean them up. secrets.list() returns the ones the binding stored:

yield* Effect.forEach(yield* secrets.list(), (secret) => secrets.delete(secret.secretId));
const token = yield* E2B.WriteSecret(GitHubToken);
// in a scheduled handler:
yield* token.put(Redacted.make(fresh));

Sandboxes use the new version on their next request.

Sandbox ports are public unless you create the sandbox with allowPublicTraffic: false:

const sandbox = yield* sandboxes.create({ network: { allowPublicTraffic: false } });
const port = yield* sandbox.getTcpPort(3000);

Then port.fetch sends the sandbox’s traffic token for you.

Give a sandbox a cloud identity, not a key

Section titled “Give a sandbox a cloud identity, not a key”
const sandbox = yield* sandboxes.create({
iam: { tokens: { aws: { audience: "sts.amazonaws.com", tokenType: "JWT-SVID" } } },
});

The sandbox gets tokens it can exchange with your cloud, so you don’t hand it long-lived keys.