API design
The Execution API is built on REST principles with Server-Sent Events (SSE) for real-time streaming:- Base URL:
http://<sandbox-endpoint>(obtained viasandbox.get_endpoint()) - Authentication:
X-EXECD-ACCESS-TOKENheader (automatically handled by SDKs) - Streaming: SSE for command output, code execution results, and metrics
The SDK abstracts the Execution API, so you typically don’t need to call it directly. Use
sandbox.commands, sandbox.files, and CodeInterpreter instead.Command execution
Execute shell commands in the sandbox with streaming output.Foreground commands
Run commands synchronously and capture output:- Real-time output streaming via SSE
- Capture stdout and stderr separately
- Exit code available after completion
- Custom working directory
Background commands
Launch long-running processes in detached mode:- Training ML models
- Running web servers
- Background data processing
- Long-running tests
Command interruption
Interrupt running commands:Code execution
Execute code in multiple languages with stateful sessions.Creating contexts
A context maintains execution state across multiple code blocks:Supported languages
The Code Interpreter supports multiple programming languages:Stateful vs stateless execution
- Stateful (with context)
- Stateless (default context)
Variables and state persist across executions:
Execution results
Code execution returns structured results:File operations
Manage files and directories in the sandbox.Writing files
- Single file
- Multiple files
- Binary files
Reading files
File metadata
Searching files
Use glob patterns to find files:Permissions
Change file permissions:Directory operations
System metrics
Monitor sandbox resource usage in real-time.Snapshot metrics
Get current resource usage:Streaming metrics
Watch metrics in real-time via SSE:Error handling
The Execution API returns structured errors:Common error codes
Best practices
Use appropriate execution methods
Use appropriate execution methods
- Foreground commands: Short-lived commands (< 1 min)
- Background commands: Long-running processes (> 1 min)
- Code execution: When you need structured output and state management
Handle streaming output
Handle streaming output
For commands with large output, process streaming data incrementally:
Clean up contexts
Clean up contexts
Delete code contexts when done to free resources:
Use file batching
Use file batching
Write multiple files in a single operation for better performance:
Command execution
Full reference for command execution endpoints
Code interpreter
Complete code execution API reference
Filesystem
File operations API reference
Examples
Practical examples using the Execution API