Connect to sandboxes that are already running. This is useful when you need to reconnect to a sandbox after your application restarts, or when working with sandboxes created by other processes.
Prerequisites
Before you begin, make sure you have:
- Existing sandbox - A sandbox that was previously created (see Creating Sandboxes)
- Sandbox ID - The ID of the sandbox you want to connect to
- API key - Your HopX API key configured
Overview
The connect() method allows you to reconnect to an existing sandbox by its ID. When connecting:
- If sandbox is paused → Automatically resumes it and refreshes JWT token
- If sandbox is stopped → Raises an error (cannot connect to stopped sandbox)
- If sandbox is running → Refreshes JWT token for agent authentication
- JWT token → Automatically refreshed and stored for agent operations
You cannot connect to a stopped sandbox. Use sandbox.start() first, or create a new sandbox.
Basic Connection
Connect to an existing sandbox by ID:
Finding Sandbox IDs
You can find sandbox IDs by listing your sandboxes:
Handling Different States
The connect() method handles different sandbox states automatically:
Complete Workflow Example
Here’s a complete example showing the connect workflow:
Error Handling
Handle connection errors appropriately:
Use Cases
Reconnecting After Application Restart
Working with Shared Sandboxes
JWT Token Management
When you connect to a sandbox, the SDK automatically:
- Refreshes the JWT token for agent authentication
- Stores the token for subsequent agent operations
- Handles token expiration automatically
You don’t need to manage tokens manually - the SDK handles this for you.
Implementation
Next Steps