# Invoke operator API service

```{raw} html
<div class="mo-api-page-show-toc" aria-hidden="true"></div>
```

Call the published and enabled operator API service. The fields of `payload` must conform to the input definition of the service; first [View Service Configuration](get-operator-api-service.md) confirms that the service can be called and obtains the input definition.

```text
POST https://moi.matrixorigin.cn/newmoi/workflow/v2/workitems/catalog/$NODE_ID/api-service/invoke
```

## Preparation before calling

First [View Service Configuration](get-operator-api-service.md), confirm that the service is enabled, and obtain the operator ID, version and service name of the service to be called. Prepare the personal access token, target workspace ID, operator ID, version, and service name that has access to the target workspace.


## Request body

::::{div} mo-api-split
:::{div} mo-api-split-main

```{raw} html
<dl class="mo-api-fields">
  <div class="mo-api-field"><dt><code>node_id</code><span class="mo-api-field__type">string</span><span class="mo-api-field__required">Required</span></dt><dd>The ID of the operator to be called.</dd></div>
  <div class="mo-api-field"><dt><code>service_name</code><span class="mo-api-field__type">string</span><span class="mo-api-field__required">Required</span></dt><dd>Service name, using data.service.service_name from the service configuration response.</dd></div>
  <div class="mo-api-field"><dt><code>type</code><span class="mo-api-field__type">string</span><span class="mo-api-field__required">Required</span></dt><dd>Fixed to operator.</dd></div>
  <div class="mo-api-field"><dt><code>version</code><span class="mo-api-field__type">string</span><span class="mo-api-field__required">Required</span></dt><dd>The published operator version.</dd></div>
  <div class="mo-api-field"><dt><code>payload</code><span class="mo-api-field__type">object</span></dt><dd>the input data of the service; fields and types are subject to data.service.input_schema.</dd></div>
</dl>
```

:::
:::{div} mo-api-split-aside

```{raw} html
<p class="mo-api-example-label">Request example</p>
```

```bash
curl -X POST "https://moi.matrixorigin.cn/newmoi/workflow/v2/workitems/catalog/$NODE_ID/api-service/invoke" \
  -H "X-API-Key: $AI_STUDIO_API_KEY" \
  -H "X-Workspace-ID: $WORKSPACE_ID" \
  -H 'Content-Type: application/json' \
  -d "{
    \"service_name\": \"$SERVICE_NAME\",
    \"type\": \"operator\",
    \"version\": \"$VERSION\",
    \"payload\": {
      \"sql\": \"SELECT now()\"
    }
  }"
```

:::
::::

## Successful response

`data.result` returns the status and operator output of this call. `result` is returned as a JSON format string; the operator output is read after parsing the string. When `error` appears in the response, handle it as an error message.


The response fields are as follows.

::::{div} mo-api-split
:::{div} mo-api-split-main

```{raw} html
<dl class="mo-api-fields">
  <div class="mo-api-field"><dt><code>code</code><span class="mo-api-field__type">string</span></dt><dd>OK on success.</dd></div>
  <div class="mo-api-field"><dt><code>msg</code><span class="mo-api-field__type">string</span></dt><dd>OK on success.</dd></div>
  <div class="mo-api-field"><dt><code>data.result.case_id</code><span class="mo-api-field__type">string</span></dt><dd>ID of this call; can be used to correlate this result when the response is provided.</dd></div>
  <div class="mo-api-field"><dt><code>data.result.status</code><span class="mo-api-field__type">string</span></dt><dd>The status returned by the service; occurs when the response is provided.</dd></div>
  <div class="mo-api-field"><dt><code>data.result.result</code><span class="mo-api-field__type">string</span></dt><dd>JSON format string returned by the service; the parsed structure is determined by the operator.</dd></div>
  <div class="mo-api-field"><dt><code>data.result.error</code><span class="mo-api-field__type">string</span></dt><dd>Error message returned by the service; appears when there is an error.</dd></div>
</dl>
```

:::
:::{div} mo-api-split-aside

```{raw} html
<p class="mo-api-example-label">Successful response example</p>
```

```json
{
  "code": "OK",
  "msg": "OK",
  "data": {
    "result": {
      "case_id": "case_01JEX4A1M2",
      "status": "COMPLETED",
      "result": "{\"count\":9}"
    }
  }
}
```

:::
::::

## Error response

::::{div} mo-api-split
:::{div} mo-api-split-main

```{raw} html
<dl class="mo-api-fields">
  <div class="mo-api-field"><dt><code>code</code><span class="mo-api-field__type">string</span></dt><dd>Error code.</dd></div>
  <div class="mo-api-field"><dt><code>msg</code><span class="mo-api-field__type">string</span></dt><dd>Readable error message.</dd></div>
  <div class="mo-api-field"><dt><code>data</code><span class="mo-api-field__type">null</span></dt><dd>`null` in an error response.</dd></div>
</dl>
```

:::
:::{div} mo-api-split-aside

```{raw} html
<p class="mo-api-example-label">Error response example</p>
```

```json
{
  "code": "api_service_unavailable",
  "msg": "服务暂不可用",
  "data": null
}
```

:::
::::

## Follow-up operations

After the call is completed, when you need to check the configuration [View operator API service configuration](get-operator-api-service.md).
