# Using C.SPARK\_UDFCALLAPI() \[legacy]

{% hint style="warning" %}
This functionality is deprecated and not supported.
{% endhint %}

## Introduction

This implementation of `Xcall` has 2 parts, the `Xcall` block to define the request, and a user-defined function, `Spark_UdfCallAPI` to receive responses.

<figure><img src="/files/5thVKXTmAotoADt1r45I" alt=""><figcaption><p>Xcall using Spark Assistant: Data Flow</p></figcaption></figure>

## Sample files

In this example we are using two files (Downstream and Upstream file) to show the functionality of the `Xcall` using Spark Assistant.

<figure><img src="/files/hdrzrgM8Xy58Nc3dRkzr" alt=""><figcaption><p>Downstream File</p></figcaption></figure>

* The downstream Excel file contains 2 Xoutputs (`Output1` & `Output2`), where `Output1` is a single value and `Ouput2` is a separate JSON object having its own outputs.

<figure><img src="/files/eR8YBqaKzd1P8tVujUP3" alt=""><figcaption><p>Upstream File</p></figcaption></figure>

* The upstream Excel file contains the `Xcall` block, JSON response and the user-defined functions (UDFs).

You can use the below files to follow along!

{% file src="/files/1WMkiPkzfaRjdODfmsfT" %}

{% file src="/files/LHBRV1zEpMeEizDeDLVG" %}

## Preparation

{% hint style="info" %}
This functionality requires the user to download **Spark Assistant Excel add-in**.
{% endhint %}

### Downstream service

1. The downstream service must be an existing service in the same Spark tenant.
2. You will need the downstream folder name, service name and a sample of the request body.

* Folder & Service name - You can check out the folder name and service name of downstream service on folder page. Note down the name on the folder card to perform the following steps.

<figure><img src="/files/4yC4eOsfbRLEbVezouU4" alt=""><figcaption></figcaption></figure>

* Request body – Select your downstream service and click on the *API tester* from the left side menu. Under *API Request (Inputs)*, you can see inputs in different formats. Click into *Raw view* and note down request body for the following steps.

<figure><img src="/files/WzpYrYgDYP1lDDnJHFf6" alt=""><figcaption><p>API Tester: Request Body Sample Under RAW View</p></figcaption></figure>

### Upstream service

#### Request

Create an XCall block in upstream service to make an API call. The XCall block can be anywhere in your workbook but must be on the same sheet as your `C.SPARK_UDFCALLAPI()` function.

This table needs the following mandatory fields:

* `CALLTYPE`: `SparkService`
* `FOLDERNAME`: name of folder containing downstream service.
* `SERVICENAME`: name of downstream service.
* `REQUESTBODY`: payload for calling downstream service. Must be in JSON format. You can copy the `REQUESTBODY` from [downstream service API tester](#downstream-service). If this is your first time using the SparkService we encourage to leave content of [Execute API (v3)](/spark-apis/execute-api/execute-api-v3.md#request_meta) empty. Spark will use the latest version and the default Service Type to execute this service. The `request_meta` follows the parameters that can be defined in [Execute API (v3)](/spark-apis/execute-api/execute-api-v3.md#request_meta).

{% hint style="info" %}
Make sure that `FOLDERNAME` and `SERVICENAME` match the uploaded service folder name and service name on Spark
{% endhint %}

<figure><img src="/files/J8eg1OTqCr8T6gXFCrno" alt=""><figcaption><p>XCall Block Example</p></figcaption></figure>

`REQUESTBODY` example (v3 format):

```json
{
    "request_data": {
        "inputs": {
            "InputField": "InputValue"
        }
    },
    "request_meta": {}
}
```

#### Response

With Spark Assistant, a user-defined function `C.SPARK_UDFCALLAPI([XCallTableRange])` can be defined in an Excel worksheet of the upstream service to receive API response from the downstream service.

* `[XCallTableRange]` is the location of XCall table in upstream service. e.g. `$A$1:$B$4`. You can also use a named range for convenience.
* The function must be on the same sheet as the XCallTable.
* The function will return a JSON formatted string with two parts.
  * The first, `metadata`, contains information about the call to the downstream service such as `status`, `error_code`, and `response_time`.
  * The second, `data`, contains the response body of the downstream service.

{% hint style="info" %}
Output of this function in Excel doesn't return the same one as Spark does. You may notice there is a `#NAME?` error in Excel. We suggest uploading the upstream service to Spark and check real outputs from in the [API Tester](/navigation/api-tester.md).
{% endhint %}

Response from Spark (example):

```json
{
    "metadata": {
        "name": null,
        "calltype": "SparkService",
        "status": "200",
        "error_code": "",
        "response_time": "225"
    },
    "data": {
        "status": "Success",
        "response_data": {
            "outputs": {
                "OutputField": "OutputValue"
            },
            "warnings": [],
            "errors": [],
            "service_chain": []
        },
        "response_meta": {
            "service_id": "166f5109-b01f-4d14-b10d-49669e3489e0",
            "version_id": "52e710a8-9ca1-4ab2-9366-dc304a9eb670",
            "version": "1.0.0",
            "process_time": 4,
            "call_id": "07ea654c-4136-4075-a195-7eaba15391c4",
            "compiler_type": "Type3",
            "compiler_version": "1.0.0",
            "source_hash": null,
            "engine_id": "F0D5B2B02645AAE15F63BABFBF94FBEF",
            "correlation_id": "",
            "system": "SPARK",
            "request_timestamp": "2022-02-28T16:14:25.148Z"
        },
        "error": null
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.coherent.global/build-spark-services/call-spark-service-apis/using-udfcallapi-legacy.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
