# JSON-RPC Client

> 调用 JSON-RPC 2.0 方法。

Source: https://docs.sommatic.ai/zh/nodes/io.jsonrpc/

- 标识: `io.jsonrpc`
- 类别: 输入与输出
- 版本: 1.0.0
- 状态: 可用
- 执行方式: async

调用 JSON-RPC 2.0 方法。

## 配置

- `connection_id` (string, 必填): JSON-RPC 端点的凭据。接受 `api-key` 或 `basic-auth` 类型的凭据。
- `method` (string, 必填): 方法名称
- `params` (string, 可选): 参数（数组/对象，可为 JSON 字符串或数组）
- `id` (string, 可选): 请求 ID 默认值: `1`.

## 输出端口

- `out` (默认): JSON-RPC 结果
- `error`: JSON-RPC 错误

## 输入契约

- `connection_id` (string, 必填)
- `method` (string, 必填)
- `params` (string, 可选)
- `id` (string, 可选)

## 输出契约

- `result` (object, 可选): JSON-RPC 结果

## 在表达式中

后续节点可通过 `$("JSON-RPC Client").item.json.<路径>` 读取此节点输出中的内容（请把 `JSON-RPC Client` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `out` 返回的路径，依据“端口”一节所示的输出。

- `$("JSON-RPC Client").item.json.result` (object) = `{…}`: JSON-RPC 结果
- `$("JSON-RPC Client").item.json.result.balance` (string) = `"1250.40"` (编辑器不提示)
- `$("JSON-RPC Client").item.json.result.currency` (string) = `"USD"` (编辑器不提示)


## 工作原理

以 HTTP POST 发送一次 JSON-RPC 2.0 请求，返回 `result` 成员，或通过 error 端口返回 JSON-RPC `error` 成员。

1. 解析配置。`connection_id`、`method`、`params` 和 `id` 缺失时回退到输入 payload 中的同名键；`connection_id` 和 `method` 为必填。
2. 解析凭据：URL 为 `config.url`（或 `config.endpoint`）；若密钥含 `username` 和 `password`，则使用 HTTP Basic 认证。
3. 构建 `{ jsonrpc: "2.0", method, params, id }`：字符串形式的 `params` 会按 JSON 解析，缺失时为 `[]`；`id` 默认为 `1`。
4. POST 该请求。响应含 `error` 成员时从 `error` 输出；否则从 `out` 输出 `{ result }`。

## 在工作流中

### 节点

```json
{
  "node_id": "node-get-balance",
  "label": "Get balance",
  "operator_slug": "io.jsonrpc",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "method": "account.getBalance",
    "params": "[\"{{ $json.account_id }}\"]",
    "id": "1"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "result": {
      "balance": "1250.40",
      "currency": "USD"
    }
  },
  "port": "out"
}
```

## 输出端口 (代码中的行为)

### `out`

服务器返回 2xx 且无 `error` 成员。`result` 为 JSON-RPC 的 `result`。

```json
{
  "result": {
    "balance": "1250.40",
    "currency": "USD"
  }
}
```

### `error`

响应包含 JSON-RPC `error`（数据为 `{ error: { code, message, data? } }`），或请求失败（数据为 `{ error, response }`，其中 error 为消息字符串）。

```json
{
  "error": {
    "code": -32601,
    "message": "Method not found"
  }
}
```

## 代码中的行为

- `connection_id`: 密钥中只读取 `username` + `password`（`basic-auth`）；`api-key` 凭据不会添加认证。以 `http://` 或 `https://` 开头的值会直接用作端点 URL，不带认证。
- `params`: 接受数组、对象或 JSON 字符串；字符串会在发送前解析。
- `id`: 按配置原样发送（在配置中设置时为字符串）；为空时发送数字 `1`。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `connection_id is required` | `error` | 未选择凭据。 |
| `method is required` | `error` | 配置和输入 payload 中的方法名均为空。 |
| `<JSON parse error>` | `error` | `params` 是无效 JSON 字符串。请写成 JSON 数组或对象，例如 `["acc_001"]`。 |
| `Request failed with status code <status>` | `error` | HTTP 调用返回非 2xx 状态；`response` 中包含正文。 |

## 说明与限制

- 每次运行一个请求；不支持批量请求和通知（无 `id` 的请求）。无超时，也不重试。


## 契约（JSON Schema）

### All (`*.definition.js`)

```json
{
  "slug": "io.jsonrpc",
  "name": "JSON-RPC Client",
  "category": "io",
  "version": "1.0.0",
  "description": "Call JSON-RPC 2.0 Method.",
  "capabilities": [
    "jsonrpc",
    "api",
    "http"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "JSON-RPC Credential",
        "description": "Credential for the JSON-RPC endpoint. Accepts credentials of type `api-key` or `basic-auth`.",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "api-key",
            "basic-auth"
          ],
          "types": [
            "jsonrpc",
            "http"
          ]
        }
      },
      "method": {
        "type": "string",
        "description": "Method Name"
      },
      "params": {
        "type": "string",
        "description": "Parameters (Array/Object as JSON string or array)"
      },
      "id": {
        "type": "string",
        "default": "1",
        "description": "Request ID"
      }
    },
    "required": [
      "connection_id",
      "method"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "method": {
        "type": "string"
      },
      "params": {
        "type": "string"
      },
      "id": {
        "type": "string"
      }
    },
    "required": [
      "connection_id",
      "method"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "result": {
        "type": "object",
        "description": "JSON-RPC Result"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Result",
      "description": "JSON-RPC Result",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "JSON-RPC Error",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard-io"
  ],
  "tags": [
    "rpc",
    "api"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "JSON-RPC Credential",
      "description": "Credential for the JSON-RPC endpoint. Accepts credentials of type `api-key` or `basic-auth`.",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "api-key",
          "basic-auth"
        ],
        "types": [
          "jsonrpc",
          "http"
        ]
      }
    },
    "method": {
      "type": "string",
      "description": "Method Name"
    },
    "params": {
      "type": "string",
      "description": "Parameters (Array/Object as JSON string or array)"
    },
    "id": {
      "type": "string",
      "default": "1",
      "description": "Request ID"
    }
  },
  "required": [
    "connection_id",
    "method"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "method": {
      "type": "string"
    },
    "params": {
      "type": "string"
    },
    "id": {
      "type": "string"
    }
  },
  "required": [
    "connection_id",
    "method"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "result": {
      "type": "object",
      "description": "JSON-RPC Result"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Result",
    "description": "JSON-RPC Result",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "JSON-RPC Error",
    "is_default": false,
    "supports_condition": false
  }
]
```
