Docs

Rust SDK

alien-sdk is the crate for Rust code that runs in a deployment: access to linked resources, plus commands, event handlers, and background tasks with the worker feature.

use alien_sdk::Bindings;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let bindings = Bindings::from_env()?;
    let cache = bindings.kv("cache").await?;
    cache.put("greeting", b"hello".to_vec(), None).await?;
    Ok(())
}

This page documents version 3.3.41. You still declare the stack in TypeScript (alien.ts); build the Rust binary with the rust toolchain ({ type: "rust", binaryName: "api" }).

Install

Cargo.toml
[dependencies]
alien-sdk = { version = "3.3.41", features = ["worker"] }
tokio = { version = "1", features = ["full"] }
FeatureDefaultWhat it adds
all-platformsyesProviders for every platform.
aws, gcp, azure, kubernetes, localnoProviders for one platform. Disable default features and pick these to shrink the binary.
workernoalien_sdk::worker: the Worker context, event handlers, commands, and wait_until.

Bindings

Bindings::from_env() reads the bindings Alien injects for each linked resource. Each accessor is async and takes the resource ID from alien.ts:

AccessorReturns
bindings.storage(name)Arc<dyn Storage>: object get, put, delete, list, and presigned requests
bindings.kv(name)Arc<dyn Kv>: get, put, delete, exists, scan_prefix
bindings.queue(name)Queue: send, receive, ack, and lease renewal
bindings.vault(name)Arc<dyn Vault>: secret get, put, delete, list
bindings.container(name)Arc<dyn Container>: internal and public URLs of a linked Container
bindings.sandbox(name)Arc<dyn Sandbox>: create sandboxes and run commands in them

The value and option types (KvEntry, PutOptions, QueueMessage, ScanResult, CreateSandboxRequest, RunCommandRequest) are in alien_sdk::traits. Errors are alien_sdk::error::Error, which carries a machine-readable code and a retryable flag.

Worker context

With the worker feature, AlienContext connects to the Worker runtime. Register handlers, register your HTTP server, then run the event loop:

src/bin/main.rs
use alien_sdk::worker::AlienContext;
use axum::{routing::get, Json, Router};
use serde_json::{json, Value};
use std::sync::Arc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let ctx = Arc::new(AlienContext::from_env().await?);

    ctx.on_command("echo", |params: Value| async move { Ok(params) });

    let app = Router::new().route("/health", get(|| async { Json(json!({ "status": "ok" })) }));
    let listener = tokio::net::TcpListener::bind("0.0.0.0:0").await?;
    ctx.register_http_server(listener.local_addr()?.port()).await?;

    let events = ctx.clone();
    tokio::spawn(async move { events.run().await });

    axum::serve(listener, app).await?;
    Ok(())
}

The server binds to port 0 and reports the chosen port to the runtime with register_http_server.

MethodPurpose
on_command::<P, R>(name, handler)Handle a command. P is deserialized from JSON and R is serialized back.
on_storage_event(bucket, handler)Handle object events from a Storage trigger.
on_queue_message(queue, handler)Handle one queue message at a time.
on_queue_batch(queue, handler)Handle a batch and return a per-delivery outcome.
on_cron_event(schedule, handler)Handle a schedule trigger.
wait_until(task)Keep a background task running after the handler returns.
bindings()The same Bindings as Bindings::from_env().
register_http_server(port)Tell the runtime where your HTTP server listens.
run()Receive and dispatch events until shutdown.

Next

On this page