Skip to main content
This guide walks you through creating a basic sandbox, executing commands, and performing file operations using the OpenSandbox SDK.

Prerequisites

Before you begin, ensure you have:
  • Docker installed and running
  • Python 3.10 or higher
  • OpenSandbox server installed and configured

Installation

1

Install OpenSandbox packages

Install the required Python packages using uv or pip:
2

Start the OpenSandbox server

Initialize the configuration and start the server:
The server runs in the foreground. Open a new terminal window to run the example code.

Creating Your First Sandbox

1

Import required modules

Start by importing the necessary modules from the OpenSandbox SDK:
2

Create a sandbox instance

Create a sandbox with the Code Interpreter image:
The create method accepts:
  • Image name: Docker image to use for the sandbox
  • Entrypoint: Command to run when the container starts
  • Environment variables: Custom environment configuration
  • Timeout: Maximum sandbox lifetime
3

Execute commands

Use the sandbox context manager to execute shell commands:
The commands.run() method executes shell commands and returns:
  • stdout: Standard output from the command
  • stderr: Standard error output
  • exit_code: Command exit code
4

Perform file operations

Write and read files within the sandbox:
5

Execute code with Code Interpreter

Create a code interpreter and run Python code:
6

Clean up resources

Always clean up the sandbox when you’re done:

Complete Example

Here’s the complete working example:

Expected Output

Next Steps

Code Interpreter Basics

Learn how to execute code in multiple languages

File Operations

Master file handling within sandboxes

API Reference

Explore the complete API documentation

More Examples

Browse advanced examples on GitHub