Execute Commands
Learn how to execute commands in your sandboxes
Using the TypeScript SDK
Execute commands in your sandbox using the execute method:
Basic Command Execution
Execute any shell command in your sandbox:
Using Shell Features
The API supports full shell features like pipes, redirection, and chaining:
Setting Environment Variables
Pass environment variables to your command:
Setting Working Directory
Execute commands in a specific directory:
Setting Timeout
Control how long a command can run (1-300 seconds):
Running Python Scripts
Execute Python code directly:
Running Node.js Scripts
Execute Node.js code:
Response Structure
The execute() method returns a SandboxExecuteResponse object:
Execution Status
The status field indicates the execution state:
running- Command is currently executingcompleted- Command finished successfully or with an errortimeout- Command exceeded the timeout limiterror- An error occurred during execution
Handling Results
Successful Execution
Error Handling
Parameters
The command to execute. Supports full CLI commands with shell features like redirection, pipes, etc.
Execution timeout in seconds (1-300). Default is 30 seconds.
Environment variables to set for the command execution (key-value pairs).
Working directory for command execution. If not specified, uses the sandbox’s default directory.
Response Fields
Best Practices
- Always check the exit code: A command may complete but fail with a non-zero exit code
- Set appropriate timeouts: Long-running commands should have higher timeout values
- Use working directories: Specify
working_dirto ensure commands run in the correct location - Handle errors gracefully: Check both
statusandexit_codeto handle all failure scenarios - Use environment variables: Pass configuration via
envrather than hardcoding values in commands