Skip to content

SYSTEM Cited by 3 sources

Cloudflare Sandbox SDK

Sandbox SDK is Cloudflare's higher-level developer API for running untrusted or agent-generated code inside isolated Cloudflare Containers from a Workers application. It was announced in mid-2025 and is positioned as the structural answer to AI agents needing to execute arbitrary code securely without a user-managed container-lifecycle layer.

What the SDK gives you

Instead of raw Container APIs, the SDK exposes a small, ergonomic TypeScript surface for:

  • Command execution: sandbox.exec('node -v')
  • Filesystem management: sandbox.mkdir(path, { recursive: true })
  • Code contexts / REPL-style execution: sandbox.createCodeContext({ language: 'python' }) then sandbox.runCode(...) with persistent state across calls
  • Background processes
  • Service exposure from inside the container
  • Per-key sandbox instances: getSandbox(env.Sandbox, 'user-123') — per-user or per-session isolation without the app managing lifecycle
  • sandbox.mountBucket() — mount an R2 bucket as a filesystem partition inside the container, giving ephemeral containers a durable working directory with zero application code changes (canonical instance of patterns/mountable-persistent-storage)

The SDK owns container lifecycle, networking, file systems, process management, and the Worker↔container channel — letting the developer focus on application logic.

Relationship to Cloudflare Containers

Sandbox SDK is built on top of Cloudflare Containers. Containers are inherently ephemeral (concepts/container-ephemerality); Sandbox SDK adds the ergonomic layer and the mountable-persistence escape hatch via R2.

Example

import { getSandbox } from '@cloudflare/sandbox';
export { Sandbox } from '@cloudflare/sandbox';

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const sandbox = getSandbox(env.Sandbox, 'user-123');
    await sandbox.mkdir('/workspace/project/src', { recursive: true });
    const version = await sandbox.exec('node -v');
    const ctx = await sandbox.createCodeContext({ language: 'python' });
    await sandbox.runCode('import math; radius = 5', { context: ctx });
    const result = await sandbox.runCode('math.pi * radius ** 2', { context: ctx });
    return Response.json({ version, result });
  }
};

Seen in

Last updated · 200 distilled / 1,178 read