Skip to content

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 sandbox
const sandbox = await daytona.create({
language: 'typescript',
envVars: { NODE_ENV: 'development' }
});
// Execute commands in the sandbox
const response = await sandbox.process.executeCommand('echo "Hello, World!"');
console.log(response.result);
// Execute code in the sandbox
const response = await sandbox.process.codeRun('console.log("Hello, World!")');
console.log(response.result);
// Initialize with explicit configuration
const daytona = new Daytona({
apiKey: process.env.CUSTOM_API_KEY,
serverUrl: 'https://daytona.example.com',
target: 'us'
});
// Create a custom sandbox
const 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 features
await 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 MemberValue
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_TARGET
const daytona = new Daytona();
const sandbox = await daytona.create();
// Using explicit configuration
const 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
ParameterTypeDescription
config?DaytonaConfigConfiguration 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
ParameterTypeDefault valueDescription
params?CreateSandboxParamsundefinedParameters for Sandbox creation
timeout?number60Timeout in seconds (0 means no timeout, default is 60)
Returns

Promise<Sandbox>

The created Sandbox instance

Examples
// Create a default sandbox
const sandbox = await daytona.create();
// Create a custom sandbox
const 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
ParameterTypeDescription
sandboxIdstringThe 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
ParameterTypeDescription
sandboxIdstringThe 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
ParameterTypeDescription
workspaceIdstringThe 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
ParameterTypeDefault valueDescription
sandboxSandboxundefinedThe Sandbox to remove
timeoutnumber60Timeout 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
ParameterTypeDescription
sandboxSandboxThe Sandbox to start
timeout?numberOptional 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 start
await daytona.start(sandbox, 60);

stop()

stop(sandbox: Sandbox): Promise<void>

Stops a Sandbox.

Parameters
ParameterTypeDescription
sandboxSandboxThe 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

PropertyTypeDescription
async?booleanIf true, will not wait for the Sandbox to be ready before returning
autoStopInterval?numberAuto-stop interval in minutes (0 means disabled)
envVars?Record<string, string>Optional environment variables to set in the Sandbox
id?stringOptional Sandbox ID. If not provided, a random ID will be generated
image?stringOptional Docker image to use for the Sandbox
labels?Record<string, string>Sandbox labels
language?stringProgramming language for direct code execution
public?booleanIs the Sandbox port preview public
resources?SandboxResourcesResource allocation for the Sandbox
target?stringTarget location for the Sandbox
timeout?numberTimeout in seconds for the Sandbox to be ready (0 means no timeout)
user?stringOptional 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

PropertyTypeDescription
apiKey?stringAPI key for authentication with Daytona server
serverUrl?stringURL of the Daytona server
target?CreateWorkspaceTargetEnumTarget location for Sandboxes

SandboxResources

Resource allocation for a Sandbox.

Example

const resources: SandboxResources = {
cpu: 2,
memory: 4, // 4GB RAM
disk: 20 // 20GB disk
};

Properties

PropertyTypeDescription
cpu?numberCPU allocation for the Sandbox in cores
disk?numberDisk space allocation for the Sandbox in GB
gpu?numberGPU allocation for the Sandbox in units
memory?numberMemory allocation for the Sandbox in GB