Skip to content

Run a Sandbox Command

Create a sandbox and run a Bash command inside it.

This example assumes using E2B; is in scope and apiKey contains your E2B API key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using var client = new E2BClient(apiKey);
var sandbox = await client.Sandboxes.CreateSandboxesAsync(
    new NewSandbox
    {
        TemplateID = "base",
        Timeout = 300,
        Secure = true,
    });

try
{
    // Stream output while a foreground command runs.
    var result = await sandbox.Commands.RunAsync(
        "echo 'Hello from E2B!'",
        new SandboxCommandOptions
        {
            OnStdout = Console.Write,
            OnStderr = Console.Error.Write,
        });

    // Start a background command with an open standard-input stream.
    await using var handle = await sandbox.Commands.StartAsync(
        "read line; echo \"Received: $line\"",
        new SandboxCommandOptions
        {
            Stdin = true,
            OutputBufferCapacity = 8,
        });

    Console.WriteLine($"Background process ID: {handle.ProcessId}");
    var process = (await sandbox.Commands.ListAsync())
        .Single(process => process.ProcessId == handle.ProcessId);
    Console.WriteLine($"Running command: {process.Command}");

    // Disconnect without stopping the process, then reconnect by PID.
    await handle.DisconnectAsync();
    await using var reconnectedHandle = await sandbox.Commands.ConnectAsync(
        handle.ProcessId,
        new SandboxCommandConnectOptions { OutputBufferCapacity = 8 });
    await reconnectedHandle.SendStdinAsync("hello\n");
    await reconnectedHandle.CloseStdinAsync();

    // Consume stdout and stderr asynchronously in arrival order.
    await foreach (var chunk in reconnectedHandle.ReadOutputAsync())
    {
        Console.Write(chunk.Data);
    }

    var backgroundResult = await reconnectedHandle.WaitAsync();
}
finally
{
    await client.Sandboxes.DeleteSandboxesBySandboxIDAsync(sandbox.SandboxID);
}