# 表达式 API 参考

> 表达式可使用的全部变量与辅助函数，包含签名、参数、返回值、错误，以及在 Sommatic AI 实际运行的引擎上执行过的示例。

Source: https://docs.sommatic.ai/zh/expressions/reference/

`{{ }}` 中的表达式可以读取或调用的全部内容。这些页面上的每个示例都在已部署的编排器表达式引擎上、基于同一个[示例上下文](#example-context)运行过，所示结果即引擎的实际返回值。

## Previous Nodes

- [Previous Nodes](https://docs.sommatic.ai/zh/expressions/reference/previous-nodes.md): 每个节点的表达式 API：后续节点通过 $("Label").item.json 读取的输出路径，包括编辑器提示的路径与 worker 实际返回的路径。

## Input Data

- [`$json`](https://docs.sommatic.ai/zh/expressions/reference/input-data.md#json) → `object`: 当前节点的输入。
- [`$binary`](https://docs.sommatic.ai/zh/expressions/reference/input-data.md#binary) → `object`: 附加到当前条目的二进制数据（文件）。保留：始终为 {}。
- [`$itemIndex`](https://docs.sommatic.ai/zh/expressions/reference/input-data.md#item-index) → `number`: 当前条目在批次中的索引。保留：始终为 0。
- [`$runIndex`](https://docs.sommatic.ai/zh/expressions/reference/input-data.md#run-index) → `number`: 当前尝试的索引。保留：始终为 0。

## Input Access

- [`$input.item`](https://docs.sommatic.ai/zh/expressions/reference/input-access.md#input-item) → `{ json, binary }`: 当前输入条目：{ json, binary }。
- [`$input.all()`](https://docs.sommatic.ai/zh/expressions/reference/input-access.md#input-all) → `list`: 所有输入条目，以列表形式返回。每次执行一个条目。
- [`$input.first()`](https://docs.sommatic.ai/zh/expressions/reference/input-access.md#input-first) → `{ json, binary }`: 第一个输入条目（即当前条目）。
- [`$input.last()`](https://docs.sommatic.ai/zh/expressions/reference/input-access.md#input-last) → `{ json, binary }`: 最后一个输入条目（即当前条目）。
- [`$input.params`](https://docs.sommatic.ai/zh/expressions/reference/input-access.md#input-params) → `object`: 上一个节点的配置。保留：始终为 {}。
- [`$input.context`](https://docs.sommatic.ai/zh/expressions/reference/input-access.md#input-context) → `object`: 上一个节点传递的上下文。保留：始终为 {}。

## Node Reference

- [`$(label)`](https://docs.sommatic.ai/zh/expressions/reference/node-reference.md#node-accessor) → `NodeAccessor`: 之前某个节点的输出，按标签查找。
- [`$prevNode`](https://docs.sommatic.ai/zh/expressions/reference/node-reference.md#prev-node) → `object`: 上一个节点的元数据。保留：始终为 Start。
- [`$('Chain Entry Point').item.json`](https://docs.sommatic.ai/zh/expressions/reference/node-reference.md#chain-entry) → `{ …data, chain_context }`: 由 workflow.chain 启动的工作流所接收的内容：数据和链上下文。

## Execution Context

- [`$execution`](https://docs.sommatic.ai/zh/expressions/reference/execution-context.md#execution) → `object`: 当前运行的 id。
- [`$workflow`](https://docs.sommatic.ai/zh/expressions/reference/execution-context.md#workflow) → `object`: 正在运行的工作流的 slug 和版本。
- [`$node`](https://docs.sommatic.ai/zh/expressions/reference/execution-context.md#node) → `object`: 当前节点的 id 和标签。
- [`$env`](https://docs.sommatic.ai/zh/expressions/reference/execution-context.md#env) → `object`: 环境变量。保留：始终为 {}。
- [`$vars`](https://docs.sommatic.ai/zh/expressions/reference/execution-context.md#vars) → `object`: 工作流变量。保留：始终为 {}。
- [`$nodeVersion`](https://docs.sommatic.ai/zh/expressions/reference/execution-context.md#node-version) → `number`: 当前节点算子的版本。保留：始终为 1。

## Date & Time

- [`$now`](https://docs.sommatic.ai/zh/expressions/reference/date-time.md#now) → `DateTime`: 节点运行的时刻，类型为 Luxon DateTime。
- [`$today`](https://docs.sommatic.ai/zh/expressions/reference/date-time.md#today) → `DateTime`: 当天的起始时刻，类型为 Luxon DateTime。

## Generators

- [`$uuid`](https://docs.sommatic.ai/zh/expressions/reference/generators.md#uuid) → `string`: 每次读取都生成新的 UUID v4。
- [`$randomId({ length, prefix })`](https://docs.sommatic.ai/zh/expressions/reference/generators.md#random-id) → `string`: 带前缀的随机 id。

## Encoders

- [`$base64Encode(value)`](https://docs.sommatic.ai/zh/expressions/reference/encoders.md#base64-encode) → `string`: 文本或 JSON 值的 URL 安全 Base64 编码。
- [`$base64Decode(text)`](https://docs.sommatic.ai/zh/expressions/reference/encoders.md#base64-decode) → `string`: Base64 字符串所编码的文本。
- [`$toQueryString(object)`](https://docs.sommatic.ai/zh/expressions/reference/encoders.md#to-query-string) → `string`: 以 ? 开头的 URL 查询字符串。

## Validators

- [`$isEmpty(value)`](https://docs.sommatic.ai/zh/expressions/reference/validators.md#is-empty) → `boolean`: 值没有自有键时为 true。

## Helpers

- [`$ifEmpty(value, fallback)`](https://docs.sommatic.ai/zh/expressions/reference/helpers.md#if-empty) → `any`: 返回 value；value 为空时返回 fallback。
- [`$slugify(text)`](https://docs.sommatic.ai/zh/expressions/reference/helpers.md#slugify) → `string`: 小写、以连字符分隔的 slug。
- [`$jmespath(data, query)`](https://docs.sommatic.ai/zh/expressions/reference/helpers.md#jmespath) → `any`: 对任意值执行 JMESPath 查询的结果。
- [`_`](https://docs.sommatic.ai/zh/expressions/reference/helpers.md#lodash) → `lodash`: lodash：约 300 个用于列表、对象和文本的函数。

## JavaScript Globals

- [JavaScript Globals](https://docs.sommatic.ai/zh/expressions/reference/javascript-globals.md): Math、JSON、字符串、数字、列表、对象、日期、正则表达式、Map 与 Set、Intl，以及不可用的部分。

## 库

- [lodash](https://docs.sommatic.ai/zh/expressions/lodash.md): 全部 lodash 函数，通过 `_` 调用。
- [Luxon 日期](https://docs.sommatic.ai/zh/expressions/luxon.md): `$now` 与 `$today` 的全部 DateTime 方法。
- [JMESPath](https://docs.sommatic.ai/zh/expressions/jmespath.md): `$jmespath` 使用的查询语言。
- [JSONata 函数](https://docs.sommatic.ai/zh/expressions/jsonata-functions.md): JSONata 字段中的函数。

## 不存在的名称

开发者常从其他工具或旧文档中期待、但引擎并未提供的名称。使用不存在的名称会抛出 `… is not defined`。

- `$node["X"].json`: `$node` 仅指当前节点。读取其他节点请用 `$('X').item.json`。
- `$items()`: 使用 `$input.all()` 或 `$('X').all()`。
- `$parameter`: 节点自身的配置不对其表达式开放。
- `$config`: 服务配置从不开放。
- `DateTime`: 不是全局名称。请使用 `$now.constructor`。
- `Several items per node`: 每次节点执行只处理一个条目。请用 `flow.split` 和 `flow.join` 迭代，或在单个表达式中使用 `map`。

## 示例上下文

示例基于以下数据运行：当前节点的输入（`$json`）、已运行的节点（`$(…)`）、本次运行的 ID 以及当前节点。时钟固定为 `2026-11-26T14:30:00.000Z`，服务器时区为 UTC，因此各页面中 `$now` 与 `$today` 的值都相同。

```json
{
  "$json": {
    "body": {
      "order_id": "ord_1042",
      "email": "ana.perez@example.com",
      "customer": {
        "name": "Ana Pérez",
        "segment": "gold"
      },
      "priority": "",
      "items": [
        {
          "sku": "A-100",
          "qty": 2,
          "price": 10.5
        },
        {
          "sku": "B-200",
          "qty": 0,
          "price": 4
        },
        {
          "sku": "C-300",
          "qty": 1,
          "price": 25
        }
      ],
      "tags": [
        "new",
        "web",
        "new"
      ],
      "created_at": "2026-11-25T09:15:00Z"
    },
    "headers": {
      "content-type": "application/json"
    },
    "query": {},
    "method": "POST"
  },
  "nodes": {
    "Webhook Receiver": "= $json",
    "Normalize input": {
      "correlation_id": "c-7f3a",
      "channel": "web"
    },
    "Classify": {
      "label": "billing",
      "confidence": 0.92
    }
  },
  "run": {
    "$flow_instance_result_id": "fir-xxxx",
    "$flow_instance_id": "fi-xxxx",
    "$flow_definition_id": "fd-xxxx",
    "$flow_version_id": "fv-xxxx",
    "$flow_slug": "order-intake"
  },
  "node": {
    "node_id": "node-notify",
    "label": "Notify customer"
  }
}
```

串联工作流的示例基于第二个上下文运行：一个由 `workflow.chain` 启动的工作流，其 `workflow.trigger` 节点的标签为 `Chain Entry Point`。

```json
{
  "$json": {
    "order_id": "ord_1042",
    "customer_email": "ana.perez@example.com",
    "amount": 46,
    "chain_context": {
      "caller_flow_definition_id": "fd-caller-xxxx",
      "caller_flow_instance_id": "fi-caller-xxxx",
      "caller_flow_slug": "order-intake",
      "caller_node_id": "node-chain-billing",
      "correlation_id": "chain-7Hq2Lw9sKd3pXa1mZr4t",
      "root_flow_instance_result_id": "fir-root-xxxx"
    }
  },
  "run": {
    "$flow_instance_result_id": "fir-hop-xxxx",
    "$flow_instance_id": "fi-hop-xxxx",
    "$flow_definition_id": "fd-billing-xxxx",
    "$flow_version_id": "fv-billing-xxxx",
    "$flow_slug": "billing-review",
    "$input": {
      "data": {
        "order_id": "ord_1042",
        "customer_email": "ana.perez@example.com",
        "amount": 46
      },
      "chain_context": {
        "caller_flow_definition_id": "fd-caller-xxxx",
        "caller_flow_instance_id": "fi-caller-xxxx",
        "caller_flow_slug": "order-intake",
        "caller_node_id": "node-chain-billing",
        "correlation_id": "chain-7Hq2Lw9sKd3pXa1mZr4t",
        "root_flow_instance_result_id": "fir-root-xxxx"
      }
    }
  },
  "node": {
    "node_id": "node-create-task",
    "label": "Create review task"
  }
}
```

示例运行所基于的编排器提交 31e874b.
