libs/sdk-go/README.md
The official Go SDK for Daytona, enabling programmatic interaction with Daytona Sandboxes
package main
import (
"context"
"log"
"time"
"github.com/daytonaio/daytona/libs/sdk-go/pkg/daytona"
"github.com/daytonaio/daytona/libs/sdk-go/pkg/types"
)
func main() {
// Create a new Daytona client (uses DAYTONA_API_KEY from environment)
client, err := daytona.NewClient()
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
// Create a sandbox
params := &types.ImageParams{
SandboxBaseParams: types.SandboxBaseParams{
Language: types.CodeLanguagePython,
EnvVars: map[string]string{
"NODE_ENV": "development",
},
},
}
buildLogs := make(chan string, 100)
go func() {
for logLine := range logChan {
fmt.Printf("[BUILD] %s\n", logLine)
}
}()
// Default WaitForStart is true, but can be overriden for more async behavior
sandbox, buildLogs, err := client.Create(ctx, params,
daytona.WithTimeout(90*time.Second),
)
if err != nil {
log.Fatal(err)
}
log.Printf("✓ Created sandbox: %s (ID: %s)\n", sandbox.Name, sandbox.ID)
// Execute a command
result, err := sandbox.Process.ExecuteCommand(ctx, "echo 'Hello, World!'")
if err != nil {
log.Fatal(err)
}
log.Printf("Output: %s\n", result.Result)
// Clean up
if err := sandbox.Delete(ctx); err != nil {
log.Printf("Failed to delete: %v", err)
}
}
The SDK can be configured using environment variables or a configuration object.
Set the following environment variables:
export DAYTONA_API_KEY=your-api-key
Then create the client:
client, err := daytona.NewClient()
config := &types.DaytonaConfig{
APIKey: "your-api-key",
}
client, err := daytona.NewClientWithConfig(config)
All of the usage examples are maintained in the /examples folder. Please check it out for latest patterns of SDK usage.
The examples/ directory contains comprehensive examples demonstrating various SDK features:
To run an example:
export DAYTONA_API_KEY=your-api-key
go run examples/sandbox/main.go
go run examples/code_interpreter/main.go
go run examples/fromimage/main.go
go run examples/snapshots/withlogstreaming/snapshot_with_logs.go
Always clean up sandboxes when done:
sandbox, err := client.Create(ctx, params)
if err != nil {
log.Fatal(err)
}
defer func() {
if err := sandbox.Delete(ctx); err != nil {
log.Printf("Failed to delete sandbox: %v", err)
}
}()
// Use the sandbox...
Always check errors and handle them appropriately:
result, err := sandbox.Process.ExecuteCommand(ctx, "some-command")
if err != nil {
log.Printf("Command failed: %v", err)
return
}
if result.ExitCode != 0 {
log.Printf("Command exited with code %d: %s", result.ExitCode, result.Result)
return
}
log.Printf("Success: %s\n", result.Result)
Use contexts appropriately for timeouts and cancellation:
// Create a context with timeout for long-running operations
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
// Use the context for all operations
sandbox, err := client.Create(ctx, params)
if err != nil {
log.Fatal(err)
}
result, err := sandbox.Process.ExecuteCommand(ctx, "long-running-command")
Properties:
Volume *VolumeService - Access volume management operationsSnapshot *SnapshotService - Access snapshot management operationsMethods:
NewClient() (*Client, error) - Create a new Daytona client with default configurationNewClientWithConfig(config *types.DaytonaConfig) (*Client, error) - Create a new Daytona client with custom configurationCreate(ctx, params, options...) (*Sandbox, <-chan string, error) - Create a sandbox and returns a channel for streaming build logs
WithTimeout(time.Duration)Get(ctx, sandboxIDOrName) (*Sandbox, error) - Get a sandbox by ID or nameList(ctx, labels, page, limit) (*PaginatedSandboxes, error) - List sandboxes with paginationProperties:
FileSystem *FileSystemService - Access file system operationsGit *GitService - Access Git operationsProcess *ProcessService - Access process executionCodeInterpreter *CodeInterpreterService - Access code interpreterComputerUse *ComputerUseService - Access desktop automationName string - Sandbox nameState apiclient.SandboxState - Current stateID string - Sandbox IDToolboxClient *toolbox.APIClient - Toolbox API clientMethods:
RefreshData(ctx) error - Refresh sandbox data from APIGetUserHomeDir(ctx) (string, error) - Get user home directory pathGetWorkingDir(ctx) (string, error) - Get working directory pathSetLabels(ctx, labels) error - Set custom labelsGetPreviewLink(ctx, port) (string, error) - Get port preview URLStart(ctx) error - Start this sandbox (60s default timeout)StartWithTimeout(ctx, timeout time.Duration) error - Start with custom timeoutStop(ctx) error - Stop this sandbox (60s default timeout)StopWithTimeout(ctx, timeout time.Duration) error - Stop with custom timeoutDelete(ctx) error - Delete this sandbox (60s default timeout)DeleteWithTimeout(ctx, timeout time.Duration) error - Delete with custom timeoutArchive(ctx) error - Archive the sandbox to object storageWaitForStart(ctx, timeoutSec int) error - Wait for sandbox to startWaitForStop(ctx, timeoutSec int) error - Wait for sandbox to stopList(ctx, page, limit) (*PaginatedSnapshots, error) - List snapshots with paginationGet(ctx, nameOrID) (*Snapshot, error) - Get a snapshot by name or IDCreate(ctx, params, timeout) (*Snapshot, <-chan string, error) - Create a snapshot from an image with real-time build log streaming
Delete(ctx, snapshot) error - Delete a snapshotList(ctx) ([]*Volume, error) - List all volumesGet(ctx, name) (*Volume, error) - Get a volume by nameCreate(ctx, name) (*Volume, error) - Create a new volumeDelete(ctx, volume) error - Delete a volumeApache-2.0
For issues and questions: