Skip to content

The env API

calimero_sdk::env is an app’s view of the host: everything outside its own state. In a component every function reaches the host through the calimero:app world’s imports (below); in host-target tests they read a thread-local stand-in (env::mock). Source: crates/sdk/src/env.rs. A function the kind of run does not allow (an event from a view, state from an action) traps HostDenied; which kind may do what is in Kinds of run.

Function Returns
env::caller() Caller: Account(id), ApiKey { key_id, account }, Public (not signed in), Visitor { link, key } (an anonymous visitor through a public link: the link’s key and the visitor’s Ed25519 public key, hex; WIT context.visitor, 0.5.6), System (the node: timers) or App(id) (a delivered cross-app call).
env::via() For a call another app made as the person it serves, with their grant (the caller is that person): the app it came through; None otherwise. Read it to give a call through an app less than the person’s own (As the person).
env::grant_scope() The scope of the caller’s grant on this instance, as JSON text: what its managers set on the person’s grant (or the invite that made it), in the app’s own terms, such as {"channel":"partners"}. None for a caller who is no person, is named without a scope, or reaches the instance as a member or anyone; and in a compute run. The platform admits; the app scopes: keep a guest invited to one channel to that channel (The platform admits, the app scopes). WIT context.grant-scope, 0.5.6.
env::unit_of() The instance whose unit this app is a member of: the app whose release created this one with it (uses with mode = "create"). None for an app created on its own, once that instance is deleted, and in a compute run. An app that serves others treats app:<unit_of()> as its admin, so the instance that created it uses it with no grant step.
env::linked(name) The app linked under name, a reference of the release the app runs (uses): the member created with the instance, or the app its owner attached since. None when nothing is linked there, and in a compute run.
env::app_id() This app instance’s id.
env::app_owner() The account that owns the app.
env::org() The app’s organization. Every app is in one (an app created without naming one is in its owner’s only organization, or one made with it), so on a node this is always Some.
env::org_role(account) Option<OrgRole>: Guest, Member, Admin or Owner, ordered.
env::in_team(account, team) Whether account is in team.

guards turns those into checks that return an AppError (crates/sdk/src/guards.rs):

Guard Passes when
guards::require_member() The caller acts for an account that is a member of the app’s org, or more; returns the account.
guards::require_admin() The caller acts for an account that is an admin or the owner; returns the account.

Both refuse system, public, visitor and app callers with LoginRequired, and an account below the role with Forbidden. (The guards still treat an app with no organization as one where the owner holds every role and anyone else is a Member; since 1.0.0-rc.20 a node has no such app.) For other checks, match on env::caller() and env::org_role(..) directly.

#[app::confirm]
pub fn remove_member_item(&mut self, id: String) -> app::Result<()> {
guards::require_admin()?;
self.items.remove(&id)?;
Ok(())
}

See Access control for the full pattern.

Function Returns
env::time_now() Milliseconds since the epoch. A logged run sees its entry’s timestamp, so every replica agrees; a view sees the newest applied entry’s, an action the node’s clock rounded down to the second (Time and randomness). std::time reads the same clock.
env::random_bytes(len) len bytes, seeded per logged run: deterministic when replayed. A view or an action draws from the node’s CSPRNG, unrecorded.
env::unique_id() <leader term>.<call seq>.<n>: the leader’s Raft term, its execution counter in that term and the id’s position in the run, unique across the app. Built from the term, not the log index, which is assigned only when the entry is proposed. In init, calls, timers and deliveries only. Prefer it to counting with a Counter.
env::applied_index() The log index this run applies on top of.
env::timer() In an #[app::every] method, the tick (scheduled, previous); otherwise None.
env::emit(Event::Booked { id, room_id, start }, Audience::All)?;

env::emit(event, audience) takes an #[app::event] value; env::emit_raw(name, &data, audience) takes any serde value. Audience is All, Caller, Accounts(Vec<String>), Team(id), Org or OrgAdmins. Events are delivered once the entry commits. See Events.

Function Does
env::xcall(app, method, &args) Sends a message delivered after this run commits, only if it succeeds, at least once and in order per sending app. The target must be in the same organization and allow this app. At most 100 per run.
env::xcall_call::<T>(app, method, &args) A future: calls the target (one call in its own log, not atomic with this run) and decodes its result as T. Calls, timers and actions.
env::xcall_view::<T>(app, method, &args) A future: reads the target’s latest committed state. Calls, timers, views and actions.
app::join!(a, b, ..), app::join_all(v) Awaits futures together: their xcalls go to the node in one batch, which it runs at once. app::block_on(fut) runs a future from a plain fn; an async fn app method needs none.

A failed xcall_call/xcall_view is AppError XcallFailed carrying the target’s error. See Cross-app calls.

calimero_sdk::pipeline (start, get, cancel) are such calls to a linked pipeline app (Start a run from another app).

env::fetch(&Request) makes an HTTP request from a call, timer or delivery, to an origin the app’s owner approved; env::start_action(method, &args) starts an #[app::action] method and returns its key. See Outbound.

calimero_sdk::files writes and reads sealed files (files::create(), files::open(&BlobRef)); BlobRef::info and BlobRef::grant wrap env::blob_info and env::blob_grant. Table::search wraps env::search_query. See Blobs and Search.

calimero_sdk::compute starts and cancels runs from a logged write (compute::start(method, &args, &inputs), compute::cancel(&run)), publishes a done run’s output (compute::publish(&output)), and, inside an #[app::compute] method, lists the run’s inputs (compute::inputs()) and reports progress (compute::progress(percent, step)). files::uploader(&file) names the account that uploaded a sealed file (account#keyId for one a pipeline key uploaded). See Compute actions.

env::log(message) writes a line to the run’s log (the component’s stderr), as do the component’s own stdout and stderr. Logs are a development tool. Neither Calimero nor the operator may see user data, and an app’s log lines can hold some, so a production node does not collect them at all: every line is dropped as the app writes it, and no log entry, job node or route stores or answers one.

Where you read them:

  • a development node (merod --dev, which meroctl local up runs) prints each line to its own log, tagged with the app, the invocation kind and the method (target app_log), up to 64 KiB per run;
  • meroctl local compute run prints a compute method’s lines on stderr as log: <line>, a failed run’s included.

To diagnose a production app, reproduce the call on a development node.

An app is a component of the world calimero:app@0.5.7 (crates/app-wit/wit/calimero-app.wit). It exports one function, handle(kind, method, args) -> result<list<u8>, app-error>, which #[app::logic] generates, and imports the interfaces below. The node refuses at install a component that imports anything else, so an app cannot reach a file system, a socket or the network except through these.

WIT import What the SDK reaches it with
storage (get, put, delete, add, range) Every table and collection; add keeps counts and lengths without a read
rebuild Nothing: it is for state migrations, which this version does not run
context env::caller (with visitor), via, grant_scope, app_id, linked, unit_of, app_owner, org, org_role, in_team, applied_index, unique_id, start_action (org-role-strict and orgs-of have no SDK wrapper)
events env::emit, env::emit_raw
blobs BlobRef::info, BlobRef::grant (env::blob_info, env::blob_grant)
search Table::search (env::search_query), views only
files files::create, files::open, files::info
xcall (send, batch; call and view for components built before 0.5.4) env::xcall; env::xcall_call, env::xcall_view and typed clients, awaited, through batch
action ActionCtx::key, view, call inside an #[app::action]
runs compute::start, cancel, publish, files::uploader
wasi:clocks, wasi:random env::time_now, env::random_bytes (and std’s clock and HashMap seed), served by the host so a logged run is deterministic
wasi:cli/stdout, stderr env::log
wasi:http env::fetch, ActionCtx::fetch

The compute world, which the compute component of an app is built against, imports only types, files, job (compute::run_id, inputs, progress) and the WASI clocks, random and stdio: no state, no events, no cross-app calls and no network.