Sandbox SDK

4 min read Original article ↗

Meet Cloudflare Sandboxes.

Execute commands, manage files, run services, and expose them via public URLs - all within secure, sandboxed containers. Get Started

import { getSandbox } from "@cloudflare/sandbox";

// Export the Sandbox class in your Worker
export { Sandbox } from "@cloudflare/sandbox";

export default {
  async fetch(request: Request, env: Env) {
    const sandbox = getSandbox(env.Sandbox, "test-env");
    // Clone a repository
    await sandbox.gitCheckout(
      "https://github.com/cloudflare/agents"
    );
    // Run tests
    const testResult = await sandbox.exec("npm test");
    return new Response(
      JSON.stringify({
        tests: testResult.exitCode === 0 
          ? "passed"
          : "failed",
        output: testResult.stdout,
      })
    );
  },
};

Meet Cloudflare Sandboxes.

Execute commands, manage files, run services, and expose them via public URLs - all within secure, sandboxed containers. Get Started

Sandbox

import { getSandbox } from "@cloudflare/sandbox";

// Export the Sandbox class in your Worker
export { Sandbox } from "@cloudflare/sandbox";

export default {
  async fetch(request: Request, env: Env) {
    const sandbox = getSandbox(env.Sandbox, "test-env");
    // Clone a repository
    await sandbox.gitCheckout(
      "https://github.com/cloudflare/agents"
    );
    // Run tests
    const testResult = await sandbox.exec("npm test");
    return new Response(
      JSON.stringify({
        tests: testResult.exitCode === 0 
          ? "passed"
          : "failed",
        output: testResult.stdout,
      })
    );
  },
};

$ git clone https://github.com/cloudflare/agents

$ npm test

FeaturesFeaturesFeaturesFeatures

FeaturesFeaturesFeaturesFeaturesFeaturesFeatures

Long-running processes

Safely execute tasks that require extended computation or monitoring without risking system stability or security.

Long-running processes

Safely execute tasks that require extended computation or monitoring without risking system stability or security.

Real time streaming

Listen to standard output & error streams live when executing long-running commands

Real time streaming

Listen to standard output & error streams live when executing long-running commands

Real time streaming

Sure thing! I can guide you through implementing the issue and opening a pull request. First, I need some details:

Preview URLs

Instantly expose any container port as a public URL with automatic subdomain routing

Preview URLs

Instantly expose any container port as a public URL with automatic subdomain routing

Preview 3

Preview 2

Preview 1

Hello world!

Code interpreter

Run Python/JavaScript code directly, with rich outputs (charts, tables, images) parsed automatically for you

Code interpreter

Run Python/JavaScript code directly, with rich outputs (charts, tables, images) parsed automatically for you

app.py

for i in range(5):
    print(f"Hello Python! On step {i}")
    time.sleep(1)

File system

Easy methods for basic filesystem operations and cloning git repositories on the container filesystem

File system

Easy methods for basic filesystem operations and cloning git repositories on the container filesystem

Command execution

Run any shell command with proper exit codes, streaming, and error handling

Command execution

Run any shell command with proper exit codes, streaming, and error handling

$ git clone https://github.com/cloudflare/agents

WebSockets

Enable real-time, bidirectional communication by connecting directly to WebSocket servers running in the sandbox

WebSockets

Enable real-time, bidirectional communication by connecting directly to WebSocket servers running in the sandbox

ExamplesExamplesExamplesExamples

ExamplesExamplesExamplesExamplesExamplesExamples

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

export default {
  async fetch(request: Request, env: Env) {
    const sandbox = getSandbox(env.Sandbox, 'user-123');

    // Create a project structure
    await sandbox.mkdir('/workspace/project/src', {
      recursive: true
    });

    // Write files
    await sandbox.writeFile(
     '/workspace/project/package.json',
      JSON.stringify({ name: 'my-app', version: '1.0.0', });

    // Read a file back
    const content = await sandbox.readFile(
      '/workspace/project/src/package.json'
    );
    return Response.json({ content });
  }
};

File OperationsFile Operations

Interactive development environmentExpose services with preview URLs

Expose services with preview URLsRun a Node.js app

Run a Node.js appCode Interpreter

Code interpreterWebSocket Connections

WebSocket connectionsWebSocket Connections

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

export default {
  async fetch(request: Request, env: Env) {
    const sandbox = getSandbox(env.Sandbox, 'user-123');

    // Create a project structure
    await sandbox.mkdir('/workspace/project/src', {
      recursive: true
    });

    // Write files
    await sandbox.writeFile(
     '/workspace/project/package.json',
      JSON.stringify({ name: 'my-app', version: '1.0.0', });

    // Read a file back
    const content = await sandbox.readFile(
      '/workspace/project/src/package.json'
    );
    return Response.json({ content });
  }
};

TestimonialsTestimonialsTestimonialsTestimonials

TestimonialsTestimonialsTestimonialsTestimonialsTestimonialsTestimonials

Testimonial

The sandbox SDK is a core part of our infrastructure at Iterate. It's made giving our agents a 'computer' really easy to do, saving us weeks of effort. The team has been very responsive and helpful when dealing with us throughout the implementation process.

Nick Blow

Founding Engineer · Iterate

Testimonial

The sandbox SDK is a core part of our infrastructure at Iterate. It's made giving our agents a 'computer' really easy to do, saving us weeks of effort. The team has been very responsive and helpful when dealing with us throughout the implementation process.

Nick Blow

Founding Engineer · Iterate

Testimonial

The developer experience is well-thought-out and built on layers of nice abstractions you can override as needed.

Seve Ibarluzea

Co-Founder · tscircuit.com

Testimonial

The developer experience is well-thought-out and built on layers of nice abstractions you can override as needed.

Seve Ibarluzea

Co-Founder · tscircuit.com

Testimonial

Sandbox-sdk has made it possible to orchestrate and manage running insecure user apps so much easier. You can simply launch a sandbox, expose port and it would handle all the proxying, allocation for you.

Ashish Kumar

Engineer · VibeSDK

Testimonial

Sandbox-sdk has made it possible to orchestrate and manage running insecure user apps so much easier. You can simply launch a sandbox, expose port and it would handle all the proxying, allocation for you.

Ashish Kumar

Engineer · VibeSDK