E2B SDK for Java and Go

Updated at:

E2B officially provides only Python and TypeScript SDKs. To make it easier for Java and Go developers to connect to Agent Sandbox Flash Edition, the Alibaba Cloud Function Compute team maintains the following E2B data-plane-compatible SDKs.

This topic first describes the connection settings shared by both SDKs, and then provides separate Java and Go quickstarts. For exact interfaces, parameter names, and defaults, refer to the source code and type definitions in each repository.

Prerequisites and common connection configuration

  • You have an Alibaba Cloud account and the required permissions to use Function Compute.

  • You have created an API key in the Agent Sandbox console.

  • You have confirmed that Agent Sandbox Flash Edition is available in the target region and that your local network can access the service endpoint in that region.

Pass the API key, API URL, and domain through environment variables. Do not hard-code them in source code.

export E2B_API_KEY="<your-api-key>"
export E2B_API_URL="https://api.<region>.sandbox.aliyuncs.com"
export E2B_DOMAIN="<region>.sandbox.aliyuncs.com"

Replace <region> with the region where Agent Sandbox Flash Edition is deployed. Supported values are cn-beijing, cn-shanghai, cn-hangzhou, cn-shenzhen, cn-hongkong, ap-southeast-1, us-east-1, and us-west-1. The API URL, domain, template, and sandbox must be in the same region.

Java SDK

Availability and installation

The Java SDK is currently available as source code. Version 2.1.0 has not been published to Maven Central or another public Maven repository. Clone the repository and install the SDK in your local Maven repository:

git clone https://github.com/aliyun-fc/e2b-java-sdk.git
cd e2b-java-sdk
mvn clean install -DskipTests

For a Maven project, add the dependency:

<dependency>
    <groupId>com.alibaba.serverless</groupId>
    <artifactId>e2b-java-sdk</artifactId>
    <version>2.1.0</version>
</dependency>

For a Gradle project, enable the local Maven repository before adding the dependency:

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    implementation 'com.alibaba.serverless:e2b-java-sdk:2.1.0'
}

The SDK requires Java 8 or later. Its main packages are:

PackageDescription
dev.e2b.sdkSandbox entry class.
dev.e2b.sdk.clientConnection configuration such as ConnectionConfig.
dev.e2b.sdk.modelData models such as CommandResult.

Create and run a sandbox

import dev.e2b.sdk.Sandbox;
import dev.e2b.sdk.client.ConnectionConfig;
import dev.e2b.sdk.model.CommandResult;

public class Quickstart {
    public static void main(String[] args) throws Exception {
        ConnectionConfig config = ConnectionConfig.builder()
                .apiKey(System.getenv("E2B_API_KEY"))
                .apiUrl(System.getenv("E2B_API_URL"))
                .domain(System.getenv("E2B_DOMAIN"))
                .build();

        try (Sandbox sandbox = Sandbox.create("code-interpreter-v1", config)) {
            CommandResult result = sandbox.getCommands().run(
                    "python3 -c \"print('hello from sandbox')\""
            );
            System.out.println(result.getStdout().trim());
        }
    }
}

Sandbox implements AutoCloseable. Leaving the try block calls kill() and releases the sandbox. For finer lifecycle control, set an explicit sandbox timeout and call sandbox.kill() when the task finishes.

Go SDK

Availability and installation

The Go SDK is currently available as source code and requires Go 1.22 or later. Its go.mod still declares the legacy module path github.com/e2b-dev/e2b-go-sdk, which is no longer resolvable. Clone the SDK next to your Go project and replace the legacy module with the local directory.

The following example assumes that your project is in myapp/:

# Run from the directory that contains myapp/.
git clone https://github.com/aliyun-fc/e2b-go-sdk.git
cd myapp
go mod init myapp  # Skip this command if go.mod already exists.
go mod edit -replace=github.com/e2b-dev/e2b-go-sdk=../e2b-go-sdk

Keep the legacy module path in imports because it matches the SDK's current go.mod declaration:

import e2b "github.com/e2b-dev/e2b-go-sdk"

Create and run a sandbox

Save the following example as main.go:

package main

import (
    "context"
    "fmt"
    "log"
    "os"
    "strings"
    "time"

    e2b "github.com/e2b-dev/e2b-go-sdk"
)

func main() {
    ctx := context.Background()

    client, err := e2b.NewClient(
        e2b.WithAPIKey(os.Getenv("E2B_API_KEY")),
        e2b.WithAPIURL(os.Getenv("E2B_API_URL")),
        e2b.WithDomain(os.Getenv("E2B_DOMAIN")),
    )
    if err != nil {
        log.Fatal(err)
    }

    sandbox, err := client.CreateSandbox(
        ctx,
        e2b.WithTemplate("code-interpreter-v1"),
        e2b.WithTimeout(900),
    )
    if err != nil {
        log.Fatal(err)
    }
    defer sandbox.Kill(context.Background())

    result, err := sandbox.Commands.Run(
        ctx,
        "python3 -c \"print('hello from sandbox')\"",
        e2b.WithCommandTimeout(60*time.Second),
    )
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(strings.TrimSpace(result.Stdout))
}

Resolve the local replacement and run the example:

go mod tidy
go run .

defer sandbox.Kill(...) releases the sandbox when the function exits. In production code, set an explicit timeout and release the sandbox when the task finishes.