Calling other handlers
A handler can call any other handler in your environment. Calls are commands in the journal: they are made exactly once per invocation, and their results survive crashes and deploys.
// Wait for the result.const plan = await ctx.call<Plan>("Planner", "plan", task);
// Fire and forget.ctx.send("Email", "welcome", { userId });
// A virtual object: pass the key.const total = await ctx.call<number>("Counter", "add", 5, { key: "user-42" });ctx.call
Section titled “ctx.call”call<T>(service: string, handler: string, input: unknown, opts?: CallOptions): DurableFuture<T>;- The callee runs as its own invocation, with its own journal and retries.
- When the result is not there yet, the caller suspends; nothing waits in memory. It resumes when the callee finishes.
- If the callee fails,
awaitthrows aTerminalErrorwith the callee’s code and message. The callee’s outcome is final, so retrying the caller would not change it. - The returned
DurableFuturecombines withctx.allandctx.race, for example to fan out:
const notes = await ctx.all( sections.map((section) => ctx.call<Note>("Researcher", "dig", { topic, section }, { name: `dig/${section}` }), ),);ctx.send
Section titled “ctx.send”send(service: string, handler: string, input: unknown, opts?: CallOptions): void;ctx.send records a one-way call and returns at once. It never suspends the caller. Use delay
(ms) to run the callee later:
ctx.send("Reminders", "remind", { userId }, { delay: 3 * 24 * 3600_000 });The callee of a send is detached work: it is not cancelled when the caller is cancelled.
Options
Section titled “Options”| Option | Meaning |
|---|---|
key |
Key of the target virtual object or workflow |
idempotencyKey |
At most one invocation for the same target and key — see below |
name |
Label of the call in the journal and the console (default service/handler) |
delay |
ctx.send only: run the callee this many ms later |
scope |
Run the callee in a scope (see Scopes). Absent: inherit the caller’s scope |
limitKey |
l1 or l1/l2: concurrency bucket inside the scope. Requires a scope |
placement |
"spread": deliberately spread unkeyed callees across partitions — see below |
tags |
Up to 8 key: value tags on the callee invocation. Keys match [a-z0-9_.-]{1,32}, values are at most 64 bytes; invalid tags are dropped and the call still runs |
Idempotency keys
Section titled “Idempotency keys”Calls from a handler are already deduplicated by the journal: a replay never sends the same call
twice. An idempotencyKey goes further: calls to the same target with the same key, from any
invocation, refer to the same callee invocation. A second call returns the
first call’s result (or waits for it) instead of running the handler again.
await ctx.call("Payments", "charge", order, { idempotencyKey: `charge-${order.id}` });Deduplication lasts as long as the finished invocation is retained. After it is purged, the same key starts a new invocation.
Scopes
Section titled “Scopes”A scope groups a tree of invocations that belong together — usually one user or one tenant of your product. Set it once at the root; every call below inherits it.
const agent = ctx.scoped(`user-${userId}`);const plan = await agent.call<Plan>("Planner", "plan", task);// Inside Planner.plan, ctx.call("Search", ...) stays in the same scope.ctx.scoped(scope, limitKey?)returns a view withcallandsendonly. Scope names use[A-Za-z0-9_.-], 1 to 36 characters; a bad name throws aTypeErrorimmediately.- A scope is part of identity.
Cart/ain scopebobis a different object, with different state, fromCart/awithout a scope. Idempotency keys and durable promises are per scope too. - Everything in a scope lives together, so calls within it stay local and fast. The price is that one scope is bounded by what a single partition can process. One scope per user or tenant is right; one per request, or one for the whole system, is not.
- A call can switch to another scope (
ctx.scoped("other")), but cannot leave a scope for “no scope”.
Spreading fan-out
Section titled “Spreading fan-out”Unkeyed calls (no scope, no key, no idempotencyKey) are placed next to their caller by
default, which keeps a whole call tree local. For a CPU-heavy fan-out you may want the opposite:
await Promise.all(files.map((f) => ctx.call("Resize", "run", f, { placement: "spread" })));placement only changes where the callee runs, not its identity. It is ignored when the call has a
scope (including an inherited one), a key or an idempotencyKey. Any other value than
"spread" throws a TypeError.
Attaching to another invocation
Section titled “Attaching to another invocation”If you have the ID of an invocation — from the client, from /send, or passed in the input — a
handler can wait for its result or peek at it:
const result = await ctx.attach<Report>(reportInvocationId); // waits durablyconst maybe = await ctx.getOutput<Report>(reportInvocationId); // null if not finished yetBoth throw a TerminalError if the target failed. ctx.invocationId is the current invocation’s
ID, in the same format the HTTP API uses.
Calls and cancellation
Section titled “Calls and cancellation”A hard cancel of an invocation also cancels the invocations it created with ctx.call and is
still waiting for, all the way down the tree. Invocations created with ctx.send are not touched; cancel
them by ID. See Cancellation.