Handle outbound traffic
Outbound Workers are Workers that handle HTTP requests made by your sandbox. They act as programmable egress proxies, running on the same machine as the sandbox with access to all Workers bindings.
Use outbound Workers to route requests to Workers functions and their bindings (KV, R2, Durable Objects, etc.)
Use outbound to intercept outbound HTTP traffic regardless of destination:
import { Sandbox } from "@cloudflare/sandbox";
export class MySandbox extends Sandbox {}
MySandbox.outbound = async (request, env, ctx) => { if (request.method !== "GET") { console.log(`Blocked ${request.method} to ${request.url}`); return new Response("Method Not Allowed", { status: 405 }); } return fetch(request);};import { Sandbox } from "@cloudflare/sandbox";
export class MySandbox extends Sandbox {}
MySandbox.outbound = async (request: Request, env: Env, ctx: OutboundHandlerContext) => {if (request.method !== "GET") {console.log(`Blocked ${request.method} to ${request.url}`);return new Response("Method Not Allowed", { status: 405 });}return fetch(request);};Use outboundByHost to map specific domain names or IP addresses to handler functions:
import { Sandbox } from "@cloudflare/sandbox";
export class MySandbox extends Sandbox {}
MySandbox.outboundByHost = { "my.worker": async (request, env, ctx) => { // Run arbitrary Workers logic from this hostname return await someWorkersFunction(request.body); },};import { Sandbox } from "@cloudflare/sandbox";
export class MySandbox extends Sandbox {}
MySandbox.outboundByHost = { "my.worker": async (request: Request, env: Env, ctx: OutboundHandlerContext) => { // Run arbitrary Workers logic from this hostname return await someWorkersFunction(request.body); },};The sandbox calls http://my.worker and the handler runs entirely inside the Workers runtime, outside of the sandbox.
If you define both, outboundByHost handlers take precedence over the catch-all outbound handler.
Outbound handlers have access to your Worker's bindings. Route sandbox traffic to internal platform resources without changing application code.
export class MySandbox extends Sandbox {}
MySandbox.outboundByHost = { "my.kv": async (request, env, ctx) => { const url = new URL(request.url); const key = url.pathname.slice(1); const value = await env.KV.get(key); return new Response(value ?? "", { status: value ? 200 : 404 }); }, "my.r2": async (request, env, ctx) => { const url = new URL(request.url); // Scope access to this sandbox's ID const path = `${ctx.containerId}${url.pathname}`; const object = await env.R2.get(path); return new Response(object?.body ?? null, { status: object ? 200 : 404 }); },};export class MySandbox extends Sandbox {}
MySandbox.outboundByHost = {"my.kv": async (request: Request, env: Env, ctx: OutboundHandlerContext) => {const url = new URL(request.url);const key = url.pathname.slice(1);const value = await env.KV.get(key);return new Response(value ?? "", { status: value ? 200 : 404 });},"my.r2": async (request: Request, env: Env, ctx: OutboundHandlerContext) => {const url = new URL(request.url);// Scope access to this sandbox's IDconst path = `${ctx.containerId}${url.pathname}`;const object = await env.R2.get(path);return new Response(object?.body ?? null, { status: object ? 200 : 404 });},};The sandbox calls http://my.kv/some-key and the outbound handler resolves it using the KV binding.
The ctx argument exposes containerId, which lets you interact with the sandbox's own Durable Object from an outbound handler.
export class MySandbox extends Sandbox {}
MySandbox.outboundByHost = { "get-state.do": async (request, env, ctx) => { const id = env.MY_SANDBOX.idFromString(ctx.containerId); const stub = env.MY_SANDBOX.get(id); // Assumes getStateForKey is defined on your DO return stub.getStateForKey(request.body); },};export class MySandbox extends Sandbox {}
MySandbox.outboundByHost = { "get-state.do": async (request: Request, env: Env, ctx: { containerId: string }) => { const id = env.MY_SANDBOX.idFromString(ctx.containerId); const stub = env.MY_SANDBOX.get(id); // Assumes getStateForKey is defined on your DO return stub.getStateForKey(request.body); },};Use outboundHandlers to define named handlers, then assign them to specific hosts at runtime using setOutboundByHost(). You can also apply a handler globally with setOutboundHandler().
import { Sandbox } from "@cloudflare/sandbox";
export class MySandbox extends Sandbox {}
MySandbox.outboundHandlers = { kvAccess: async (request, env, ctx) => { const key = new URL(request.url).pathname.slice(1); const value = await env.KV.get(key); return new Response(value ?? "", { status: value ? 200 : 404 }); },};import { Sandbox } from "@cloudflare/sandbox";
export class MySandbox extends Sandbox {}
MySandbox.outboundHandlers = {kvAccess: async (request: Request, env: Env, ctx: OutboundHandlerContext) => {const key = new URL(request.url).pathname.slice(1);const value = await env.KV.get(key);return new Response(value ?? "", { status: value ? 200 : 404 });},};Apply handlers to hosts programmatically from your Worker:
import { getSandbox } from "@cloudflare/sandbox";
export default { async fetch(request, env) { const sandbox = getSandbox(env.Sandbox, "agent-session");
// Give the sandbox access to KV on a specific host during setup await sandbox.setOutboundByHost("my.kv", "kvAccess"); await sandbox.exec("node setup.js");
// Remove access once setup is complete await sandbox.removeOutboundByHost("my.kv"); },};import { getSandbox } from "@cloudflare/sandbox";
export default { async fetch(request: Request, env: Env) { const sandbox = getSandbox(env.Sandbox, "agent-session");
// Give the sandbox access to KV on a specific host during setup await sandbox.setOutboundByHost("my.kv", "kvAccess"); await sandbox.exec("node setup.js");
// Remove access once setup is complete await sandbox.removeOutboundByHost("my.kv");
},};wrangler dev supports outbound interception. A sidecar process is spawned inside the sandbox's network namespace. It applies TPROXY rules to route matching traffic to the local Workerd instance, mirroring production behavior.
- Handle outbound traffic (Containers) — Container SDK API for outbound handlers
- Sandbox options — Configure sandbox behavior
- Environment variables — Configure secrets and environment variables
- Handle outbound traffic (Containers) — Container SDK API for outbound handlers
- Sandbox options — Configure sandbox behavior
- Environment variables — Configure secrets and environment variables