Sandbox Management
Sandboxes are isolated development environments managed by Daytona. This guide covers how to create, manage, and remove Sandboxes using the SDK.
Examples
// Initialize using environment variables (DAYTONA_API_KEY, DAYTONA_SERVER_URL, DAYTONA_TARGET)const daytona = new Daytona();
// Create and use a sandboxconst sandbox = await daytona.create({ language: 'typescript', envVars: { NODE_ENV: 'development' }});
// Execute commands in the sandboxconst response = await sandbox.process.executeCommand('echo "Hello, World!"');console.log(response.result);
// Execute code in the sandboxconst response = await sandbox.process.codeRun('console.log("Hello, World!")');console.log(response.result);
// Initialize with explicit configurationconst daytona = new Daytona({ apiKey: process.env.CUSTOM_API_KEY, serverUrl: 'https://daytona.example.com', target: 'us'});
// Create a custom sandboxconst sandbox = await daytona.create({ language: 'typescript', image: 'node:18', resources: { cpu: 2, memory: 4 // 4GB RAM }, autoStopInterval: 60 // Auto-stop after 1 hour of inactivity});
// Use sandbox featuresawait sandbox.git.clone('https://github.com/user/repo.git');await sandbox.process.executeCommand('npm test');
CodeLanguage
Supported programming languages for code execution
Enumeration Members
Enumeration Member | Value |
---|---|
JAVASCRIPT | "javascript" |
PYTHON | "python" |
TYPESCRIPT | "typescript" |
Daytona
Main class for interacting with Daytona Server API.
Provides methods for creating, managing, and interacting with Daytona Sandboxes. Can be initialized either with explicit configuration or using environment variables.
Examples
// Using environment variables// Uses DAYTONA_API_KEY, DAYTONA_SERVER_URL, DAYTONA_TARGETconst daytona = new Daytona();const sandbox = await daytona.create();
// Using explicit configurationconst config: DaytonaConfig = { apiKey: "your-api-key", serverUrl: "https://your-server.com", target: "us"};const daytona = new Daytona(config);
@class
Constructors
new Daytona()
new Daytona(config?: DaytonaConfig): Daytona
Creates a new Daytona client instance.
Parameters
Parameter | Type | Description |
---|---|---|
config? | DaytonaConfig | Configuration options |
Returns
Daytona
Throws
DaytonaError
- When API key or server URL is missing
Methods
create()
create(params?: CreateSandboxParams, timeout?: number): Promise<Sandbox>
Creates Sandboxes with default or custom configurations. You can specify various parameters, including language, image, resources, environment variables, and volumes for the Sandbox.
Parameters
Parameter | Type | Default value | Description |
---|---|---|---|
params? | CreateSandboxParams | undefined | Parameters for Sandbox creation |
timeout? | number | 60 | Timeout in seconds (0 means no timeout, default is 60) |
Returns
Promise<Sandbox>
The created Sandbox instance
Examples
// Create a default sandboxconst sandbox = await daytona.create();
// Create a custom sandboxconst params: CreateSandboxParams = { language: 'typescript', image: 'node:18', envVars: { NODE_ENV: 'development', DEBUG: 'true' }, resources: { cpu: 2, memory: 4 // 4GB RAM }, autoStopInterval: 60};const sandbox = await daytona.create(params, 40);
get()
get(sandboxId: string): Promise<Sandbox>
Gets a Sandbox by its ID.
Parameters
Parameter | Type | Description |
---|---|---|
sandboxId | string | The ID of the Sandbox to retrieve |
Returns
Promise<Sandbox>
The Sandbox
Example
const sandbox = await daytona.get('my-sandbox-id');console.log(`Sandbox state: ${sandbox.instance.state}`);
getCurrentSandbox()
getCurrentSandbox(sandboxId: string): Promise<Sandbox>
Gets the Sandbox by ID.
Parameters
Parameter | Type | Description |
---|---|---|
sandboxId | string | The ID of the Sandbox to retrieve |
Returns
Promise<Sandbox>
The Sandbox
Example
const sandbox = await daytona.getCurrentSandbox('my-sandbox-id');console.log(`Current sandbox state: ${sandbox.instance.state}`);
getCurrentWorkspace()
getCurrentWorkspace(workspaceId: string): Promise<Sandbox>
Gets the Sandbox by ID.
Parameters
Parameter | Type | Description |
---|---|---|
workspaceId | string | The ID of the Sandbox to retrieve |
Returns
Promise<Sandbox>
The Sandbox
Deprecated
Use getCurrentSandbox
instead. This method will be removed in a future version.
list()
list(): Promise<Sandbox[]>
Lists all Sandboxes.
Returns
Promise<Sandbox[]>
Array of Sandboxes
Example
const sandboxes = await daytona.list();for (const sandbox of sandboxes) { console.log(`${sandbox.id}: ${sandbox.instance.state}`);}
remove()
remove(sandbox: Sandbox, timeout: number): Promise<void>
Removes a Sandbox.
Parameters
Parameter | Type | Default value | Description |
---|---|---|---|
sandbox | Sandbox | undefined | The Sandbox to remove |
timeout | number | 60 | Timeout in seconds (0 means no timeout, default is 60) |
Returns
Promise<void>
Example
const sandbox = await daytona.get('my-sandbox-id');await daytona.remove(sandbox);
start()
start(sandbox: Sandbox, timeout?: number): Promise<void>
Starts a Sandbox and waits for it to be ready.
Parameters
Parameter | Type | Description |
---|---|---|
sandbox | Sandbox | The Sandbox to start |
timeout? | number | Optional timeout in seconds (0 means no timeout) |
Returns
Promise<void>
Example
const sandbox = await daytona.get('my-sandbox-id');// Wait up to 60 seconds for the sandbox to startawait daytona.start(sandbox, 60);
stop()
stop(sandbox: Sandbox): Promise<void>
Stops a Sandbox.
Parameters
Parameter | Type | Description |
---|---|---|
sandbox | Sandbox | The Sandbox to stop |
Returns
Promise<void>
Example
const sandbox = await daytona.get('my-sandbox-id');await daytona.stop(sandbox);
CreateSandboxParams
Parameters for creating a new Sandbox.
Example
const params: CreateSandboxParams = { language: 'typescript', envVars: { NODE_ENV: 'development' }, resources: { cpu: 2, memory: 4 // 4GB RAM }, autoStopInterval: 60 // Auto-stop after 1 hour of inactivity};const sandbox = await daytona.create(params, 50);
Properties
Property | Type | Description |
---|---|---|
async? | boolean | If true, will not wait for the Sandbox to be ready before returning |
autoStopInterval? | number | Auto-stop interval in minutes (0 means disabled) |
envVars? | Record<string, string> | Optional environment variables to set in the Sandbox |
id? | string | Optional Sandbox ID. If not provided, a random ID will be generated |
image? | string | Optional Docker image to use for the Sandbox |
labels? | Record<string, string> | Sandbox labels |
language? | string | Programming language for direct code execution |
public? | boolean | Is the Sandbox port preview public |
resources? | SandboxResources | Resource allocation for the Sandbox |
target? | string | Target location for the Sandbox |
timeout? | number | Timeout in seconds for the Sandbox to be ready (0 means no timeout) |
user? | string | Optional os user to use for the Sandbox |
DaytonaConfig
Configuration options for initializing the Daytona client.
Example
const config: DaytonaConfig = { apiKey: "your-api-key", serverUrl: "https://your-server.com", target: "us"};const daytona = new Daytona(config);
Properties
Property | Type | Description |
---|---|---|
apiKey? | string | API key for authentication with Daytona server |
serverUrl? | string | URL of the Daytona server |
target? | CreateWorkspaceTargetEnum | Target location for Sandboxes |
SandboxResources
Resource allocation for a Sandbox.
Example
const resources: SandboxResources = { cpu: 2, memory: 4, // 4GB RAM disk: 20 // 20GB disk};
Properties
Property | Type | Description |
---|---|---|
cpu? | number | CPU allocation for the Sandbox in cores |
disk? | number | Disk space allocation for the Sandbox in GB |
gpu? | number | GPU allocation for the Sandbox in units |
memory? | number | Memory allocation for the Sandbox in GB |