# Preview Analytics Merge

Try a merge before you save it, as the app's merge editor previews one: send the same dataSources and transformCode Create Analytics Data Source takes for a merge source. It answers each input after its SQL and what the code returns. Nothing is saved. A code error comes back in output.error, so you can fix it and try again.

`POST https://connect.mindcloud.co/v2/analytics/data-sources/merge-preview`

Permissions: `analytics:page:create`.

## Parameters

### Headers

- `X-Company-Id` (string): Optional. The id of the organization to act in. Leave it out to act in the API key's own organization. Here a key acts in another organization only as one of its members: with "Use this key across all of your organizations" on, in an organization its creator belongs to with a role. Child organizations and MindCloud staff access do not reach this endpoint. Every response's X-Company-Id header names the organization it acted in. See [Organizations](https://mindcloud.co/docs/api/rest/introduction/organizations).

### Body

- `dataSources` (array, required): The inputs, each one of your sync or fixed sources with an alias and optional SQL.
  The sources to combine, as Create Analytics Data Source takes them for a merge source.
  - `dataSourceId` (string, required): The input source, a sync or fixed source of yours, from List Analytics Data Sources.
  - `alias` (string, required): The name the merge SQL and code read this input by.
  - `query` (string): SQL that shapes this input before the merge, reading it as `src`. Leave it out to use the input as is.
- `transformCode` (string): The code that combines them.
  The code that combines them, as for a merge source. Leave it out to see the inputs only.

## Example request

```bash
curl --request POST \
  --url 'https://connect.mindcloud.co/v2/analytics/data-sources/merge-preview' \
  --header 'Authorization: Bearer $MINDCLOUD_API_TOKEN' \
  --header 'Content-Type: application/json' \
  --data '{
  "dataSources": [
    {
      "dataSourceId": "Ds3Hq9Lp2WxM",
      "alias": "deals"
    },
    {
      "dataSourceId": "Ds9Kp4Wm7TxB",
      "alias": "targets"
    }
  ],
  "transformCode": "return deals.map((deal) => ({ ...deal, target: targets.find((row) => row.month === deal.month)?.target ?? null }));"
}'
```

## Response fields

- `inputs` (array of objects): Each input after its SQL: its alias and its first 10 rows.
  - `alias` (string): The alias the code reads it by.
  - `rowCount` (number): How many rows there are in all.
  - `columns` (array): The column names.
  - `rows` (array): The first rows.
- `isPartial` (boolean): True when an input is still on its first fetch, so only its rows so far were used.
- `output` (object): What the code returned: its first 50 rows, or error when it failed. Null when no transformCode was sent.
  - `rowCount` (number): How many rows there are in all.
  - `columns` (array): The column names.
  - `rows` (array): The first rows.
  - `error` (string): Why the code failed, or null when it ran.
