Skip to content

Connect to Workers bindings

Sandboxes can access Workers bindings — KV, R2, D1, Durable Objects, and others — through outbound handlers. An outbound handler intercepts HTTP requests from the sandbox and runs inside the Workers runtime, where all of your configured bindings are available.

The sandbox makes a plain HTTP request to a virtual hostname (for example, http://my.kv/some-key), and the outbound handler resolves it using the bound resource. No SDK or client library is required inside the sandbox.

Use bindings in outbound handlers

Define an outboundByHost handler for each virtual hostname. The env argument gives you access to every binding declared in your Wrangler configuration.

JavaScript
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 });
},
};

The sandbox calls http://my.kv/some-key and the handler resolves it using the KV binding. A call to http://my.r2/file.png reads from R2, scoped to the current sandbox instance.

Access Durable Object state

The ctx argument exposes containerId, which lets you interact with the sandbox's own Durable Object from an outbound handler.

JavaScript
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);
},
};