usingvarclient=newE2BClient(apiKey);varsandbox=awaitclient.Sandboxes.CreateSandboxesAsync(newNewSandbox{TemplateID="base",Timeout=300,Secure=true,});try{// Stream output while a foreground command runs.varresult=awaitsandbox.Commands.RunAsync("echo 'Hello from E2B!'",newSandboxCommandOptions{OnStdout=Console.Write,OnStderr=Console.Error.Write,});// Start a background command with an open standard-input stream.awaitusingvarhandle=awaitsandbox.Commands.StartAsync("read line; echo \"Received: $line\"",newSandboxCommandOptions{Stdin=true,OutputBufferCapacity=8,});Console.WriteLine($"Background process ID: {handle.ProcessId}");varprocess=(awaitsandbox.Commands.ListAsync()).Single(process=>process.ProcessId==handle.ProcessId);Console.WriteLine($"Running command: {process.Command}");// Disconnect without stopping the process, then reconnect by PID.awaithandle.DisconnectAsync();awaitusingvarreconnectedHandle=awaitsandbox.Commands.ConnectAsync(handle.ProcessId,newSandboxCommandConnectOptions{OutputBufferCapacity=8});awaitreconnectedHandle.SendStdinAsync("hello\n");awaitreconnectedHandle.CloseStdinAsync();// Consume stdout and stderr asynchronously in arrival order.awaitforeach(varchunkinreconnectedHandle.ReadOutputAsync()){Console.Write(chunk.Data);}varbackgroundResult=awaitreconnectedHandle.WaitAsync();}finally{awaitclient.Sandboxes.DeleteSandboxesBySandboxIDAsync(sandbox.SandboxID);}
Templates
List available sandbox templates.
1234
usingvarclient=newE2BClient(apiKey);// List all sandbox templates available in your team.vartemplates=awaitclient.Templates.GetTemplatesAsync();
Snapshots
List sandbox snapshots for resuming paused sandboxes.
12345
usingvarclient=newE2BClient(apiKey);// List all snapshots with pagination.varsnapshots=awaitclient.Snapshots.GetSnapshotsAsync(limit:10);