Network
Every sandbox has an outbound policy. Archil enforces it outside the microVM, so code inside can’t change it, and it can add headers to requests as they leave, so a key the sandbox uses never sits inside it.
The policy is enforced outside the microVM
Section titled “The policy is enforced outside the microVM”Deny everything but a list of hosts
Section titled “Deny everything but a list of hosts”export const MySandbox = Archil.Sandbox("MySandbox", { network: { egress: { default: "deny", allow: ["registry.npmjs.org", "*.github.com"] }, },});Deny entries win over allow entries. A wildcard matches subdomains, not the apex domain.
Add a credential on the way out
Section titled “Add a credential on the way out”An allow entry can set headers on matching HTTPS requests. The sandbox never sees the value:
export const MySandbox = Archil.Sandbox("MySandbox", { network: { egress: { default: "deny", allow: [ { target: "api.github.com", headers: { Authorization: Config.Redacted("GITHUB_AUTH") } }, ], }, },});Archil has no secret store, so the value lives in the sandbox’s policy.
Send requests through your own gateway
Section titled “Send requests through your own gateway”An allow entry can also forward matching requests to a URL of yours. Archil decrypts the request, applies the entry’s headers, and sends it to forwardUrl with the original path and query. It names the original destination and the calling sandbox in archil-forwarded-host, archil-forwarded-scheme, archil-forwarded-port, archil-forwarded-path and archil-sandbox-id headers.
Forward the API’s requests to the gateway
Section titled “Forward the API’s requests to the gateway”const gateway = yield* MyGateway;const sandbox = yield* Archil.Sandbox("MySandbox", { network: { egress: { default: "deny", allow: [ { target: "api.example.com", headers: { "X-Gateway-Token": Config.Redacted("GATEWAY_TOKEN") }, forwardUrl: gateway.url.as<string>(), }, ], }, },});Requests for api.example.com now go to the gateway. Archil adds the gateway token on the way, so the gateway can tell they came through this policy.
Give the gateway its secrets
Section titled “Give the gateway its secrets”export default class MyGateway extends Cloudflare.Worker<MyGateway>()( "MyGateway", { main: import.meta.url, env: { GATEWAY_TOKEN: Config.Redacted("GATEWAY_TOKEN"), API_KEY: Config.Redacted("API_KEY"), }, }, Effect.succeed({ fetch: forward }),) {}A Cloudflare Worker makes a small gateway. It holds the token it expects and the API key it adds. forward is the handler the next steps build.
Refuse requests without the token
Section titled “Refuse requests without the token”const request = yield* HttpServerRequest;const env = yield* Cloudflare.Workers.WorkerEnvironment;if (request.headers["x-gateway-token"] !== env.GATEWAY_TOKEN) { return HttpServerResponse.text("forbidden", { status: 403 });}The gateway’s URL is public, so anyone can call it. Only Archil adds the token.
Send on only to hosts you list
Section titled “Send on only to hosts you list”const host = request.headers["archil-forwarded-host"];if (host === undefined || !["api.example.com"].includes(host)) { return HttpServerResponse.text("host not allowed", { status: 403 });}Without the list, the gateway would add your key to requests for any host.
Add the key and call the real host
Section titled “Add the key and call the real host”const { pathname, search } = new URL(request.originalUrl);const client = yield* HttpClient.HttpClient;const upstream = yield* client.execute( HttpClientRequest.get(`https://${host}${pathname}${search}`).pipe( HttpClientRequest.setHeader("x-api-key", env.API_KEY), ),);return HttpServerResponse.text(yield* upstream.text, { status: upstream.status });Archil appends the original path and query to forwardUrl, so the gateway rebuilds the URL from them. For other methods, bodies and headers, copy them from request, leaving out the archil- headers and the token.
The gateway holds the API key, so rotating it changes only the Worker. archil-sandbox-id tells it which sandbox called, which it can use to log, rate-limit or pick a key per sandbox. forwardUrl must be a public HTTPS URL on a domain name, so a workers.dev URL or a custom domain works, and an IP address doesn’t.
Change the policy while it runs
Section titled “Change the policy while it runs”yield* sandbox.setEgress({ default: "deny", allow: ["api.openai.com"] });The new policy applies at once. On a declared Archil.Sandbox, changing network updates it in place.
Your plan sets the default
Section titled “Your plan sets the default”On Archil’s Free plan, sandboxes reach only a managed allowlist of package registries, Git hosts and model APIs, and adding headers needs the Developer plan or above. A sandbox created on a paid plan with no policy reaches anything.