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
[dependencies]
alien-sdk = { version = "3.3.41", features = ["worker"] }
tokio = { version = "1", features = ["full"] }| Feature | Default | What it adds |
|---|---|---|
all-platforms | yes | Providers for every platform. |
aws, gcp, azure, kubernetes, local | no | Providers for one platform. Disable default features and pick these to shrink the binary. |
worker | no | alien_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:
| Accessor | Returns |
|---|---|
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:
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.
| Method | Purpose |
|---|---|
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
- Resources: options and limits for each resource
- Worker: triggers and the
rusttoolchain - TypeScript SDK