# Query Analytics Data Source

Run one read-only SQL statement (DuckDB) over a data source's stored rows, read as `src`, to explore them before you build a panel: counts, distinct values, date ranges. Nothing is saved. It reads the rows the last fetch stored, so fetch first if they are old. For a source another member owns, send the id of a page shared with the creator that uses it.

`POST https://connect.mindcloud.co/v2/analytics/data-sources/:dataSourceId/query`

Permissions: `analytics:page:view`.

## Parameters

### Path

- `dataSourceId` (string, required): The data source's id, from List Analytics Data Sources or a panel's dataSources.

### 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

- `sql` (string, required): The SQL, reading the rows as src.
  One read-only SELECT statement over the stored rows, read as `src`. DuckDB SQL.
- `limit` (number): How many rows, 1 to 200.
  How many rows to answer, 1 to 200. Defaults to 200. Page past it with LIMIT and OFFSET in the SQL.
  Default: `200`
- `pageId` (string): For a source another member owns: a page shared with the key's creator that uses it, from List Analytics Pages.
  For a source another member owns: the id of a page shared with the key's creator that uses it, from List Analytics Pages.

## Example request

```bash
curl --request POST \
  --url 'https://connect.mindcloud.co/v2/analytics/data-sources/Ds3Hq9Lp2WxM/query' \
  --header 'Authorization: Bearer $MINDCLOUD_API_TOKEN' \
  --header 'Content-Type: application/json' \
  --data '{
  "sql": "SELECT stage, COUNT(*) AS deals FROM src GROUP BY stage ORDER BY deals DESC",
  "limit": 50,
  "pageId": "Pq7Lm2Xc9RtA"
}'
```

## Response fields

- `columns` (array of string): The column names of the answer, in order.
- `rows` (array of objects): The rows the SQL answered, as objects keyed by column.
- `hasMore` (boolean): True when more rows match than limit answered: narrow the SQL, or page with LIMIT and OFFSET in it.
