Skip to content
Cloudflare Docs

API Reference

The Sandbox SDK provides a comprehensive API for executing code, managing files, running processes, and exposing services in isolated sandboxes. All operations are performed through the Sandbox instance you obtain via getSandbox().

Getting a sandbox instance

TypeScript
import { getSandbox } from '@cloudflare/sandbox';
const sandbox = getSandbox(env.Sandbox, 'user-123');

The sandbox ID should be unique per user or session. The same ID will always return the same sandbox instance with persistent state.

API organization

The Sandbox SDK is organized into focused APIs:

Commands

Execute commands and stream output. Includes exec(), execStream(), and background process management.

Files

Read, write, and manage files in the sandbox filesystem. Includes directory operations and file metadata.

Code Interpreter

Execute Python and JavaScript code with rich outputs including charts, tables, and formatted data.

Ports

Expose services running in the sandbox via preview URLs. Access web servers and APIs from the internet.

Sessions

Advanced: Create isolated execution contexts with persistent shell state. Configure environment variables and manage container lifecycle.