Skip to main content
A ts function or ts effect declares TypeScript that belongs to the procedure. A declaration is not a step and never runs by itself. A call runs it once, addressed as '@local.<id>'. A polling wait can run an effect repeatedly.
  • Use a function for deterministic computation over its input: no network, secrets, clock or randomness. Running it again always gives the same result.
  • Use an effect for anything else: reading or writing an outside system, reading a secret, calling an Effective API operation, or reading the current time.
  • Use a value node instead when you only need to copy fields, fill a default or build a string.
Declarations, like schemas and secrets, can appear anywhere in the document, before or after the calls that use them. Each call of @local.<id> needs exactly one matching declaration, even when every test mocks it.

Syntax

Function

function id=pro-rata-refund input=refund-input result=refund
The body is exactly one synchronous arrow function. It takes one argument, the validated input.

Effect

effect id=cancel-policy input=cancel-request result=cancellation uses=fetch,secrets secrets=pas-token retry=pas-write-retry timeout=1m
The body is exactly one async arrow function. It takes two arguments: the validated input and the context.

Fields

Attributes follow id on the fence’s info line. Lists are comma-separated, with no spaces, empty entries or duplicates.

Function attributes

Effect attributes

What the code can use

Both forms use the same TypeScript, with a fixed set of standard objects: Members not listed, such as String.prototype.repeat, are not available. Not available in either form: imports of any kind, eval and other dynamic code, type assertions (as, <Type>value and the non-null !), Intl, console, timers, the filesystem, processes and environment variables. Write the code you need inside the declaration. Durable sleeps and polling don’t belong in code either; use a wait. A function also has no Date, no Math.random, no Promise and no services. It receives only its input. An effect also has Promise.resolve and then for async code, the ambient Date for wall-clock time, and the context below. Date can differ between attempts; never use it, or anything random, as an idempotency key. Code can’t trust outside data. response.json() returns unknown, so check its shape at run time before you return it, as the effect above does.

Effect context

Capabilities

uses= lists everything an effect may do beyond computing. A capability you don’t declare doesn’t exist in the effect’s context, and the code fails to compile if it uses it.

fetch

uses=fetch grants requests to the public internet. It excludes private networks, loopback and link-local addresses, cloud metadata services and other tenants’ networks. You can’t list allowed hosts per effect.
  • Each fetch sends one request. It never retries on its own; retries come only from the retry policy.
  • It follows up to 20 redirects. A redirect to another origin fails if it would forward a header or body you set; use redirect: "manual" and send the next request yourself.
  • It doesn’t add an idempotency header. Pass operation.idempotencyKey yourself in the header or field the receiving system uses.
  • It never reaches the Effective API. A relative URL, or a URL on the Effective API’s own origin, is refused. Use the effective client instead.

secrets

uses=secrets with secrets=<slot>,... grants the listed slots. Each slot needs a yaml secret declaration, and an organization binds the actual value outside the procedure.
  • Read a slot with secrets.getText('<slot>'), secrets.getJson('<slot>') or secrets.getBytes('<slot>'). The method must match the slot’s declared format.
  • The slot name must be a string literal. Computed names are invalid.
  • The effect must read every slot it lists, and list every slot it reads.
uses=fetch,secrets lets your code send secret values to any public host. Review effects that combine the two with care.

effective:<scope>

uses=effective:<scope> grants Effective API operations through the effective client. Effective authenticates the call for the run; you don’t store an Effective API token.
  • Name one operation exactly, such as uses=effective:api.email.send, or every read in a group, such as uses=effective:api.chat:read. Separate several with commas.
  • Name each write individually. Group selectors for writes are rejected, so every write is visible in the source and when the procedure is reviewed.
  • One effect may hold at most 64 operations, so very broad read selectors are too large.
  • The operation list is fixed when the procedure is published. A later API addition never widens an existing version; republish to use it.
  • The call runs with the run’s identity, and the API still applies its own permission checks.
  • On writes, Effective sends Idempotency-Key: operation.idempotencyKey for you. Where the operation deduplicates on that key, retries are safe.
The same operations can be called directly from YAML as '@effective.api.<operation>' in a call. The effect form is what lets you handle a refusal.
effect id=email-refund-notice input=email-input result=email-receipt uses=effective:api.email.send

Branch on a refused Effective API call

A refused effective.api.* call throws an error whose message doesn’t say why. Inside the effect, effective.apiError(error) tells you:
  • It returns { status, code, body } when the API answered with a 4xx. The call was refused and nothing was applied. body is the parsed JSON error body, or null if it wasn’t a JSON object or was larger than 16 KiB.
  • It returns null for a 5xx, no answer, or any other error. Those may have been applied, so rethrow them and let the retry policy apply.
Return the refusals you want to route on as a closed enum in the result, and route on that with a choice or repeat. Throw FatalError for any other refusal, so the run stops instead of retrying a call the API already refused:
effect id=email-refund-notice input=email-input result=email-receipt uses=effective:api.email.send
Here email-receipt declares outcome as enum: [sent, rejected]. Which status codes an operation returns for which refusals depends on the operation. effective.apiError exists only in effects with an effective: capability. A YAML call of an @effective.api.* operation has no failure routing: a refusal there stops the run with the API’s reason.

Example

A function works out the pro-rata return premium, then an effect emails the insured:
return-premium.procedure.md
The return-premium email template is illustrative. Use a template your organization has set up.
To try it, start it as a draft with your own inputs; see Try a draft. A draft can’t declare secrets, so stub any effect that needs one while you try it.

On retry and resume

  • Validated at both ends. The input is validated before the code runs, and the result is validated against result before it’s recorded.
  • Three attempts by default. Functions and effects get three attempts: one second before the first retry, doubling up to ten seconds. A retry policy replaces this.
  • Effect errors retry; function errors don’t. An uncaught effect error is retried. Throw FatalError to stop at once. A function error is final, because the function would give the same answer again; functions retry only after a timeout or a lost attempt.
  • Thrown messages aren’t shown. A message your code throws, FatalError or any other, could hold a secret value, so Effective doesn’t show it. The run’s failure names the step and a code such as local-code-failed, not your message. Return a refusal you need to see as a typed result instead, as in Branch on a refused Effective API call, and log the detail in your own system.
  • A finished call doesn’t run again. When a run resumes, a completed call’s recorded result is used, and the code isn’t called again.
  • Effects are not exactly once. A remote write can commit and its answer still be lost, so a retry can send it again. Send operation.idempotencyKey, use a natural business key, or select a one-attempt policy. Omitting retry= keeps the three-attempt default.
  • One write per effect. An effect may do supporting reads and one business write. Put writes that must recover independently in separate effects and separate calls, so each has its own result and identity.
  • Polling stops on doubt. In a polling wait, the next poll starts only after the previous one finished. If a poll attempt’s outcome is unknown, the wait fails for a person to check instead of polling again.
  • Pinned with the version. Publishing seals the code, its schemas and its capabilities into the version. Changing any of them needs a new version.

Common errors

See also