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 workspaceconst workspace = await daytona.create({ language: 'typescript', envVars: { NODE_ENV: 'development' }});
// Execute commands in the workspaceconst response = await workspace.process.executeCommand('echo "Hello, World!"');console.log(response.result);
// Execute code in the workspaceconst response = await workspace.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 workspaceconst workspace = 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 workspace featuresawait workspace.git.clone('https://github.com/user/repo.git');await workspace.process.executeCommand('npm test');
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 workspace = 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
Error
- When API key or server URL is missing
Methods
create()
create(params?: CreateWorkspaceParams): Promise<Workspace>
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 | Description |
---|---|---|
params? | CreateWorkspaceParams | Parameters for Sandbox creation |
Returns
Promise<Workspace>
The created Sandbox instance
Examples
// Create a default workspaceconst workspace = await daytona.create();
// Create a custom workspaceconst params: CreateWorkspaceParams = { language: 'typescript', image: 'node:18', envVars: { NODE_ENV: 'development', DEBUG: 'true' }, resources: { cpu: 2, memory: 4 // 4GB RAM }, timeout: 300, autoStopInterval: 60};const workspace = await daytona.create(params);
get()
get(workspaceId: string): Promise<Workspace>
Gets a Sandbox by its ID.
Parameters
Parameter | Type | Description |
---|---|---|
workspaceId | string | The ID of the Sandbox to retrieve |
Returns
Promise<Workspace>
The Sandbox
Example
const workspace = await daytona.get('my-workspace-id');console.log(`Workspace state: ${workspace.instance.state}`);
getCurrentWorkspace()
getCurrentWorkspace(workspaceId: string): Promise<Workspace>
Gets the Sandbox by ID.
Parameters
Parameter | Type | Description |
---|---|---|
workspaceId | string | The ID of the Sandbox to retrieve |
Returns
Promise<Workspace>
The Sandbox
Example
const workspace = await daytona.getCurrentWorkspace('my-workspace-id');console.log(`Current workspace state: ${workspace.instance.state}`);
list()
list(): Promise<Workspace[]>
Lists all Sandboxes.
Returns
Promise<Workspace[]>
Array of Sandboxes
Example
const workspaces = await daytona.list();for (const workspace of workspaces) { console.log(`${workspace.id}: ${workspace.instance.state}`);}
remove()
remove(workspace: Workspace): Promise<void>
Removes a Sandbox.
Parameters
Parameter | Type | Description |
---|---|---|
workspace | Workspace | The Sandbox to remove |
Returns
Promise<void>
Example
const workspace = await daytona.get('my-workspace-id');await daytona.remove(workspace);
start()
start(workspace: Workspace, timeout?: number): Promise<void>
Starts a Sandbox and waits for it to be ready.
Parameters
Parameter | Type | Description |
---|---|---|
workspace | Workspace | The Sandbox to start |
timeout? | number | Optional timeout in seconds (0 means no timeout) |
Returns
Promise<void>
Example
const workspace = await daytona.get('my-workspace-id');// Wait up to 60 seconds for the workspace to startawait daytona.start(workspace, 60);
stop()
stop(workspace: Workspace): Promise<void>
Stops a Sandbox.
Parameters
Parameter | Type | Description |
---|---|---|
workspace | Workspace | The Sandbox to stop |
Returns
Promise<void>
Example
const workspace = await daytona.get('my-workspace-id');await daytona.stop(workspace);
CreateWorkspaceParams
Parameters for creating a new Sandbox.
Example
const params: CreateWorkspaceParams = { language: 'typescript', envVars: { NODE_ENV: 'development' }, resources: { cpu: 2, memory: 4 // 4GB RAM }, timeout: 300, autoStopInterval: 60 // Auto-stop after 1 hour of inactivity};const workspace = await daytona.create(params);
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? | CodeLanguage | Programming language for direct code execution |
public? | boolean | Is the Sandbox port preview public |
resources? | WorkspaceResources | 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: "local"};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 |
WorkspaceResources
Resource allocation for a Sandbox.
Example
const resources: WorkspaceResources = { 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 |
CodeLanguage
type CodeLanguage = "python" | "javascript" | "typescript";
Supported programming languages for code execution.