Execute Commands

Learn how to run commands inside your sandboxes.

Using the TypeScript SDK

Run commands in your sandbox via the execute method.

example.ts
import SandboxSDK from '@dvmcodes/sandbox-sdk';

const client = new SandboxSDK({
  apiKey: process.env['SANDBOX_SDK_API_KEY'],
});

// Execute a simple command
const result = await client.sandboxes.execute('sbx_x1y2z3a4b5c6d7e8', {
  command: "python -c \"print('Hello, World!')\"",
});

console.log('Exit code:', result.exit_code);
console.log('Output:', result.stdout);
console.log('Errors:', result.stderr);

Basic Command Execution

Run arbitrary shell commands inside your sandbox.

Using Shell Features

The API provides full shell capabilities, including piping, redirection, and command chaining.

Setting Environment Variables

Provide environment variables when running a command.

Setting Working Directory

Run commands from a specified working directory.

Setting Timeout

Set a maximum execution time for commands (1–300 seconds).

Running Python Scripts

Run Python code directly within the sandbox.

Running Node.js Scripts

Execute Node.js code:

Response Structure

The execute() method returns a SandboxExecuteResponse object. Example fields:

Execution Status

The status field indicates the execution state:

  • running — Command is currently executing

  • completed — Command finished successfully or with an error

  • timeout — Command exceeded the timeout limit

  • error — An error occurred during execution

circle-info

Check both status and exit_code to determine command outcome reliably.

Handling Results

Successful Execution

Error Handling

Parameters

command string required

The command to run, supporting full CLI syntax such as pipes and output redirection.

timeout integer default:"30"

Maximum execution time in seconds (1–300). Defaults to 30 seconds.

env object

Environment variables to set for the command execution (key-value pairs).

working_dir string

Working directory for command execution. If not specified, uses the sandbox’s default directory.

Response Fields

Best Practices

  1. Always check the exit code: a command may complete but fail with a non-zero exit code.

  2. Set appropriate timeouts: long-running commands should have higher timeout values.

  3. Use working directories: specify working_dir to ensure commands run in the correct location.

  4. Handle errors gracefully: check both status and exit_code to handle all failure scenarios.

  5. Use environment variables: pass configuration via env rather than hardcoding values in commands.

Last updated