Skip to main content
Sandboxes are isolated, ephemeral cloud environments where your code runs. This guide shows you how to create sandboxes from templates with custom configuration options.

Prerequisites

Before you begin, make sure you have:
  • HopX API key - Get one from console.hopx.dev or see [API key Management](/API key)
  • SDK installed - Python SDK (pip install hopx-ai) or JavaScript SDK (npm install @hopx-ai/sdk)
  • Basic understanding - Familiarity with the Quickstart Guide is helpful

Overview

When you create a sandbox, HopX:
  • Spins up a lightweight VM in seconds (~100ms)
  • Loads resources (CPU, memory, disk) from the template
  • Provides a persistent filesystem during the sandbox lifecycle
  • Auto-destroys the sandbox after timeout (if configured)
Resources (vcpu, memory, disk) are always loaded from the template. You cannot specify custom resources when creating a sandbox. To use different resources, create a custom template first.

Basic Creation

The simplest way to create a sandbox is using a template name:
Expected Output:

Using Template ID

You can also create a sandbox using a template ID instead of a name:
Expected Output:

Configuration Options

When creating a sandbox, you can configure several options:

Timeout

Set an auto-kill timeout to automatically destroy the sandbox after a specified duration:
Expected Output:

Internet Access

Control whether the sandbox has internet access:

Environment Variables

Set initial environment variables when creating the sandbox:
Expected Output:

Using Context Managers (Python)

Python SDK supports context managers for automatic cleanup:
Expected Output:
The context manager automatically calls sandbox.kill() when exiting the with block, even if an error occurs. This ensures proper cleanup.

Finding Available Templates

Before creating a sandbox, you may want to list available templates:

Best Practices

1

1. Always Clean Up

Use context managers (Python) or try/finally blocks to ensure sandboxes are cleaned up, even if errors occur.
2

2. Set Appropriate Timeouts

Set timeouts based on your use case. For quick scripts, use shorter timeouts. For long-running tasks, set longer timeouts or use timeout management.
3

3. Use Environment Variables

Set environment variables at creation time rather than later for better performance and consistency.
4

4. Choose the Right Template

Select templates that match your needs. Use list_templates() to find available options.
5

5. Handle Errors Gracefully

Always handle potential errors (template not found, resource limits, etc.) in your code.

Implementation

Next Steps