# Query analytics

`POST /v1/analytics/query`

Required scope: `analytics:read`

Metric values for a period, by member, team or not grouped. Each value comes with its sample size; below a metric's minimum sample it is null with the reason `not_enough_data`.

## Request body (application/json)

- `compare_previous` (boolean, optional) Default: `false`.
- `filters` (object, required): The days a query covers: a `period`, or a custom range `from` to `to` (both dates included, at most 731 days).
  - `from` (string (date) or null, optional)
  - `members` (string[], optional)
  - `owner` (string or null, optional)
  - `period` (string or null, optional) Allowed values: `"7d"`, `"30d"`, `"90d"`, `"365d"`, `"all"`.
  - `positions` (string[], optional)
  - `teams` (string[], optional)
  - `to` (string (date) or null, optional)
- `group_by` (string or null, optional) Allowed values: `"member"`, `"position"`, `"team"`, `"source"`, `"day"`, `"week"`, `"month"`, `"stage"`, `"category"`.
- `metrics` (string[], required)

## Responses

- `200`: Successful Response
- `400`: The request is not valid.
- `401`: Not signed in, or the credentials are not valid.
- `402`: The balance is too low.
- `403`: Not allowed to do this.
- `404`: Not found.
- `409`: Conflicts with the current state.
- `413`: The request body is too large.
- `415`: The content type is not supported.
- `422`: A value in the request is not valid.
- `429`: Too many requests. Wait for `Retry-After` seconds.
- `500`: Something went wrong on our side.
- `502`: A service Valeron depends on refused the request.
- `503`: A service Valeron depends on is unavailable.

## Examples

### Request

```bash title="cURL"
curl -X POST "https://api.valeron.io/v1/analytics/query" \
  -H "Authorization: Bearer $VALERON_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "compare_previous": false,
  "filters": {
    "from": "2026-12-03",
    "members": [
      "maya@acme.com"
    ],
    "owner": "example",
    "period": "7d",
    "positions": [
      "example"
    ],
    "teams": [
      "example"
    ]
  },
  "group_by": "member",
  "metrics": [
    "example"
  ]
}'
```

```js title="JavaScript"
const response = await fetch("https://api.valeron.io/v1/analytics/query", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.VALERON_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "compare_previous": false,
    "filters": {
      "from": "2026-12-03",
      "members": [
        "maya@acme.com"
      ],
      "owner": "example",
      "period": "7d",
      "positions": [
        "example"
      ],
      "teams": [
        "example"
      ]
    },
    "group_by": "member",
    "metrics": [
      "example"
    ]
  }),
});
if (!response.ok) throw new Error(`Valeron returned ${response.status}`);
const data = await response.json();
```

```python title="Python"
import os
import requests

response = requests.post(
    "https://api.valeron.io/v1/analytics/query",
    headers={"Authorization": f"Bearer {os.environ['VALERON_API_KEY']}"},
    json={
        "compare_previous": False,
        "filters": {
            "from": "2026-12-03",
            "members": [
                "maya@acme.com",
            ],
            "owner": "example",
            "period": "7d",
            "positions": [
                "example",
            ],
            "teams": [
                "example",
            ],
        },
        "group_by": "member",
        "metrics": [
            "example",
        ],
    },
)
response.raise_for_status()
data = response.json()
```

### Response 200

```json
{
  "data": [
    {
      "group": {
        "key": "example_key",
        "label": "Example"
      },
      "metrics": {
        "example_key": {
          "previous": {
            "reason": null,
            "sample_size": 1,
            "unknown_count": 3,
            "value": 1
          },
          "reason": null,
          "sample_size": 1,
          "unknown_count": 3,
          "value": 1
        }
      }
    }
  ],
  "group_by": "example",
  "metrics": [
    {
      "description": "A short description.",
      "key": "example_key",
      "label": "Example",
      "unit": "example"
    }
  ],
  "owner": "example",
  "period": {
    "ends_on": "2026-12-03",
    "label": "Example",
    "starts_on": "2026-12-03",
    "time_zone": "America/New_York",
    "value": "example"
  },
  "previous_period": {
    "ends_on": "2026-12-03",
    "label": "Example",
    "starts_on": "2026-12-03",
    "time_zone": "America/New_York",
    "value": "example"
  }
}
```

### Response 401

```json
{
  "error": null
}
```

### Response 402

```json
{
  "error": null
}
```

### Response 403

```json
{
  "error": null
}
```

### Response 404

```json
{
  "error": null
}
```
