Skip to Content
CraftBot Docs v1 — internal preview

Set Variable

Category: Logic & Control

Overview

The Set Variable node writes a value into one of the writable variable scopes. Use it to capture computed state, normalize input, stage data before an API call, or persist context across a transfer. The value is sourced as a literal, a reference to another variable, or an expression.

When to use

  • Initialize a counter or retry flag at the top of a loop.
  • Normalize input (uppercase a code, concatenate two fields) before validating.
  • Stage data for the next API or Webhook node.
  • Persist context across a transfer (e.g. originating_channel = whatsapp).

Ports

PortDirectionDescription
ininputReceives the flow from the previous node.
nextoutputFires immediately after the variable is written.

Configuration

Target variable

Two fields:

  • ScopeRecipe, Conversation, or User. The Config scope is read-only and is not offered as a write target. The synthetic system scope is also read-only.
  • Variable name — snake_case. Created if it does not exist; overwritten if it does.

See Variables for the full scope lifecycle.

Source

Three mutually exclusive source modes, picked via the button row:

  • Literal — type a value directly into the field.
  • Reference — write {{scope.var}} to copy from another variable.
  • Expression — a small arithmetic or string expression. Supported operators: + - * / for numbers; + for string concatenation. Variables are referenced as {{scope.var}}. Function calls and property access are rejected.

Common patterns

Retry counter

Top of loop: Set Variable retries = 0 (literal). Inside the loop: Set Variable retries = {{recipe.retries}} + 1 (expression). Condition: retries >= 3 → escalate.

Normalize for matching

Question → Set Variable (normalized = {{conversation.raw_input}}, reference mode) → Condition on the normalized value. (Use a follow-up Set Variable with an expression like {{conversation.code}} + "-US" for concatenation.)

Limits & validation

  • The config scope is provisioned by the tenant and is read-only — it can be referenced but never written.
  • The system scope is computed on read (e.g. system.now) — it cannot be written.
  • Expressions only accept the documented operator set. Any other token (function call, dot-property access, brackets) is rejected at runtime and escalates with set_variable_expr_failed.

Troubleshooting

Expression evaluates to literal text.

Verify variable names are wrapped in {{scope.var}} (not just {{var}}) and that the expression syntax matches the documented operators. If the Source mode is set to Literal, the field is taken verbatim — switch to Expression for arithmetic.

Set Variable escalates the session.

The runtime escalates when the target scope/name is missing, the source is missing, or an expression contains a forbidden construct (function call, property access).

Last updated on