---
title: "Cloudflare Sandboxes - Secure Code Execution"
description: "Secure code execution for AI agents, developer tooling, and anything you don't quite trust. Run untrusted code in isolated environments with a simple SDK."
url: "https://www.cloudflare.com/products/sandboxes"
---

# Sandboxes

> Run untrusted code in isolated environments. Give your AI agents, code interpreters, and developer tools a secure place to execute code, install packages, and interact with the filesystem.

## Benefits

### Fully isolated

Each sandbox runs in its own secure container. Execute AI-generated or user-submitted code with zero risk to your infrastructure.

### Fast spin-up

Sandboxes start in milliseconds, not minutes. Execute code immediately without waiting for cold starts.

### Bring your own images

Use standard container images with your own dependencies, tools, and runtimes. Full flexibility without lock-in.

## Use Cases

### AI agent code execution

Give your agents a secure environment to write and run code, install packages, and interact with the filesystem.

### Interactive development environments

Spin up complete dev environments on-demand for testing, CI/CD, or collaborative coding.

### Code interpreters

Run user-submitted Python or JavaScript with automatic output parsing for charts, tables, and rich content.

## Code Examples

### Clone and run tests

Clone a repository and execute shell commands with a simple SDK. Get exit codes, stdout, and stderr.

```typescript
import { getSandbox } from "@cloudflare/sandbox";
export { Sandbox } from "@cloudflare/sandbox";

export default {
  async fetch(request: Request, env: Env) {
    const sandbox = getSandbox(env.Sandbox, "test-runner");

    await sandbox.gitCheckout("https://github.com/cloudflare/agents");
    const result = await sandbox.exec("npm test");

    return Response.json({
      passed: result.exitCode === 0,
      output: result.stdout,
    });
  },
};
```

### Create and manage files

Read, write, and organize files in the sandbox filesystem. Perfect for scaffolding projects or processing uploads.

```typescript
import { getSandbox } from "@cloudflare/sandbox";
export { Sandbox } from "@cloudflare/sandbox";

export default {
  async fetch(request: Request, env: Env) {
    const sandbox = getSandbox(env.Sandbox, "file-ops");

    await sandbox.mkdir("/workspace/src", { recursive: true });
    await sandbox.writeFile("/workspace/package.json", JSON.stringify({
      name: "my-app",
      type: "module",
    }));

    const result = await sandbox.readFile("/workspace/package.json");
    return Response.json({ content: result.content });
  },
};
```

### Run Python or JavaScript

Execute code with automatic output parsing. Charts, tables, and images are extracted for you.

```typescript
import { getSandbox } from "@cloudflare/sandbox";
export { Sandbox } from "@cloudflare/sandbox";

export default {
  async fetch(request: Request, env: Env) {
    const sandbox = getSandbox(env.Sandbox, "interpreter");
    const ctx = await sandbox.createCodeContext({ language: "python" });

    const result = await sandbox.runCode(`
import math
for i in range(5):
    print(f"Step {i}: {math.pi * i:.2f}")
`, { context: ctx });

    return Response.json({ output: result.logs?.stdout?.join("\n") });
  },
};
```

## Resources

- [Full Documentation](https://developers.cloudflare.com/sandbox): Complete technical documentation
- [Get Started](https://dash.cloudflare.com/sign-up): Sign up and start building
- [Pricing](/plans.md): See pricing details

## Related Products

- [Browser Run](/products/browser-rendering.md): Automated browsers
- [Cloudflare Pages](/products/pages.md): Build & deploy frontend sites
- [Containers](/products/containers.md): Any language, anywhere
- [Durable Objects](/products/durable-objects.md): Stateful compute

---

*This is a markdown version of [https://www.cloudflare.com/products/sandboxes](https://www.cloudflare.com/products/sandboxes) for AI/LLM consumption.*
