e2b-sdk

Crates.ioe2b-sdk
lib.rse2b-sdk
version0.1.1
created_at2025-02-13 23:17:34.298121+00
updated_at2025-02-24 23:55:45.970738+00
descriptionA Rust SDK for interacting with the E2B API
homepage
repositoryhttps://github.com/tullytim/e2b_rust
max_upload_size
id1555187
size61,843
Tim Tully (tullytim)

documentation

https://docs.rs/e2b_sdk

README

My Library

A simple Rust library for running code in e2b via Rust

🚀 Installation

Add this to your Cargo.toml:

[dependencies]
e2b_sdk = "0.1.0"

Usage

use e2b_sdk::{E2BClient, ExecuteResponse};
use std::error::Error;
use tokio::time::{Duration, timeout};

async fn main() -> Result<(), Box<dyn Error>> {
    let api_key = std::env::var("E2B_API_KEY").expect("E2B_API_KEY must be set");
    let client = E2BClient::new(api_key);

    let sandbox_id = client.create_sandbox().await?;
    println!("Have sandbox id: {}", sandbox_id);
    let code = r#"
    print("Starting...")
    for i in range(5):
        print(f"Running for {i} ...")

    print("Finished!")
        "#;    
    let mut rx = client.execute_code(sandbox_id.clone(), code).await?;
    let timeout_duration = Duration::from_secs(10);

                    while let Ok(Some(response)) = timeout(timeout_duration, rx.recv()).await {
                        match response {
                            ExecuteResponse::Stdout { text, timestamp} => println!("stdout: {} {}", text, timestamp),
                            ExecuteResponse::Stderr { name } => eprintln!("stderr: {}", name),
                            ExecuteResponse::Result { content } => println!("result: {}", content),
                            ExecuteResponse::Error { name, value } => eprintln!("error: {} : {}", name, value),
                        }
                    }
   
    println!("Killing sandbox...");
    client.kill_sandbox(&sandbox_id).await?;

    Ok(())
}
Commit count: 17

cargo fmt