Skip to content

OpenSandbox SDK for C# ​

A C# SDK for low-level interaction with OpenSandbox. It provides the ability to create, manage, and interact with secure sandbox environments, including executing shell commands, managing files, and reading resource metrics.

Installation ​

NuGet ​

bash
dotnet add package Alibaba.OpenSandbox

Package Manager ​

powershell
Install-Package Alibaba.OpenSandbox

Quick Start ​

Run this example in a .NET 8+ console project with implicit usings enabled. It creates a sandbox, runs a shell command, and releases remote and local resources.

TIP

Before running this example, ensure the OpenSandbox service is running. See the Getting Started guide for startup instructions.

csharp
using OpenSandbox;
using OpenSandbox.Config;
using OpenSandbox.Core;

var config = new ConnectionConfig(new ConnectionConfigOptions
{
    Domain = "api.opensandbox.io",
    ApiKey = "your-api-key",
    // Protocol = ConnectionProtocol.Https,
    // RequestTimeoutSeconds = 60,
});

try
{
    await using var sandbox = await Sandbox.CreateAsync(new SandboxCreateOptions
    {
        ConnectionConfig = config,
        Image = "ubuntu",
        TimeoutSeconds = 10 * 60,
    });

    try
    {
        var execution = await sandbox.Commands.RunAsync("echo 'Hello Sandbox!'");
        Console.WriteLine(execution.Logs.Stdout.FirstOrDefault()?.Text);
    }
    finally
    {
        await sandbox.KillAsync();
    } // await using releases the client even if KillAsync fails.
}
catch (SandboxException ex)
{
    Console.Error.WriteLine($"Sandbox Error: [{ex.Error.Code}] {ex.Error.Message}");
    Console.Error.WriteLine($"Request ID: {ex.RequestId}");
}

Lifecycle Hooks ​

Set Lifecycle in SandboxCreateOptions. PreStart completes before the entrypoint starts, while Periodic hooks run on their schedules after startup.

csharp
using OpenSandbox.Models;

await using var sandbox = await Sandbox.CreateAsync(new SandboxCreateOptions
{
    ConnectionConfig = config,
    Image = "ubuntu:24.04",
    Lifecycle = new SandboxLifecycle
    {
        PreStart = new LifecycleHook
        {
            Command = new[] { "sh", "-c", "echo ready > /tmp/prestart.done" },
            TimeoutSeconds = 120,
        },
        Periodic = new[]
        {
            new PeriodicLifecycleHook
            {
                Name = "checkpoint",
                Schedule = "@every 5m",
                Command = new[] { "sh", "-c", "date -u >> /tmp/checkpoints.log" },
                TimeoutSeconds = 120,
            },
        },
    },
});

The Server validates TimeoutSeconds; PreStart accepts 1–10800 seconds, while Periodic accepts 1–300 seconds. Both default to 60 seconds when omitted. See Lifecycle Hooks for timing, failure behavior, and provider limitations.

Idle policy (auto-pause and wake-on-access) ​

SandboxLifecycle.IdlePolicy (OSEP-0024, fast-sandbox only) tells the control plane to auto-pause the sandbox after IdleTimeoutSeconds (30–86400) of continuously idle ingress-observed traffic, and to wake it on the next request. It is control-plane state: it never reaches the guest, is accepted in template mode (SandboxCreateFromTemplateOptions.IdlePolicy), and is rejected by non-fast-sandbox backends. Omitting it preserves non-automated behavior exactly.

csharp
var sandbox = await Sandbox.CreateAsync(new SandboxCreateOptions
{
    ConnectionConfig = config,
    Image = "ubuntu:24.04",
    Lifecycle = new SandboxLifecycle
    {
        IdlePolicy = new IdlePolicy
        {
            IdleTimeoutSeconds = 300,
            OnIdle = OnIdleAction.Pause,
            WakeOnAccess = true
        }
    }
});

ActiveProbe lets the application veto a pause while real work runs without producing ingress traffic: declare an endpoint answering {"active": bool, "message"?: string} and the server polls it when the sandbox is otherwise idle; a busy answer resets the idle clock. Invalid policies fail fast client-side through IdlePolicy.Validate().

Renewal policy (renew-on-access) ​

SandboxLifecycle.RenewalPolicy.ExtendSeconds (OSEP-0009) opts the sandbox into automatic expiration extension on observed access: each proxied or gateway-observed request moves the expiration to max(now + ExtendSeconds, current expiresAt). It is control-plane state and never reaches the guest. Requires the server operator to enable [renew_intent] — see Renew-on-Access. The deprecated extensions["access.renew.extend.seconds"] key remains supported through 1.x and is removed in 2.0.

csharp
var sandbox = await Sandbox.CreateAsync(new SandboxCreateOptions
{
    ConnectionConfig = config,
    Image = "ubuntu:24.04",
    Lifecycle = new SandboxLifecycle
    {
        RenewalPolicy = new RenewalPolicy { ExtendSeconds = 3600 }
    }
});

Wake retry ​

A request to a paused sandbox may arrive while its restore is still in progress. The gateway answers 503 with the OpenSandbox-Wake: expired header; the HTTP transport recognizes it and resends the original request after the Retry-After delay (safe for any method — the marker proves the request was never forwarded). The wake budget defaults to 5 attempts / 60 seconds across all wake cycles; when it is exhausted the SDK throws SandboxWakeTimeoutException (carrying Attempts and Waited) while the server-side resume continues in the background. Bare 503 responses follow the existing rules untouched. Tune it on the connection config: WakeRetryEnabled (default true), WakeMaxAttempts (5), WakeOverallDeadlineSeconds (60, non-positive for unbounded). SSE and file stream transports bypass wake retry.

Usage Examples ​

The snippets below use config and a live sandbox from the quick start. Run them before its cleanup block, with using OpenSandbox.Models; at the top of the file. Creation examples are alternatives. await using releases local clients; call KillAsync() to terminate sandboxes that are no longer needed.

1. Lifecycle Management ​

Manage the sandbox lifecycle, including renewal, pausing, and resuming.

csharp
var info = await sandbox.GetInfoAsync();
Console.WriteLine($"State: {info.Status.State}");
Console.WriteLine($"Created: {info.CreatedAt}");
Console.WriteLine($"Expires: {info.ExpiresAt}"); // null when manual cleanup mode is used

await sandbox.PauseAsync();

using var pauseBudget = new CancellationTokenSource(TimeSpan.FromMinutes(2));
while (true)
{
    var current = await sandbox.GetInfoAsync(pauseBudget.Token);
    if (current.Status.State == SandboxStates.Paused) break;
    if (current.Status.State == "Failed")
        throw new InvalidOperationException(current.Status.Message);
    await Task.Delay(1000, pauseBudget.Token);
}

// Resume returns a fresh local handle.
await using var resumed = await sandbox.ResumeAsync();

// Renew: expiresAt = now + timeoutSeconds
await resumed.RenewAsync(30 * 60);

Pause is asynchronous and runtime-dependent. See Pause and Resume.

Create a non-expiring sandbox by setting ManualCleanup = true:

csharp
var manual = await Sandbox.CreateAsync(new SandboxCreateOptions
{
    ConnectionConfig = config,
    Image = "ubuntu",
    ManualCleanup = true,
});

INFO

TimeoutSeconds = null uses the default TTL. Set ManualCleanup = true to disable automatic expiration.

Connect to an Existing Sandbox ​

Use ConnectAsync when you already have a sandbox ID and need a new SDK instance bound to it.

csharp
await using var connected = await Sandbox.ConnectAsync(new SandboxConnectOptions
{
    SandboxId = "existing-sandbox-id",
    ConnectionConfig = config
});

2. Custom Health Check ​

Resolving an endpoint confirms that a route exists; it does not confirm that the application on that port is healthy. For service readiness, make a bounded request to the application's health endpoint and include the returned endpoint headers.

Define custom logic to determine whether the sandbox is ready/healthy.

csharp
using var http = new HttpClient { Timeout = TimeSpan.FromSeconds(2) };
await using var sandbox = await Sandbox.CreateAsync(new SandboxCreateOptions
{
    ConnectionConfig = config,
    Image = "nginx:latest",
    Entrypoint = new[] { "nginx", "-g", "daemon off;" },
    HealthCheck = async (sbx) =>
    {
        var ep = await sbx.GetEndpointAsync(80);
        using var request = new HttpRequestMessage(HttpMethod.Get, await sbx.GetEndpointUrlAsync(80));
        foreach (var header in ep.Headers)
            request.Headers.TryAddWithoutValidation(header.Key, header.Value);
        try
        {
            using var response = await http.SendAsync(request);
            return response.StatusCode == System.Net.HttpStatusCode.OK;
        }
        catch (HttpRequestException) { return false; }
        catch (TaskCanceledException) { return false; }
    },
});

3. Command Execution & Streaming ​

Execute commands and handle output streams in real-time.

csharp
using OpenSandbox.Models;

var handlers = new ExecutionHandlers
{
    OnStdout = msg => { Console.WriteLine($"STDOUT: {msg.Text}"); return Task.CompletedTask; },
    OnStderr = msg => { Console.Error.WriteLine($"STDERR: {msg.Text}"); return Task.CompletedTask; },
    OnExecutionComplete = c => { Console.WriteLine($"Finished in {c.ExecutionTimeMs}ms"); return Task.CompletedTask; },
};

await sandbox.Commands.RunAsync(
    "for i in 1 2 3; do echo \"Count $i\"; sleep 0.2; done",
    handlers: handlers
);

To execute a native program without shell parsing, pass an argument list. On Linux, this example prints literal $HOME and keeps hello world as one argument:

csharp
await sandbox.Commands.RunAsync(new[] { "printf", "%s\n", "$HOME", "hello world" });

Native argv execution requires an updated execd. See command execution modes for executable lookup and platform behavior.

Background commands ​

Poll status and incremental logs. Command timeout is separate from sandbox TTL. Each log read returns at most 16 MiB; follow the returned cursor until it stops advancing so a longer log is fully drained.

csharp
var execution = await sandbox.Commands.RunAsync(
    "for i in 1 2 3; do echo step-$i; sleep 1; done",
    options: new RunCommandOptions
    {
        Background = true,
        TimeoutSeconds = 30,
    });

var commandId = execution.Id ?? throw new InvalidOperationException("No command ID returned");
long cursor = 0;
using var budget = new CancellationTokenSource(TimeSpan.FromSeconds(45));
try
{
    while (true)
    {
        var status = await sandbox.Commands.GetCommandStatusAsync(commandId, budget.Token);
        while (true)
        {
            var page = await sandbox.Commands.GetBackgroundCommandLogsAsync(commandId, cursor, budget.Token);
            Console.Write(page.Content);
            var next = page.Cursor ?? cursor;
            if (page.Content.Length == 0 || next == cursor) break;
            cursor = next;
        }
        if (status.Running == false)
        {
            if (status.ExitCode != 0)
                throw new InvalidOperationException($"Command failed: {status.ExitCode}, {status.Error}");
            break;
        }
        await Task.Delay(500, budget.Token);
    }
}
catch (OperationCanceledException) when (budget.IsCancellationRequested)
{
    await sandbox.Commands.InterruptAsync(commandId);
    throw;
}

Persistent shell sessions ​

A Bash session preserves shell variables and the working directory across commands.

csharp
var sessionId = await sandbox.Commands.CreateSessionAsync(
    new CreateSessionOptions { WorkingDirectory = "/tmp" });
try
{
    await sandbox.Commands.RunInSessionAsync(sessionId, "export DEMO=hello");
    var result = await sandbox.Commands.RunInSessionAsync(sessionId, "echo \"$DEMO\"; pwd");
    foreach (var message in result.Logs.Stdout) Console.Write(message.Text);
}
finally
{
    await sandbox.Commands.DeleteSessionAsync(sessionId);
}

Persistent environment variables ​

Set environment variables that the runtime injects into every subsequent command and session — without hand-writing shell escaping against the sandbox env file.

csharp
await sandbox.Commands.SetEnvAsync("MY_TOKEN", "it's a safe value");

Keys must match [A-Za-z_][A-Za-z0-9_]*. Values without a single quote are stored verbatim; values containing a single quote use the env file's double-quoted form, in which shell-style $NAME sequences may be expanded when the runtime loads the file. The env file is append-only: the last write for a key wins. Throws SandboxException if the sandbox fails to persist the variable.

For filesystem/process isolation within a sandbox, see Isolation Sessions. These are separate from Bash sessions.

4. File Operations ​

Manage files and directories, including read, write, list/search, and delete.

csharp
await sandbox.Files.CreateDirectoriesAsync(new[]
{
    new CreateDirectoryEntry { Path = "/tmp/demo", Mode = 755 }
});

await sandbox.Files.WriteFilesAsync(new[]
{
    new WriteEntry { Path = "/tmp/demo/hello.txt", Data = "Hello World", Mode = 644 }
});

var content = await sandbox.Files.ReadFileAsync("/tmp/demo/hello.txt");
Console.WriteLine($"Content: {content}");

var entries = await sandbox.Files.ListDirectoryAsync("/tmp/demo", depth: 1);
foreach (var entry in entries) Console.WriteLine(entry.Path);

var files = await sandbox.Files.SearchAsync(new SearchEntry { Path = "/tmp/demo", Pattern = "*.txt" });
foreach (var file in files)
{
    Console.WriteLine(file.Path);
}

await sandbox.Files.DeleteFilesAsync(new[] { "/tmp/demo/hello.txt" });
await sandbox.Files.DeleteDirectoriesAsync(new[] { "/tmp/demo" });

For binary data, use byte[] or Stream in WriteEntry.Data, and download with ReadBytesAsync() or ReadBytesStreamAsync(). Read options support partial downloads.

Use ReadBytesDetailedAsync or ReadBytesStreamDetailedAsync for the body together with HTTP status and range metadata; unknown sizes use -1. Check IsPartial before appending a resumed download, and dispose a detailed stream body with await using. These extension methods are in OpenSandbox; custom adapters must implement IDownloadSandboxFiles or the methods throw NotSupportedException.

For content-encoded responses, ContentLength and ContentRange describe the encoded HTTP representation when available. The default HTTP client decodes the body and can remove the encoding and length headers; a partial response reports TotalSize as -1 when neither Content-Length nor an explicit identity encoding is visible.

File operations with an explicit identity ​

Bind a filesystem client to a Linux UID and GID:

csharp
var files = sandbox.FilesWithIdentity(1001, 2000);

The bound client exposes the usual file operations and shares the sandbox connection, authentication, and endpoint routing headers. The existing file client retains its default identity. Select the UID/GID separately for command execution.

This requires Linux and an Execd version that supports filesystem execution identity. Unsupported servers return an error; requests never fall back to the default filesystem routes. Keep sandbox credentials and identity selection in the trusted backend. Linux filesystem permissions apply; this feature does not create a tenant isolation boundary. See Execd for permission and workspace provisioning details.

5. Endpoints ​

GetEndpointAsync() returns an endpoint without a scheme (for example "localhost:44772"). Use GetEndpointUrlAsync() if you want a ready-to-use absolute URL.

csharp
var endpoint = await sandbox.GetEndpointAsync(44772);
Console.WriteLine(endpoint.EndpointAddress);

var url = await sandbox.GetEndpointUrlAsync(44772);
Console.WriteLine(url); // e.g., "http://localhost:44772"

Forward endpoint.Headers on requests to the sandbox service, including any secure-access credentials. The health-check example above shows an HTTP request to an application listening on the target port.

6. Sandbox Management (Admin) ​

Use SandboxManager for administrative tasks and finding existing sandboxes. When listing sandboxes, snapshots, or templates, Page and PageSize must be positive if set; leave them unset to use the server defaults. Non-positive values throw InvalidArgumentException before any request is sent.

csharp
await using var manager = SandboxManager.Create(new SandboxManagerOptions
{
    ConnectionConfig = config
});

var list = await manager.ListSandboxInfosAsync(new SandboxFilter
{
    States = new[] { SandboxStates.Running },
    Page = 1, // First page only; increase for subsequent pages.
    PageSize = 10
});

foreach (var s in list.Items)
{
    Console.WriteLine(s.Id);
}

Resource metrics ​

Read current sandbox resource usage with await sandbox.Metrics.GetMetricsAsync(). This is separate from SDK creation telemetry.

Snapshots, templates, and metadata ​

OperationPublic API
Snapshot a sandboxsandbox.CreateSnapshotAsync or manager.CreateSnapshotAsync
Inspect/list/delete snapshotsmanager.GetSnapshotAsync, ListSnapshotsAsync, DeleteSnapshotAsync
Restore a snapshotSandbox.CreateAsync with SnapshotId and no Image/Entrypoint
Manage Fsb templatesmanager.CreateTemplateAsync, GetTemplateAsync, ListTemplatesAsync, DeleteTemplateAsync
Create from a published templateSandbox.CreateFromTemplateAsync with SandboxCreateFromTemplateOptions
Patch metadatasandbox.PatchMetadataAsync or manager.PatchSandboxMetadataAsync

Poll snapshot status before restoring. Template builds are asynchronous; wait for Succeeded before use. Template-backed creation requires TimeoutSeconds and inherits workload configuration from the published template. Metadata patch values add/replace keys; null deletes a key. See the lifecycle contract for backend constraints.

Snapshot mode selects what the snapshot persists:

  • SnapshotModes.Full (default): the most complete capture the runtime supports. On the fast-sandbox backend that includes memory and process state, and restoring resumes the sandbox in place. Docker and Kubernetes snapshots capture the filesystem only — running processes are not restored there.
  • SnapshotModes.RootfsOnly: only the writable filesystem. Restoring boots a fresh sandbox from the disk — the entrypoint re-runs, and resume moves from milliseconds (fast-sandbox in-place restore) to seconds (cold boot). The artifact is smaller and CPU-portable. Requires the fast-sandbox backend; other backends reject the mode before any capture starts, and an unqualified fast-sandbox node fails the snapshot instead of silently producing a full artifact.

SnapshotInfo.Mode reports the artifact class of an existing snapshot; snapshots created before the field existed report SnapshotModes.Full.

Snapshot support depends on the runtime and server configuration. Renew the source sandbox first if its remaining TTL may expire during snapshot creation. This example waits up to 15 minutes and retains the snapshot for reuse:

csharp
await using var manager = SandboxManager.Create(new SandboxManagerOptions { ConnectionConfig = config });
var snapshot = await sandbox.CreateSnapshotAsync("demo");
Console.WriteLine($"Snapshot: {snapshot.Id}");
using var budget = new CancellationTokenSource(TimeSpan.FromMinutes(15));
while (true)
{
    snapshot = await manager.GetSnapshotAsync(snapshot.Id, budget.Token);
    if (snapshot.Status.State == "Ready") break;
    if (snapshot.Status.State == "Failed")
        throw new InvalidOperationException(snapshot.Status.Message);
    await Task.Delay(2000, budget.Token);
}
await using var restored = await Sandbox.CreateAsync(new SandboxCreateOptions
{
    SnapshotId = snapshot.Id,
    ConnectionConfig = config,
});
try { Console.WriteLine(restored.Id); }
finally { await restored.KillAsync(); }
// When no longer needed: await manager.DeleteSnapshotAsync(snapshot.Id);

Create a rootfs-only snapshot for workspace persistence (fast-sandbox backend only). Restoring is a cold boot, so budget seconds instead of milliseconds for readiness:

csharp
var rootfsSnapshot = await sandbox.CreateSnapshotWithModeAsync("workspace-state", SnapshotModes.RootfsOnly);
// ... poll manager.GetSnapshotAsync(rootfsSnapshot.Id) until Ready, then:
await using var rebooted = await Sandbox.CreateAsync(new SandboxCreateOptions
{
    SnapshotId = rootfsSnapshot.Id,
    ConnectionConfig = config,
});
// Files written before the snapshot are present; processes were not preserved
// and the entrypoint re-ran on boot.

Use an existing template after its build reaches Succeeded:

csharp
await using var templated = await Sandbox.CreateFromTemplateAsync(new SandboxCreateFromTemplateOptions
{
    TemplateId = "your-published-template-id",
    TimeoutSeconds = 600,
    ConnectionConfig = config,
});

Add/replace a metadata key and remove another:

csharp
await sandbox.PatchMetadataAsync(new Dictionary<string, string?>
{
    ["project"] = "demo",
    ["obsolete-key"] = null,
});

Configuration ​

1. Connection Configuration ​

The ConnectionConfig class manages API server connection settings.

An explicit OPEN-SANDBOX-API-KEY entry in Headers takes precedence over ApiKey and its environment fallback, regardless of header-name casing. A present-but-empty entry is preserved rather than replaced with the configured key. This also applies to SDK telemetry requests.

Transfer timeouts

RequestTimeoutSeconds bounds the wait for response headers, not the whole transfer. The deadline starts with the request (so the connect phase is bounded too), is cleared while the request body is written, and is re-armed once the body of the current hop has been sent: a healthy upload or download that keeps making progress is never cut off by it. A download that gets no response headers, or an upload that gets none within the timeout after its body has been sent, fails with a TaskCanceledException carrying a TimeoutException instead of hanging (the Go SDK's ResponseHeaderTimeout contract); a caller cancellation still surfaces as an OperationCanceledException with the caller's token. Streaming (SSE) calls are not capped as a whole either: they fail when the response headers or the next stream bytes take longer than max(RequestTimeoutSeconds, 30s). All other calls keep the whole-call request timeout.

A request body that never finishes being sent, and a response body that stops making progress after the headers have arrived, are bounded only by the CancellationToken you pass to the call, so pass one when a transfer needs an end-to-end cap. The upload guarantee assumes the HTTP handler streams the request body (for example SocketsHttpHandler on .NET 5+): the default HttpClientHandler on .NET Framework buffers the request body before sending it, so a slow but progressing upload can still count against the header timeout there.

A 307/308 redirect resends the request body once per hop. A seekable upload source (FileStream, MemoryStream, ...) is rewound and resent in full; a non-seekable source cannot be replayed without buffering it, so the upload fails with a clear error instead of sending an empty file part to the redirected endpoint.

ParameterDescriptionDefaultEnvironment Variable
ApiKeyAPI key for authenticationOptionalOPEN_SANDBOX_API_KEY
DomainSandbox service domain (host[:port])localhost:8080OPEN_SANDBOX_DOMAIN
ProtocolHTTP protocol (Http/Https)Http-
RequestTimeoutSecondsRequest timeout applied to SDK HTTP calls; command, code and isolated-session streams (SSE) wait at most max(RequestTimeoutSeconds, 30s) for the response and between stream bytes30-
HeadersExtra headers applied to every request{}-
DisableMetricsDisable SDK create-latency telemetry (see SDK Telemetry)falseOPENSANDBOX_DISABLE_METRICS
csharp
using OpenSandbox.Config;

// 1. Basic configuration
var config = new ConnectionConfig(new ConnectionConfigOptions
{
    Domain = "api.opensandbox.io",
    ApiKey = "your-key",
    RequestTimeoutSeconds = 60,
});

// 2. Advanced: custom headers
var config2 = new ConnectionConfig(new ConnectionConfigOptions
{
    Domain = "api.opensandbox.io",
    ApiKey = "your-key",
    Headers = new Dictionary<string, string>
    {
        ["X-Custom-Header"] = "value"
    },
});

SDK Telemetry

Sandbox.CreateAsync reports create latency to POST /v1/metrics/events by default. Set ConnectionConfigOptions.DisableMetrics = true or export OPENSANDBOX_DISABLE_METRICS=1 to opt out. See SDK Telemetry.

The C# Code Interpreter SDK inherits the sandbox's connection configuration. With the deprecated UseServerProxy = true (maintenance mode), context requests and streamed code execution include the configured API key in OPEN-SANDBOX-API-KEY for server authentication. In direct mode, the SDK does not attach this key to code execution requests.

2. SDK Logging ​

The SDK uses Microsoft.Extensions.Logging abstractions. SdkDiagnosticsOptions configures local logging; it does not retrieve remote sandbox diagnostic logs or events. For remote diagnostics, use GetDiagnosticContextAsync, GetDiagnosticLogsAsync, and GetDiagnosticEventsAsync on SandboxManager or Sandbox; see SDK diagnostics. Client Pool and built-in pool warmup tracing are not currently available in C#. Install Microsoft.Extensions.Logging.Console to use AddConsole() below.

csharp
using Microsoft.Extensions.Logging;
using OpenSandbox.Config;

using var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.SetMinimumLevel(LogLevel.Debug);
    builder.AddConsole();
});

var sandbox = await Sandbox.CreateAsync(new SandboxCreateOptions
{
    Image = "python:3.11",
    ConnectionConfig = new ConnectionConfig(),
    Diagnostics = new SdkDiagnosticsOptions
    {
        LoggerFactory = loggerFactory
    }
});

3. Sandbox Creation Configuration ​

Sandbox.CreateAsync() allows configuring the sandbox environment.

ParameterDescriptionDefault
ImageDocker image to useOne of image or snapshot ID
TimeoutSecondsAutomatic termination timeout (server-side TTL)10 minutes
EntrypointContainer entrypoint command["tail","-f","/dev/null"]
ResourceCPU and memory limits (string map){"cpu":"1","memory":"2Gi"}
EnvEnvironment variables{}
MetadataCustom metadata tags{}
NetworkPolicyOptional outbound network policy (egress)-
CredentialProxyOptional Credential Vault proxy startup settings-
VolumesOptional storage mounts (Host / PVC / OSSFS, supports ReadOnly and SubPath)-
ExtensionsExtra server-defined fields{}
SkipHealthCheckSkip readiness checks (Running + health check)false
HealthCheckCustom readiness check-
ReadyTimeoutSecondsMax time to wait for readiness30 seconds
HealthCheckPollingIntervalPoll interval while waiting (milliseconds)200 ms
SnapshotIdRestore a snapshot instead of passing Image; omit Entrypoint-
ResourceRequestsKubernetes resource requests; must not exceed limits-
LifecyclePre-start and periodic hooks-
PlatformOS/architecture constraint-
SecureAccessRequire endpoint access credentialsfalse
ManualCleanupDisable TTL expiration for image/snapshot creationfalse

WARNING

Metadata keys under opensandbox.io/ are reserved for system-managed labels and will be rejected by the server.

csharp
var sandbox = await Sandbox.CreateAsync(new SandboxCreateOptions
{
    ConnectionConfig = config,
    Image = "python:3.11",
    NetworkPolicy = new NetworkPolicy
    {
        DefaultAction = NetworkRuleAction.Deny,
        Egress = new List<NetworkRule>
        {
            new() { Action = NetworkRuleAction.Allow, Target = "pypi.org" }
        }
    },
    Volumes = new[]
    {
        new Volume
        {
            Name = "workspace",
            Host = new Host { Path = "/tmp/opensandbox-e2e/host-volume-test" },
            MountPath = "/workspace",
            ReadOnly = false
        }
    }
});

4. Runtime Egress Policy Updates ​

Runtime egress policy routing depends on the sandbox origin. For image-backed sandboxes, the SDK resolves port 18080 and calls the sidecar /policy API. For template-backed sandboxes (including restored template snapshots), the SDK detects OPEN-SANDBOX-ORIGIN: template and routes policy operations through the lifecycle /sandboxes/{sandboxId}/networkpolicy API.

Patch uses merge semantics:

  • Incoming rules take priority over existing rules with the same Target.
  • Existing rules for other targets remain unchanged.
  • Within a single patch payload, the first rule for a Target wins.
  • The current DefaultAction is preserved.
csharp
var policy = await sandbox.GetEgressPolicyAsync();

await sandbox.PatchEgressRulesAsync(new[]
{
    new NetworkRule { Action = NetworkRuleAction.Allow, Target = "www.github.com" },
    new NetworkRule { Action = NetworkRuleAction.Deny, Target = "pypi.org" }
});

5. Credential Vault ​

Credential Vault requires a sandbox-side egress service and is unavailable for template-backed sandboxes. It injects outbound credentials from the egress sidecar while keeping real secrets out of sandbox environment variables, commands, files, and logs. Create the sandbox with CredentialProxy enabled, then write credentials and bindings through sandbox.CredentialVault or the sandbox helper methods.

csharp
var sandbox = await Sandbox.CreateAsync(new SandboxCreateOptions
{
    ConnectionConfig = config,
    Image = "python:3.11",
    NetworkPolicy = new NetworkPolicy
    {
        DefaultAction = NetworkRuleAction.Deny,
        Egress = new List<NetworkRule>
        {
            new() { Action = NetworkRuleAction.Allow, Target = "api.example.com" }
        }
    },
    CredentialProxy = new CredentialProxyConfig { Enabled = true }
});

await sandbox.CreateCredentialVaultAsync(
    new[]
    {
        new Credential
        {
            Name = "api-token",
            Source = new InlineCredentialSource { Value = "<token>" }
        }
    },
    new[]
    {
        new CredentialBinding
        {
            Name = "api-token",
            Match = new CredentialMatch
            {
                Schemes = new[] { "https" },
                Hosts = new[] { "api.example.com" },
                Paths = new[] { "/v1/*" }
            },
            Auth = new CredentialAuth
            {
                Type = "apiKey",
                Name = "x-api-key",
                Credential = "api-token"
            }
        }
    });

See Credential Vault for auth types, binding guidance, and Git/curl examples.

6. Timeout and Retry Behavior ​

  • ConnectionConfig.RequestTimeoutSeconds controls timeout for SDK HTTP calls. File uploads and downloads are exempt while they keep making progress; the timeout still bounds the connect phase and how long a transfer waits for response headers before failing. Streaming (SSE) calls run for as long as the command does, but fail after max(RequestTimeoutSeconds, 30s) without response headers or stream bytes.
  • RunCommandOptions.TimeoutSeconds controls command execution timeout for command runs.
  • RunInSessionOptions.TimeoutSeconds controls command execution timeout for session runs.
  • SandboxCreateOptions.TimeoutSeconds controls sandbox server-side TTL.
  • ReadyTimeoutSeconds controls readiness waits. For ConnectAsync / ResumeAsync, endpoint discovery and health checks share this timeout. Blocking custom code can delay timeout reporting.
  • The SDK does not automatically retry failed API requests; implement retries in caller code where appropriate.

Redirects ​

SDK HTTP, SSE, and lifecycle-metrics clients follow up to 50 redirects. When the scheme, host, or port changes, they strip OPEN-SANDBOX-*, OPENSANDBOX-*, X-EXECD-ACCESS-TOKEN, and X-API-Key headers (case-insensitive). These headers remain removed if a later hop returns to the original origin. Same-origin hops retain them, and unrelated custom headers are unchanged.

As with .NET's normal redirect handling, Authorization is cleared on every hop; explicit Cookie headers retain .NET's existing behavior. HTTPS-to-HTTP redirects are not followed. Request bodies are not buffered to make uploads replayable.

The sandbox package includes a net5.0 asset so .NET 5 synchronous HttpClient.Send calls use the same redirect policy. Its existing dependencies support .NET 6 and later; this asset does not extend their support policy.

The package-consumer regression probe checks .NET 5 asset selection and synchronous and asynchronous redirects for both HTTP factories, including a return to the original origin. Run it against a locally packed fixture from sdks/sandbox/csharp:

bash
dotnet pack src/OpenSandbox/OpenSandbox.csproj -c Release -p:PackageVersion=1.1.1-redirect-probe -o artifacts/redirect-probe
dotnet restore tests/OpenSandbox.Net5.PackageProbe/OpenSandbox.Net5.PackageProbe.csproj -p:ProbePackageVersion=1.1.1-redirect-probe --source artifacts/redirect-probe --source https://api.nuget.org/v3/index.json
dotnet run --project tests/OpenSandbox.Net5.PackageProbe/OpenSandbox.Net5.PackageProbe.csproj -c Release --no-restore -p:ProbePackageVersion=1.1.1-redirect-probe

The probe explicitly permits major runtime roll-forward and prints the selected asset and actual runtime. Passing on a newer runtime does not verify execution on the .NET 5 runtime. Existing dependency-support warnings remain visible to consumers.

7. Resource Cleanup ​

Both Sandbox and SandboxManager implement IAsyncDisposable. Use await using or call DisposeAsync() when done.

csharp
await using var sandbox = await Sandbox.CreateAsync(options);
// ... use sandbox ...
// Automatically disposed when leaving scope

Error Handling ​

The SDK throws SandboxException (and derived exceptions such as SandboxApiException, SandboxReadyTimeoutException, and InvalidArgumentException) when operations fail.

csharp
try
{
    var execution = await sandbox.Commands.RunAsync("echo 'Hello Sandbox!'");
    Console.WriteLine(execution.Logs.Stdout.FirstOrDefault()?.Text);
}
catch (SandboxReadyTimeoutException)
{
    Console.Error.WriteLine("Sandbox did not become ready before the configured timeout.");
}
catch (SandboxApiException ex)
{
    Console.Error.WriteLine($"API Error: status={ex.StatusCode}, requestId={ex.RequestId}, message={ex.Message}");
}
catch (SandboxException ex)
{
    Console.Error.WriteLine($"Sandbox Error: [{ex.Error.Code}] {ex.Error.Message}");
}

Supported Frameworks ​

  • .NET Standard 2.0 (for maximum compatibility with .NET Framework 4.6.1+, .NET Core 2.0+, Mono, Xamarin, etc.)
  • .NET Standard 2.1
  • .NET 6.0
  • .NET 7.0
  • .NET 8.0
  • .NET 9.0
  • .NET 10.0

License ​

The C# SDK code is licensed under Apache 2.0.

Copyright © OpenSandbox a Series of LF Projects, LLC