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 imports; in host-target tests they read a thread-local stand-in (env::mock). Source: crates/sdk/src/env.rs.

Function Returns
env::caller() Caller: Account(id), ApiKey { key_id, account }, Public (not signed in), System (the node: timers) or App(id) (a delivered cross-app call).
env::app_id() This app instance’s id.
env::app_owner() The account that owns the app.
env::org() The app’s organization, if it has one.
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 and app callers with LoginRequired, and an account below the role with Forbidden. With no org, the owner holds every role and any other account only Member. 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.
env::random_bytes(len) len bytes, seeded per logged run: deterministic when replayed.
env::unique_id() <leader term>.<call seq>.<n>, unique across the app. 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 org (or have the same owner) and allow this app.
env::xcall_call::<T>(app, method, &args) Calls the target now and decodes its result as T.
env::xcall_view::<T>(app, method, &args) Reads the target now.

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

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.

env::log(message) writes a line to the run’s log. The host keeps 64 KiB per run.