> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hopx.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Code Execution

> Complete guide to executing code in HopX sandboxes - synchronous, asynchronous, background, and streaming execution modes. Learn how to run Python, JavaScript, Bash, and Go code, capture rich outputs like plots and DataFrames, manage background processes, and stream execution results in real-time using SDK methods or REST API endpoints.

Execute code in HopX sandboxes using multiple execution modes. This hub covers all code execution methods, from simple synchronous runs to advanced background and streaming execution.

## Related

* [CLI Code Execution](/cli/commands/run) - Execute code from CLI

## What You'll Learn

* How to execute code synchronously and asynchronously
* How to capture rich outputs (plots, DataFrames, visualizations)
* How to run code in the background for long-running tasks
* How to stream execution results in real-time
* How to manage processes and handle async webhooks

## Code Execution Overview

| Execution Mode                                                             | SDK Method                              | API Endpoint               |
| -------------------------------------------------------------------------- | --------------------------------------- | -------------------------- |
| **[Synchronous](/core-concepts/code-execution/synchronous)**               | `sandbox.run_code()`                    | `POST /execute`            |
| **[Rich Output](/core-concepts/code-execution/rich-output)**               | `sandbox.run_code()` with `rich_output` | `POST /execute`            |
| **[Background](/core-concepts/code-execution/background)**                 | `sandbox.run_code_background()`         | `POST /execute/background` |
| **[Streaming](/core-concepts/code-execution/streaming)**                   | `sandbox.run_code_stream()`             | `POST /execute/stream`     |
| **[Process Management](/core-concepts/code-execution/process-management)** | `sandbox.list_processes()`              | `GET /processes`           |
| **[Async Webhooks](/core-concepts/code-execution/async-webhooks)**         | Webhook configuration                   | Webhook endpoints          |

## Core Concepts

### Synchronous Execution

Execute code and wait for results. Perfect for quick scripts and data analysis.

* **[Synchronous Execution](/core-concepts/code-execution/synchronous)** - Execute code and get results immediately
* **SDK**: [Python: sandbox.run\_code()](/sdk/python/sandbox#run_code) | [JavaScript: sandbox.runCode()](/sdk/javascript/sandbox#runcode)
* **API**: [POST /execute](/api/vm-agent/execute)

### Rich Output Capture

Capture matplotlib plots, pandas DataFrames, and other rich outputs automatically.

* **[Rich Output](/core-concepts/code-execution/rich-output)** - Capture plots, DataFrames, and visualizations
* **SDK**: [Python: sandbox.run\_code() with rich\_output](/sdk/python/sandbox#run_code) | [JavaScript: sandbox.runCode() with richOutput](/sdk/javascript/sandbox#runcode)
* **API**: [POST /execute](/api/vm-agent/execute) with `rich_output` parameter

### Background Execution

Run long-running code without blocking. Perfect for training models or processing large datasets.

* **[Background Execution](/core-concepts/code-execution/background)** - Run code asynchronously
* **SDK**: [Python: sandbox.run\_code\_background()](/sdk/python/sandbox#run_code_background) | [JavaScript: sandbox.runCodeBackground()](/sdk/javascript/sandbox#runcodebackground)
* **API**: [POST /execute/background](/api/vm-agent/execute-background)

### Streaming Execution

Stream execution results in real-time as code runs.

* **[Streaming Execution](/core-concepts/code-execution/streaming)** - Stream results in real-time
* **SDK**: [Python: sandbox.run\_code\_stream()](/sdk/python/sandbox#run_code_stream) | [JavaScript: sandbox.runCodeStream()](/sdk/javascript/sandbox#runcodestream)
* **API**: [POST /execute/stream](/api/vm-agent/execute-async)

### Process Management

List, monitor, and manage background processes.

* **[Process Management](/core-concepts/code-execution/process-management)** - Manage background processes
* **SDK**: [Python: sandbox.list\_processes()](/sdk/python/sandbox#list_processes) | [JavaScript: sandbox.listProcesses()](/sdk/javascript/sandbox#listprocesses)
* **API**: [GET /processes](/api/vm-agent/list-processes) | [DELETE /execute/kill](/api/vm-agent/kill-process)

### Async Webhooks

Handle code execution results via webhooks for async workflows.

* **[Async Webhooks](/core-concepts/code-execution/async-webhooks)** - Handle results via webhooks
* **SDK**: Webhook configuration in SDK | **API**: Webhook endpoints

## SDK Reference

### Python SDK

Complete reference for Python code execution.

* **[Sandbox.run\_code()](/sdk/python/sandbox#run_code)** - Synchronous execution
* **[Sandbox.run\_code\_background()](/sdk/python/sandbox#run_code_background)** - Background execution
* **[Sandbox.run\_code\_stream()](/sdk/python/sandbox#run_code_stream)** - Streaming execution
* **[Sandbox.list\_processes()](/sdk/python/sandbox#list_processes)** - Process management

### JavaScript SDK

Complete reference for JavaScript/TypeScript code execution.

* **[Sandbox.runCode()](/sdk/javascript/sandbox#runcode)** - Synchronous execution
* **[Sandbox.runCodeBackground()](/sdk/javascript/sandbox#runcodebackground)** - Background execution
* **[Sandbox.runCodeStream()](/sdk/javascript/sandbox#runcodestream)** - Streaming execution
* **[Sandbox.listProcesses()](/sdk/javascript/sandbox#listprocesses)** - Process management

## API Reference

### VM Agent API

REST API endpoints for code execution.

* **[VM Agent API Overview](/api/vm-agent/overview)** - API overview
* **[Execute Code](/api/vm-agent/execute)** - `POST /execute` - Synchronous execution
* **[Execute Background](/api/vm-agent/execute-background)** - `POST /execute/background` - Background execution
* **[Execute Async](/api/vm-agent/execute-async)** - `POST /execute/async` - Async execution
* **[List Processes](/api/vm-agent/list-processes)** - `GET /processes` - List background processes
* **[Kill Process](/api/vm-agent/kill-process)** - `DELETE /execute/kill` - Terminate process

## Quick Start

If you're new to code execution, start here:

1. **[Synchronous Execution](/core-concepts/code-execution/synchronous)** - Execute your first code
2. **[Rich Output](/core-concepts/code-execution/rich-output)** - Capture plots and DataFrames
3. **[Background Execution](/core-concepts/code-execution/background)** - Run long-running tasks

## Related Topics

* **[Sandboxes](/core-concepts/sandboxes/creating)** - Create and manage sandboxes
* **[File Operations](/core-concepts/filesystem/reading)** - Work with files during execution
* **[Environment Variables](/core-concepts/environment/getting)** - Configure execution environment
* **[Observability](/core-concepts/observability/metrics)** - Monitor execution metrics

## Next Steps

* Learn about [File Operations](/core-concepts/filesystem/reading) for working with files
* Explore [Environment Variables](/core-concepts/environment/getting) for configuration
* Review [Process Management](/core-concepts/code-execution/process-management) for advanced workflows
* Check out [Observability](/core-concepts/observability/metrics) for monitoring
