# JSON-RPC Client

> Call JSON-RPC 2.0 Method.

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

- Slug: `io.jsonrpc`
- Family: Input and output
- Version: 1.0.0
- Status: Available
- Execution: async

Call JSON-RPC 2.0 Method.

## Configuration

- `connection_id` (string, required): Credential for the JSON-RPC endpoint. Accepts credentials of type `api-key` or `basic-auth`.
- `method` (string, required): Method Name
- `params` (string, optional): Parameters (Array/Object as JSON string or array)
- `id` (string, optional): Request ID Default: `1`.

## Output ports

- `out` (default): JSON-RPC Result
- `error`: JSON-RPC Error

## Input contract

- `connection_id` (string, required)
- `method` (string, required)
- `params` (string, optional)
- `id` (string, optional)

## Output contract

- `result` (object, optional): JSON-RPC Result

## In expressions

What a later node can read from this node's output with `$("JSON-RPC Client").item.json.<path>` (write your node's label instead of `JSON-RPC Client`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("JSON-RPC Client").item.json.result` (object) = `{…}`: JSON-RPC Result
- `$("JSON-RPC Client").item.json.result.balance` (string) = `"1250.40"` (Not suggested by the editor)
- `$("JSON-RPC Client").item.json.result.currency` (string) = `"USD"` (Not suggested by the editor)


## How it works

Sends one JSON-RPC 2.0 request as an HTTP POST and returns the `result` member, or the JSON-RPC `error` member through the error port.

1. Resolves the config. `connection_id`, `method`, `params` and `id` fall back to the same keys of the incoming payload; `connection_id` and `method` are required.
2. Resolves the credential: the URL is `config.url` (or `config.endpoint`); when the secret has `username` and `password`, HTTP Basic authentication is used.
3. Builds `{ jsonrpc: "2.0", method, params, id }`: a string `params` is parsed as JSON, a missing one becomes `[]`; `id` defaults to `1`.
4. POSTs the request. A response with an `error` member leaves through `error`; otherwise `{ result }` leaves through `out`.

## In a workflow

### Node

```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"
}
```

## Output ports (behaviour in the code)

### `out`

The server answered 2xx without an `error` member. `result` is the JSON-RPC `result`.

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

### `error`

The response has a JSON-RPC `error` (data is `{ error: { code, message, data? } }`), or the request failed (data is `{ error, response }` with a message string).

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

## Behaviour in the code

- `connection_id`: Only `username` + `password` are read from the secret (`basic-auth`); an `api-key` credential adds no authentication. A value starting with `http://` or `https://` is used as the endpoint URL itself, without authentication.
- `params`: Accepts an array, an object or a JSON string; strings are parsed before sending.
- `id`: Sent as configured (a string when set in the config); the number `1` when empty.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `connection_id is required` | `error` | No credential selected. |
| `method is required` | `error` | The method name is empty in the config and in the incoming payload. |
| `<JSON parse error>` | `error` | `params` is a string that is not valid JSON. Write a JSON array or object, for example `["acc_001"]`. |
| `Request failed with status code <status>` | `error` | The HTTP call answered non-2xx; `response` holds the body. |

## Notes and limits

- One request per run; batch requests and notifications (requests without `id`) are not supported. No timeout and no retries.


## Contracts (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
  }
]
```
