Process and Code Execution
Daytona provides process and code execution capabilities through the process module in sandboxes. For interactive terminal sessions, see Pseudo Terminal (PTY). For real-time log streaming from long-running session commands, see Log Streaming.
Code execution
Section titled “Code execution”Daytona provides methods to execute code in sandboxes. You can run code snippets in multiple languages with support for both stateless execution and stateful interpretation with persistent contexts.
- Run code (stateless): run independent code snippets where each execution starts from a clean interpreter state; inherits the sandbox language that you choose at sandbox creation. Supports Python, JavaScript, and TypeScript.
- Run code (stateful): run Python code in a persistent interpreter context with variables, imports, and state to carry across executions; available in every SDK.
Run code (stateless)
Section titled “Run code (stateless)”Run code snippets in sandboxes using stateless execution. Each invocation starts from a clean interpreter, making it ideal for independent code snippets.
from daytona import CodeRunParams
# Run Python coderesponse = sandbox.process.code_run('''def greet(name): return f"Hello, {name}!"
print(greet("Daytona"))''')
print(response.result)
# Run code with argv and environment variablesresponse = sandbox.process.code_run( 'import sys; print(f"Hello, {sys.argv[1]}!")', params=CodeRunParams(argv=["Daytona"], env={"FOO": "BAR"}), timeout=5,)print(response.result)// Run TypeScript codelet response = await sandbox.process.codeRun(`function greet(name: string): string { return \`Hello, \${name}!\`;}
console.log(greet("Daytona"));`);console.log(response.result);
// Run code with argv and environment variablesresponse = await sandbox.process.codeRun( ` console.log(\`Hello, \${process.argv[2]}!\`); console.log(\`FOO: \${process.env.FOO}\`); `, { argv: ["Daytona"], env: { FOO: "BAR" } });console.log(response.result);
// Run code with timeout (5 seconds)response = await sandbox.process.codeRun( 'setTimeout(() => console.log("Done"), 2000);', undefined, 5);console.log(response.result);# Run Python coderesponse = sandbox.process.code_run(code: <<~PYTHON) def greet(name): return f"Hello, {name}!"
print(greet("Daytona"))PYTHON
puts response.result// Run Python code (language defaults to sandbox language)result, err := sandbox.Process.CodeRun(ctx, `def greet(name): return f"Hello, {name}!"
print(greet("Daytona"))`)if err != nil { log.Fatal(err)}fmt.Println(result.Result)
// Run code with environment variablesresult, err = sandbox.Process.CodeRun(ctx, `import os; print(os.environ.get("FOO"))`, options.WithCodeRunParams(types.CodeRunParams{ Env: map[string]string{"FOO": "BAR"}, }),)if err != nil { log.Fatal(err)}fmt.Println(result.Result)
// Run code with timeout (5 seconds)result, err = sandbox.Process.CodeRun(ctx, `import time; time.sleep(2); print("Done")`, options.WithCodeRunTimeout(5*time.Second),)if err != nil { log.Fatal(err)}fmt.Println(result.Result)import io.daytona.sdk.model.ExecuteResponse;import java.util.Map;
// Run code (stateless; language matches the sandbox image)ExecuteResponse response = sandbox.process.codeRun( """ def greet(name): return f"Hello, {name}!"
print(greet("Daytona")) """);System.out.println(response.getResult());
// Run code with environment variables and timeout (seconds)response = sandbox.process.codeRun( "import os; print('FOO:', os.environ.get('FOO'))", Map.of("FOO", "BAR"), null);System.out.println(response.getResult());
response = sandbox.process.codeRun( "import time; time.sleep(2); print(\"Done\")", null, 5);System.out.println(response.getResult());curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/code-run' \ --request POST \ --header 'Content-Type: application/json' \ --data '{ "code": "def greet(name):\n return f\"Hello, {name}!\"\n\nprint(greet(\"Daytona\"))", "language": "python", "envs": { "FOO": "BAR" }, "timeout": 5}'Artifacts
Section titled “Artifacts”Stateless code_run responses can include an artifacts field. When your code produces matplotlib charts, the SDK strips chart metadata from result and returns it in the artifacts.charts field.
code = '''import matplotlib.pyplot as pltimport numpy as np
x = np.linspace(0, 10, 30)plt.plot(x, np.sin(x))plt.title("Sine wave")plt.show()'''
response = sandbox.process.code_run(code)if response.artifacts and response.artifacts.charts: chart = response.artifacts.charts[0] print(chart.type, chart.title)const response = await sandbox.process.codeRun(`import matplotlib.pyplot as pltimport numpy as np
x = np.linspace(0, 10, 30)plt.plot(x, np.sin(x))plt.title("Sine wave")plt.show()`)
if (response.artifacts?.charts?.length) { const chart = response.artifacts.charts[0] console.log(chart.type, chart.title)}Run code (stateful)
Section titled “Run code (stateful)”Run Python code with persistent state using the code interpreter. You can maintain variables and imports between calls, create isolated contexts with optional working directories, list active contexts, and stream stdout, stderr, and errors via callbacks.
from daytona import Daytona, OutputMessage, ExecutionError
def handle_stdout(message: OutputMessage): print(f"[STDOUT] {message.output}")
def handle_stderr(message: OutputMessage): print(f"[STDERR] {message.output}")
def handle_error(error: ExecutionError): print(f"[ERROR] {error.name}: {error.value}")
daytona = Daytona()sandbox = daytona.create()
# Shared default contextsandbox.code_interpreter.run_code( "counter = 1\nprint(f'Counter initialized at {counter}')", on_stdout=handle_stdout, on_stderr=handle_stderr, on_error=handle_error, timeout=60,)
# Isolated context with working directoryctx = sandbox.code_interpreter.create_context(cwd="workspace/src")try: sandbox.code_interpreter.run_code( "value = 'stored in ctx'", context=ctx, envs={"DEBUG": "1"}, ) sandbox.code_interpreter.run_code( "print(value)", context=ctx, on_stdout=handle_stdout, )finally: sandbox.code_interpreter.delete_context(ctx)
# List user-created contextsfor context in sandbox.code_interpreter.list_contexts(): print(context.id, context.cwd)import { Daytona } from '@daytona/sdk'
const daytona = new Daytona()
async function main() { const sandbox = await daytona.create()
// Shared default context await sandbox.codeInterpreter.runCode(`counter = 1print(f'Counter initialized at {counter}')`, { onStdout: (msg) => process.stdout.write(`[STDOUT] ${msg.output}`), onStderr: (msg) => process.stdout.write(`[STDERR] ${msg.output}`), timeout: 60, }, )
// Isolated context with working directory const ctx = await sandbox.codeInterpreter.createContext('workspace/src') try { await sandbox.codeInterpreter.runCode( `value = 'stored in ctx'`, { context: ctx, envs: { DEBUG: '1' } }, ) await sandbox.codeInterpreter.runCode( `print(value)`, { context: ctx, onStdout: (msg) => process.stdout.write(`[STDOUT] ${msg.output}`) }, ) } finally { await sandbox.codeInterpreter.deleteContext(ctx) }
// List user-created contexts const contexts = await sandbox.codeInterpreter.listContexts() for (const context of contexts) { console.log(context.id, context.cwd) }}
main()require 'daytona'
daytona = Daytona::Daytona.newsandbox = daytona.create
# Shared default contextsandbox.code_interpreter.run_code( <<~PYTHON, counter = 1 print(f'Counter initialized at {counter}') PYTHON on_stdout: ->(msg) { print "[STDOUT] #{msg.output}" })
# Isolated contextctx = sandbox.code_interpreter.create_contextbegin sandbox.code_interpreter.run_code("value = 'stored in ctx'", context: ctx) sandbox.code_interpreter.run_code( "print(value)", context: ctx, on_stdout: ->(msg) { print "[STDOUT] #{msg.output}" } )ensure sandbox.code_interpreter.delete_context(ctx)end// Shared default contextchannels, err := sandbox.CodeInterpreter.RunCode(ctx, "counter = 1\nprint(f'Counter initialized at {counter}')",)if err != nil { log.Fatal(err)}for msg := range channels.Stdout { fmt.Printf("[STDOUT] %s\n", msg.Text)}
// Isolated contextcwd := "workspace/src"ctxInfo, err := sandbox.CodeInterpreter.CreateContext(ctx, &cwd)if err != nil { log.Fatal(err)}contextID := ctxInfo["id"].(string)
channels, err = sandbox.CodeInterpreter.RunCode(ctx, "value = 'stored in ctx'", options.WithCustomContext(contextID), options.WithEnv(map[string]string{"DEBUG": "1"}),)if err != nil { log.Fatal(err)}for msg := range channels.Stdout { fmt.Printf("[STDOUT] %s\n", msg.Text)}
// List user-created contextscontexts, err := sandbox.CodeInterpreter.ListContexts(ctx)if err != nil { log.Fatal(err)}for _, context := range contexts { fmt.Println(context["id"], context["cwd"])}
// Clean up contexterr = sandbox.CodeInterpreter.DeleteContext(ctx, contextID)if err != nil { log.Fatal(err)}import io.daytona.sdk.RunCodeOptions;import io.daytona.toolbox.client.model.InterpreterContext;
// Default interpreter context (Python)sandbox.codeInterpreter.runCode( """ counter = 1 print(f'Counter initialized at {counter}') """, new RunCodeOptions() .setOnStdout(chunk -> System.out.print("[STDOUT] " + chunk)) .setOnStderr(chunk -> System.out.print("[STDERR] " + chunk)) .setTimeout(60));
// Context managementInterpreterContext ctx = sandbox.codeInterpreter.createContext("workspace/src");sandbox.codeInterpreter.deleteContext(ctx.getId());
for (InterpreterContext context : sandbox.codeInterpreter.listContexts()) { System.out.println(context.getId() + " " + context.getCwd());}# Create contextcurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/interpreter/context' \ --request POST \ --header 'Content-Type: application/json' \ --data '{"cwd": "workspace/src"}'
# List user-created contextscurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/interpreter/context'
# Run code in context (WebSocket endpoint)# Connect via WebSocket to:# wss://proxy.app.daytona.io/toolbox/{sandboxId}/process/interpreter/execute# Send JSON message:# {# "code": "counter = 1\nprint(f\"Counter initialized at {counter}\")",# "contextId": "your-context-id"# }
# Delete contextcurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/interpreter/context/{contextId}' \ --request DELETECommand execution
Section titled “Command execution”Daytona provides methods to execute shell commands in sandboxes. You can run commands with working directory, timeout, and environment variable options. The default timeout is 10 seconds when not specified.
Git operations assume you are operating in the sandbox user’s home directory (e.g. workspace implies /home/[username]/workspace). Use a leading / when providing absolute paths.
Execute commands
Section titled “Execute commands”Execute shell commands in sandboxes by providing the command string and optional parameters for working directory, timeout, and environment variables.
You can also use the daytona exec CLI command for quick command execution.
# Execute any shell commandresponse = sandbox.process.exec("ls -la")print(response.result)
# Setting a working directory and a timeout
response = sandbox.process.exec("sleep 3", cwd="workspace/src", timeout=5)print(response.result)
# Passing environment variables
response = sandbox.process.exec("echo $CUSTOM_SECRET", env={ "CUSTOM_SECRET": "DAYTONA" })print(response.result)// Execute any shell commandconst response = await sandbox.process.executeCommand("ls -la");console.log(response.result);
// Setting a working directory and a timeoutconst response2 = await sandbox.process.executeCommand("sleep 3", "workspace/src", undefined, 5);console.log(response2.result);
// Passing environment variablesconst response3 = await sandbox.process.executeCommand("echo $CUSTOM_SECRET", ".", { "CUSTOM_SECRET": "DAYTONA" });console.log(response3.result);# Execute any shell commandresponse = sandbox.process.exec(command: 'ls -la')puts response.result
# Setting a working directory and a timeoutresponse = sandbox.process.exec(command: 'sleep 3', cwd: 'workspace/src', timeout: 5)puts response.result
# Passing environment variablesresponse = sandbox.process.exec( command: 'echo $CUSTOM_SECRET', env: { 'CUSTOM_SECRET' => 'DAYTONA' })puts response.result// Execute any shell commandresponse, err := sandbox.Process.ExecuteCommand(ctx, "ls -la")if err != nil { log.Fatal(err)}fmt.Println(response.Result)
// Setting a working directory and a timeoutresponse, err = sandbox.Process.ExecuteCommand(ctx, "sleep 3", options.WithCwd("workspace/src"), options.WithExecuteTimeout(5*time.Second),)if err != nil { log.Fatal(err)}fmt.Println(response.Result)
// Passing environment variablesresponse, err = sandbox.Process.ExecuteCommand(ctx, "echo $CUSTOM_SECRET", options.WithCommandEnv(map[string]string{"CUSTOM_SECRET": "DAYTONA"}),)if err != nil { log.Fatal(err)}fmt.Println(response.Result)import io.daytona.sdk.model.ExecuteResponse;import java.util.Map;
// Execute any shell commandExecuteResponse response = sandbox.process.executeCommand("ls -la");System.out.println(response.getResult());
// Working directory and timeout (seconds)response = sandbox.process.executeCommand("sleep 3", "workspace/src", null, 5);System.out.println(response.getResult());
// Environment variablesresponse = sandbox.process.executeCommand( "echo $CUSTOM_SECRET", ".", Map.of("CUSTOM_SECRET", "DAYTONA"), null);System.out.println(response.getResult());# Execute any shell commanddaytona exec my-sandbox -- ls -la
# Setting a working directory and a timeoutdaytona exec my-sandbox --cwd workspace/src --timeout 5 -- sleep 3
# Passing environment variables (use shell syntax)daytona exec my-sandbox -- sh -c 'CUSTOM_SECRET=DAYTONA echo $CUSTOM_SECRET'curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/execute' \ --request POST \ --header 'Content-Type: application/json' \ --data '{ "command": "ls -la", "cwd": "workspace", "timeout": 5}'Session operations
Section titled “Session operations”Daytona provides methods to manage background process sessions in sandboxes. You can create sessions, execute commands, monitor status, and manage long-running processes.
Get session status
Section titled “Get session status”Get session status and list all sessions in a sandbox by providing the session ID.
# Check session's executed commandssession = sandbox.process.get_session(session_id)print(f"Session {session_id}:")for command in session.commands: print(f"Command: {command.command}, Exit Code: {command.exit_code}")
# List all running sessions
sessions = sandbox.process.list_sessions()for session in sessions: print(f"Session: {session.session_id}, Commands: {session.commands}")// Check session's executed commandsconst session = await sandbox.process.getSession(sessionId);console.log(`Session ${sessionId}:`);for (const command of session.commands) { console.log(`Command: ${command.command}, Exit Code: ${command.exitCode}`);}
// List all running sessionsconst sessions = await sandbox.process.listSessions();for (const session of sessions) { console.log(`Session: ${session.sessionId}, Commands: ${session.commands}`);}# Check session's executed commandssession = sandbox.process.get_session(session_id)puts "Session #{session_id}:"session.commands.each do |command| puts "Command: #{command.command}, Exit Code: #{command.exit_code}"end
# List all running sessionssessions = sandbox.process.list_sessionssessions.each do |session| puts "Session: #{session.session_id}, Commands: #{session.commands}"end// Check session's executed commandssession, err := sandbox.Process.GetSession(ctx, sessionID)if err != nil { log.Fatal(err)}fmt.Printf("Session %s:\n", sessionID)commands := session["commands"].([]any)for _, cmd := range commands { cmdMap := cmd.(map[string]any) fmt.Printf("Command: %s, Exit Code: %v\n", cmdMap["command"], cmdMap["exitCode"])}
// List all running sessionssessions, err := sandbox.Process.ListSessions(ctx)if err != nil { log.Fatal(err)}for _, sess := range sessions { fmt.Printf("Session: %s, Commands: %v\n", sess["sessionId"], sess["commands"])}import io.daytona.sdk.model.Command;import io.daytona.sdk.model.Session;
// Check session's executed commandsSession session = sandbox.process.getSession(sessionId);System.out.println("Session " + sessionId + ":");for (Command command : session.getCommands()) { System.out.println("Command: " + command.getCommand() + ", Exit Code: " + command.getExitCode());}
// List all running sessionsfor (Session s : sandbox.process.listSessions()) { System.out.println("Session: " + s.getSessionId() + ", Commands: " + s.getCommands());}# Get session infocurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session/{sessionId}'
# List all sessionscurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session'Get session command
Section titled “Get session command”Get the status of a specific command within a session, including its exit code when execution has finished. Use this to poll asynchronous session commands.
command = sandbox.process.get_session_command(session_id, command_id)print(f"Command: {command.command}, Exit Code: {command.exit_code}")const command = await sandbox.process.getSessionCommand(sessionId, commandId);console.log(`Command: ${command.command}, Exit Code: ${command.exitCode}`);command = sandbox.process.get_session_command(session_id: session_id, command_id: command_id)puts "Command: #{command.command}, Exit Code: #{command.exit_code}"command, err := sandbox.Process.GetSessionCommand(ctx, sessionID, commandID)if err != nil { log.Fatal(err)}fmt.Printf("Command: %s, Exit Code: %v\n", command["command"], command["exitCode"])import io.daytona.sdk.model.Command;
Command command = sandbox.process.getSessionCommand(sessionId, commandId);System.out.println("Command: " + command.getCommand() + ", Exit Code: " + command.getExitCode());curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session/{sessionId}/command/{commandId}'Entrypoint session
Section titled “Entrypoint session”Retrieve information about the internal entrypoint session in sandboxes. In each sandbox, the configured entrypoint command is executed inside a dedicated internal session, and you can fetch the session details (including the commands) and read its logs.
# Entrypoint session detailssession = sandbox.process.get_entrypoint_session()print(f"Entrypoint session: {session.session_id}")cmd = session.commands[0]print(f"Entrypoint command id: {cmd.id}")print(f"Command: {cmd.command}")
# Entrypoint logs (HTTP)logs = sandbox.process.get_entrypoint_logs()print(f"[STDOUT]: {logs.stdout}")print(f"[STDERR]: {logs.stderr}")
# Stream entrypoint logs (WebSocket)async def stream_entrypoint_logs(): await sandbox.process.get_entrypoint_logs_async( lambda log: print(f"[STDOUT]: {log}"), lambda log: print(f"[STDERR]: {log}"), )
# Use asyncio.run in scripts; in notebooks or async apps, await stream_entrypoint_logs() instead.asyncio.run(stream_entrypoint_logs())// Entrypoint session detailsconst session = await sandbox.process.getEntrypointSession();console.log(`Entrypoint session: ${session.sessionId}`);const cmd = session.commands[0]console.log(`Entrypoint command id: ${cmd.id}`);console.log(`Command: ${cmd.command}`);
// Entrypoint logs (HTTP)const logs = await sandbox.process.getEntrypointLogs();console.log('[STDOUT]:', logs.stdout);console.log('[STDERR]:', logs.stderr);
// Stream entrypoint logs (WebSocket)await sandbox.process.getEntrypointLogs( (chunk) => console.log('[STDOUT]:', chunk), (chunk) => console.log('[STDERR]:', chunk),);# Entrypoint session detailssession = sandbox.process.get_entrypoint_sessionputs "Entrypoint session: #{session.session_id}"cmd = session.commands.firstputs "Entrypoint command id: #{cmd.id}"puts "Command: #{cmd.command}"
# Entrypoint logs (HTTP)logs = sandbox.process.get_entrypoint_logsputs "[STDOUT]: #{logs.stdout}"puts "[STDERR]: #{logs.stderr}"
# Stream entrypoint logs (WebSocket)sandbox.process.get_entrypoint_logs_async( on_stdout: ->(log) { puts "[STDOUT]: #{log}" }, on_stderr: ->(log) { puts "[STDERR]: #{log}" })// Entrypoint session detailsinfo, err := sandbox.Process.GetEntrypointSession(ctx)if err != nil { log.Fatal(err)}fmt.Printf("Entrypoint session: %s\n", info.GetSessionId())cmds := info.GetCommands()cmd := cmds[0]fmt.Printf("Entrypoint command id: %s\n", cmd.GetId())fmt.Printf("Command: %s\n", cmd.GetCommand())
// Entrypoint logs (HTTP)logs, err := sandbox.Process.GetEntrypointLogs(ctx)if err != nil { log.Fatal(err)}fmt.Println(logs)
// Stream entrypoint logs (WebSocket)stdout := make(chan string, 100)stderr := make(chan string, 100)go func() { for msg := range stderr { log.Printf("[STDERR]: %s", msg) }}()go func() { if err := sandbox.Process.GetEntrypointLogsStream(ctx, stdout, stderr); err != nil { log.Println("Entrypoint log stream error:", err) }}()for msg := range stdout { fmt.Printf("[STDOUT]: %s\n", msg)}import io.daytona.sdk.model.Command;import io.daytona.sdk.model.Session;import io.daytona.sdk.model.SessionCommandLogsResponse;
// Entrypoint session detailsSession session = sandbox.process.getEntrypointSession();System.out.println("Entrypoint session: " + session.getSessionId());Command cmd = session.getCommands().get(0);System.out.println("Entrypoint command id: " + cmd.getId());System.out.println("Command: " + cmd.getCommand());
// Entrypoint logs (HTTP)SessionCommandLogsResponse logs = sandbox.process.getEntrypointLogs();System.out.println("[STDOUT]: " + logs.getStdout());System.out.println("[STDERR]: " + logs.getStderr());
// Stream entrypoint logs (WebSocket)sandbox.process.getEntrypointLogs( chunk -> System.out.println("[STDOUT]: " + chunk), chunk -> System.out.println("[STDERR]: " + chunk));# Get entrypoint session detailscurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session/entrypoint'
# Get entrypoint logs (HTTP)curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session/entrypoint/logs'
# Follow entrypoint logs in real-time (WebSocket)# wss://proxy.app.daytona.io/toolbox/{sandboxId}/process/session/entrypoint/logs?follow=trueExecute interactive commands
Section titled “Execute interactive commands”Execute interactive commands in sessions. You can send input to running commands that expect user interaction, such as confirmations or interactive tools like database CLIs and package managers.
import asynciofrom daytona import SessionExecuteRequest
session_id = "interactive-session"sandbox.process.create_session(session_id)
# Execute command that requires confirmationcommand = sandbox.process.execute_session_command( session_id, SessionExecuteRequest( command='pip uninstall requests', run_async=True, ),)
# Stream logs asynchronouslylogs_task = asyncio.create_task( sandbox.process.get_session_command_logs_async( session_id, command.cmd_id, lambda log: print(f"[STDOUT]: {log}"), lambda log: print(f"[STDERR]: {log}"), ))
await asyncio.sleep(1)# Send input to the commandsandbox.process.send_session_command_input(session_id, command.cmd_id, "y")
# Wait for logs to completeawait logs_taskconst sessionId = 'interactive-session'await sandbox.process.createSession(sessionId)
// Execute command that requires confirmationconst command = await sandbox.process.executeSessionCommand(sessionId, { command: 'pip uninstall requests', runAsync: true,})
// Stream logs asynchronouslyconst logPromise = sandbox.process.getSessionCommandLogs( sessionId, command.cmdId!, (stdout) => console.log('[STDOUT]:', stdout), (stderr) => console.log('[STDERR]:', stderr),)
await new Promise((resolve) => setTimeout(resolve, 1000))// Send input to the commandawait sandbox.process.sendSessionCommandInput(sessionId, command.cmdId!, 'y')
// Wait for logs to completeawait logPromisesession_id = "interactive-session"sandbox.process.create_session(session_id)
# Execute command that requires confirmationinteractive_command = sandbox.process.execute_session_command( session_id: session_id, req: Daytona::SessionExecuteRequest.new( command: 'pip uninstall requests', run_async: true ))
# Wait a moment for the command to startsleep 1
# Send input to the commandsandbox.process.send_session_command_input( session_id: session_id, command_id: interactive_command.cmd_id, data: "y")
# Get logs for the interactive command asynchronouslysandbox.process.get_session_command_logs_async( session_id: session_id, command_id: interactive_command.cmd_id, on_stdout: ->(log) { puts "[STDOUT]: #{log}" }, on_stderr: ->(log) { puts "[STDERR]: #{log}" })sessionID := "interactive-session"err := sandbox.Process.CreateSession(ctx, sessionID)if err != nil { log.Fatal(err)}
// Execute command that requires confirmationresult, err := sandbox.Process.ExecuteSessionCommand(ctx, sessionID, "pip uninstall requests", true, false)if err != nil { log.Fatal(err)}cmdID := result["cmdId"].(string)
// Stream logs asynchronouslystdout := make(chan string)stderr := make(chan string)
go func() { err := sandbox.Process.GetSessionCommandLogsStream(ctx, sessionID, cmdID, stdout, stderr) if err != nil { log.Println("Log stream error:", err) }}()
time.Sleep(1 * time.Second)
// Note: SendSessionCommandInput is not available in Go SDK// Use the API endpoint directly for sending input
// Read logsfor msg := range stdout { fmt.Printf("[STDOUT]: %s\n", msg)}import io.daytona.sdk.model.SessionExecuteRequest;import io.daytona.sdk.model.SessionExecuteResponse;
String sessionId = "interactive-session";sandbox.process.createSession(sessionId);
SessionExecuteResponse command = sandbox.process.executeSessionCommand( sessionId, new SessionExecuteRequest("pip uninstall requests", true));String cmdId = command.getCmdId();
Thread logThread = new Thread(() -> sandbox.process.getSessionCommandLogs( sessionId, cmdId, log -> System.out.println("[STDOUT]: " + log), log -> System.out.println("[STDERR]: " + log)));logThread.start();
Thread.sleep(1000);sandbox.process.sendSessionCommandInput(sessionId, cmdId, "y");logThread.join();# Create sessioncurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session' \ --request POST \ --header 'Content-Type: application/json' \ --data '{"sessionId": "interactive-session"}'
# Execute session commandcurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session/{sessionId}/exec' \ --request POST \ --header 'Content-Type: application/json' \ --data '{ "command": "pip uninstall requests", "runAsync": true}'
# Send input to commandcurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session/{sessionId}/command/{commandId}/input' \ --request POST \ --header 'Content-Type: application/json' \ --data '{ "data": "y"}'
# Get command logs (append ?follow=true for WebSocket streaming)curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session/{sessionId}/command/{commandId}/logs'Resource management
Section titled “Resource management”Use sessions for long-running operations, clean up sessions after execution, and handle exceptions properly.
# Python - Clean up sessionsession_id = "long-running-cmd"try: sandbox.process.create_session(session_id) session = sandbox.process.get_session(session_id) # Do work...finally: sandbox.process.delete_session(session.session_id)// TypeScript - Clean up sessionconst sessionId = "long-running-cmd";try { await sandbox.process.createSession(sessionId); const session = await sandbox.process.getSession(sessionId); // Do work...} finally { await sandbox.process.deleteSession(session.sessionId);}# Ruby - Clean up sessionsession_id = 'long-running-cmd'begin sandbox.process.create_session(session_id) session = sandbox.process.get_session(session_id) # Do work...ensure sandbox.process.delete_session(session.session_id)end// Go - Clean up sessionsessionID := "long-running-cmd"err := sandbox.Process.CreateSession(ctx, sessionID)if err != nil { log.Fatal(err)}defer sandbox.Process.DeleteSession(ctx, sessionID)
session, err := sandbox.Process.GetSession(ctx, sessionID)if err != nil { log.Fatal(err)}// Do work...import io.daytona.sdk.model.Session;
// Clean up sessionString sessionId = "long-running-cmd";try { sandbox.process.createSession(sessionId); Session session = sandbox.process.getSession(sessionId); // Do work...} finally { sandbox.process.deleteSession(sessionId);}# Create sessioncurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session' \ --request POST \ --header 'Content-Type: application/json' \ --data '{"sessionId": "long-running-cmd"}'
# Delete session when donecurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/session/{sessionId}' \ --request DELETEError handling
Section titled “Error handling”Handle process exceptions properly, log error details for debugging, and use try-catch blocks for error handling.
from daytona import DaytonaError
try: response = sandbox.process.code_run("invalid python code") if response.exit_code != 0: print(f"Exit code: {response.exit_code}") print(f"Error output: {response.result}")except DaytonaError as e: print(f"Execution failed: {e}")import { DaytonaError } from '@daytona/sdk'
try { const response = await sandbox.process.codeRun("invalid typescript code"); if (response.exitCode !== 0) { console.error("Exit code:", response.exitCode); console.error("Error output:", response.result); }} catch (e) { if (e instanceof DaytonaError) { console.error("Execution failed:", e); }}begin response = sandbox.process.code_run(code: 'invalid python code') if response.exit_code != 0 puts "Exit code: #{response.exit_code}" puts "Error output: #{response.result}" endrescue StandardError => e puts "Execution failed: #{e}"endresult, err := sandbox.Process.CodeRun(ctx, "invalid python code")if err != nil { fmt.Println("Execution failed:", err)}if result != nil && result.ExitCode != 0 { fmt.Println("Exit code:", result.ExitCode) fmt.Println("Error output:", result.Result)}import io.daytona.sdk.exception.DaytonaException;import io.daytona.sdk.model.ExecuteResponse;
try { ExecuteResponse response = sandbox.process.codeRun("invalid python code"); if (response.getExitCode() != null && response.getExitCode() != 0) { System.out.println("Exit code: " + response.getExitCode()); System.out.println("Error output: " + response.getResult()); }} catch (DaytonaException e) { System.out.println("Execution failed: " + e.getMessage());}# API responses include exitCode field for error handlingcurl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/process/execute' \ --request POST \ --header 'Content-Type: application/json' \ --data '{ "command": "python3 -c \"invalid python code\""}'
# Response includes:# {# "result": "",# "exitCode": 1# }