> ## Documentation Index
> Fetch the complete documentation index at: https://docs.synq.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Cube

> Integrating Cube with Coalesce Quality

> [!NOTE]
>
> This guide shows how to connect a Cube deployment to Coalesce Quality. After the setup, each cube and each view of the deployment shows in Coalesce Quality as a semantic model, with lineage to your warehouse tables.
>
> It works for Cube Cloud and for self-hosted Cube Core, also for a deployment in a private network.
>
> ⏱️ Estimated time to finish: 15 minutes.

## How it works

Coalesce Quality reads the data model of the deployment. There are two ways, and they give the same result:

* Coalesce Quality connects to Cube Cloud or to the Cube REST API and reads the data model on a schedule.
* You run `synq-cube`, a small open source tool, beside Cube. It reads the data model and sends it to Coalesce Quality. Use it for a deployment in a private network: you open no port and allow no IP address.

The two ways send the same requests to the Cube REST API. They are the same on Cube Core and on Cube Cloud.

| Request | Content |
| - | - |
| `GET /cubejs-api/v1/meta?extended` | The compiled data model: the cubes and the views with their measures, dimensions, segments, joins, hierarchies, folders and pre-aggregations |
| `POST /cubejs-api/v1/sql` | The SQL that Cube generates for the measures and the dimensions of each cube and each view |

Cube compiles the SQL and does not run it, so no query reaches your warehouse. Coalesce Quality never requests `/v1/load` and reads no data rows.

On Cube Cloud, Coalesce Quality also reads the model export and the list of data sources from the Cube Cloud API.

## What you get

* One asset for each cube and each view, with the title and the description from the data model. The assets are in the folders of their files in the data model.
* The semantic model of each: measures, dimensions and segments with their SQL, the type of each measure, measure filters, the primary key, the joins of a cube, and which members are not public.
* Lineage from the warehouse tables to the cubes, and from the cubes to the views. Column lineage goes from a warehouse column to the measure or the dimension that reads it, and then to the member of each view that includes it.
* The compiled definition of each cube and view on its Code tab. It is the data model after the render of Jinja templates and the merge of `extends`.
* A link from each cube and view to its file in your git repository, and to your deployment.

On Cube Cloud, a multi-stage measure also shows its settings (`time_shift`, `reduce_by`, `group_by`). The REST API of Cube Core does not return them, so on Cube Core the measure shows with its SQL only.

## Before you start

You need:

* A credential for Cube: an API key of Cube Cloud, or a credential for the Cube REST API (see [The Cube token](#the-cube-token)).
* For lineage to the warehouse: the warehouse of each Cube data source connected to Coalesce Quality as a [data warehouse integration](https://docs.synq.io/dw-integrations/snowflake.md).

## Set up the integration

1. In Coalesce Quality, go to **Settings → Integrations** and add a **Cube** integration.
2. Choose how Coalesce Quality gets the data model. You choose it one time. To use another way later, create a new integration.
3. Enter the connection of that way:

> [tab] Cube Cloud
>
> Enter the URL of your Cube Cloud account (`https://example.cubecloud.dev`) and an API key. Then select the deployment and its environment from the list.
>
> Coalesce Quality creates a token for the deployment with the API key and reads the data model on a schedule. The default is each hour.
>
> The API key needs three permissions: the list of the deployments, the list of the environments of the deployment and the token for its metadata. Add a fourth one, the settings of the deployment, for the links to git (see [Links to git](#links-to-git)). The [synq-cube repository](https://github.com/getsynq/synq-cube#cube-cloud) has the request that creates such a key.

> [tab] Cube REST API
>
> Use this for a self-hosted Cube that Coalesce Quality can reach. If you limit access by IP address, allow the [Coalesce Quality IP addresses](https://docs.synq.io/security/ip.md).
>
> Enter the API URL with the base path, for example `https://cube.example.com/cubejs-api`, and one credential:
>
> * The API secret of the deployment, the value of `CUBEJS_API_SECRET`. Coalesce Quality signs a short-lived token with it.
> * Or a JSON Web Token that you made. Use it if your deployment checks tokens with JWKS.
>
> Coalesce Quality reads the data model on a schedule. The default is each hour.

> [tab] Uploader
>
> Use this for a Cube deployment in a private network. Finish the setup and copy the token that it shows. This is `QUALITY_TOKEN`. Then run the `synq-cube` uploader in a place that can reach Cube: a CI job, a Kubernetes CronJob or a machine beside Cube.
>
> For Cube Core:
>
> ```bash theme={null}
> docker run --rm \
>   -e CUBE_API_URL=http://cube:4000/cubejs-api \
>   -e CUBE_API_SECRET \
>   -e QUALITY_TOKEN \
>   -e QUALITY_REGION=eu \
>   ghcr.io/getsynq/synq-cube:latest
> ```
>
> `CUBE_API_SECRET` is the value of `CUBEJS_API_SECRET` of the deployment. To give the tool what each data source is, run it with the same env file as Cube (`--env-file cube.env`). It reads the database type, the database and the host from the `CUBEJS_DB_*` variables, and it reads no credential.
>
> For Cube Cloud:
>
> ```bash theme={null}
> docker run --rm \
>   -e CUBE_CLOUD_URL=https://example.cubecloud.dev \
>   -e CUBE_CLOUD_API_KEY \
>   -e CUBE_CLOUD_DEPLOYMENT_ID=42 \
>   -e QUALITY_TOKEN \
>   -e QUALITY_REGION=eu \
>   ghcr.io/getsynq/synq-cube:latest
> ```
>
> `QUALITY_REGION` is the region of your workspace: `eu`, `us` or `au`.
>
> To see what leaves your network before you send it, write the snapshot to a file. This sends nothing:
>
> ```bash theme={null}
> synq-cube upload --dry-run --output snapshot.json
> ```
>
> The file is the snapshot in JSON. You can look at it, and send the same file with `synq-cube upload --input snapshot.json`.
>
> Each snapshot is the whole data model. Send one after each deploy of the data model, or on a schedule.
>
> The tool is open source. You can read the code, build it and change it. All settings, a CronJob, a CI step and the steps for an AI agent that sets it up are in the [synq-cube repository](https://github.com/getsynq/synq-cube). There are binaries for Linux, macOS and Windows on its releases page.

4. Optional: name the warehouse integration of a Cube data source (see [Lineage to the warehouse](#lineage-to-the-warehouse)) and choose the git repository of the data model (see [Links to git](#links-to-git)).
5. Check the values on the last step and submit.

Coalesce Quality gives the deployment a name: the name of the deployment in Cube Cloud, the host of the API URL, or the name of the integration. If another Cube integration has that name, the setup asks for a new one. The name is a part of the ID of each cube and view (see [Asset IDs](#asset-ids)), so it does not change after the setup.

## Check the result

After some minutes, the cubes and the views show in the catalog of Coalesce Quality, in the folders of their files in the data model. Open one cube and check its measures, its dimensions and its lineage to the warehouse tables.

The Cube integration shows the time and the result of the last read. If a part of the data model has no lineage, the integration says which data source or which members, and why.

The uploader also prints what it sent:

```text theme={null}
Cubes: 14
Views: 6
Members: 254
Members with SQL: 254, in 39 SQL statements
Members with an error: 0
Data source default: snowflake, database ANALYTICS, host ab12345.eu-west-1 (from the environment of Cube)
Git repository: https://github.com/example/analytics, branch main, commit 0123abc (from the variables of GitHub Actions)
Sent the snapshot to Coalesce Quality, eu (developer.synq.io:443)
```

## Lineage to the warehouse

Each cube reads one data source of Cube, for example `default`. Coalesce Quality must know which of your warehouse integrations a data source is.

* On Cube Cloud, Coalesce Quality reads what each data source is from Cube Cloud.
* The uploader reads it from the environment of Cube, or from its `--data-source` setting.
* If nothing says what a data source is and your workspace has one warehouse integration, the data source is that warehouse.

Coalesce Quality then finds the warehouse integration with the database of each table. If a cube has no lineage to its tables, name the warehouse integration of its data source in the **Data sources** section of the Cube integration. That setting always wins.

## Links to git

Coalesce Quality links each cube and each view to its file in your git repository. For the link, connect the repository to Coalesce Quality as a code integration: [GitHub](https://docs.synq.io/code-integrations/github.md), [GitLab](https://docs.synq.io/code-integrations/gitlab.md) or [git over SSH](https://docs.synq.io/code-integrations/ssh-git.md).

You set the repository in the **Git repository** step of the setup, or in the **Git repository** section of the Cube integration.

> [tab] Cube Cloud
>
> Cube Cloud gives the branch of the deployment, so you need no setup. Coalesce Quality finds the Git integration that has the files of the data model on that branch, and the directory of the data model in it. The **Git repository** step shows the branch and the project directory that Cube Cloud gives.
>
> * The project directory needs the permission `DeploymentsPublicController.getDeploymentSettings` on the API key. Without it, the links still work if the data model is in a directory that Coalesce Quality can find, and the integration shows a warning.
> * If no Git integration has the files, or more than one has them, select the Git integration in the **Git repository** section. You can also set the branch and the data model directory there. Leave them empty to use the values from Cube Cloud.
> * If Cube Cloud hosts the repository of the deployment, Coalesce Quality cannot read it, and the cubes and the views have no links to their files. To get the links, connect the deployment to your own Git repository in Cube Cloud, then add a Git integration for it.

> [tab] Cube REST API and Uploader
>
> Select the Git integration and the branch that the deployment runs. Enter the data model directory, for example `model`. Leave it empty if the data model is in the root of the repository.
>
> The uploader can also send the repository: it reads the URL, the branch and the commit from the variables of GitHub Actions or GitLab CI, or from the git checkout that it runs in. A value of the Cube integration wins over the value that the uploader sends.

The link of a file names its branch. If the deployment runs another branch than the one that the Git integration reads, the link breaks.

If Coalesce Quality cannot link the files, the Cube integration says why and what to change.

## The Cube token

The data model that Coalesce Quality shows is the data model that the token can see.

### API scopes

The token needs the API scopes `meta` and `sql`. Cube gives both by default. If your deployment sets `context_to_api_scopes`, give the two scopes to the security context that you use for Coalesce Quality. The `data` scope is not necessary.

Without the `sql` scope, the cubes and the views still show, and the lineage to the warehouse tables is absent.

### Read access to all members

Access policies and `query_rewrite` apply to the security context of the token. A cube, a view or a member that is hidden from that security context is not in the data model that Coalesce Quality reads. Give the security context read access to all cubes, views and members. Set it as a JSON object in the integration, or in `CUBE_SECURITY_CONTEXT` of the uploader.

### One integration for each tenant

A deployment with `context_to_app_id` compiles one data model for each tenant. One Cube integration is one data model, so create one integration for each tenant, each with its own deployment name and the security context of that tenant.

## Asset IDs

Each cube and each view has an `entity_id`. The deployment name is a part of it, so a new deployment name gives each cube and each view a new `entity_id`. Do not build an `entity_id` from its parts. Get it from the Cube name, as [Find a cube or a view by its Cube name](#find-a-cube-or-a-view-by-its-cube-name) shows.

### Find a cube or a view by its Cube name

Each cube and each view has an external ID: the ID of the Cube integration and the name in the data model, as Cube spells it. Cube names are case sensitive, and so is the external ID.

| Asset | `external_system` | `external_id` |
| - | - | - |
| Cube `orders` | `cube_cube_name` | `<integration ID>/orders` |
| View `orders_overview` | `cube_view_name` | `<integration ID>/orders_overview` |

The integration ID is in the address of the settings page of the Cube integration: `/settings/integrations/<integration ID>`. `ListIntegrations` (`GET /api/integrations/v1`) also returns it. The external ID does not contain the deployment name, so it stays the same when the deployment name changes.

`BatchResolveIdentifiers` gives the `entity_id` of each external ID. `QUALITY_API_ENDPOINT` is `developer.synq.io` (EU), `api.us.synq.io` (US) or `api.au.synq.io` (AU). `TOKEN` is an access token of the [API](https://docs.synq.io/custom-integrations/building-an-integration.md#credentials):

```bash theme={null}
curl -X POST "https://$QUALITY_API_ENDPOINT/api/resolve/v1/by-id" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "ids": [
      {"externalId": {"externalSystem": "cube_view_name", "externalId": "<integration ID>/orders_overview"}}
    ],
    "checkExistence": true
  }'
```

```json theme={null}
{
  "resolvedIdentifiers": [
    {
      "id": {
        "entityId": "<entity_id>",
        "externalId": {"externalSystem": "cube_view_name", "externalId": "<integration ID>/orders_overview"}
      },
      "synqPath": "<entity_id>",
      "identities": [ ... ],
      "identitySynqPaths": [ ... ],
      "exists": true
    }
  ]
}
```

The response has one entry for each different external ID, in the order of its first occurrence in the request. Use `id.entityId` of an entry only if its `exists` is `true`. If no cube or view has the external ID, `exists` is `false`.

### Point a custom entity at a view

A [custom entity](https://docs.synq.io/custom-integrations/overview.md), for example a dashboard that queries Cube, can read a view. Resolve the view as above. Then write the relationship with the `entity_id` in the `synq_path` variant:

```bash theme={null}
curl -X POST "https://$QUALITY_API_ENDPOINT/api/entities/custom/v1/relationships" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "relationships": [
      {
        "upstream": {"synqPath": {"path": "<entity_id>"}},
        "downstream": {"custom": {"id": "<your dashboard ID>"}}
      }
    ]
  }'
```

Resolve the external IDs again each time the script runs. Other calls do not take an external ID: only `BatchResolveIdentifiers` reads it.

## How removal works

Each read gives Coalesce Quality the whole data model. A cube or a view that is no longer in the data model is removed from Coalesce Quality. If you leave a part of the data model out with `--include`, `--exclude` or `--skip-hidden` of the uploader, use the same setting on each run.

If the data model that the token sees is empty, Coalesce Quality removes nothing and the integration shows an error.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.