---
type: api
title: Get the Elasticsearch query for a rule
description: **Spaces method and path for this operation:**
url: https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/kibana-serverless/operation/operation-get-alerting-rule-id-query-inspector
resource: https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/kibana-serverless/operation/operation-get-alerting-rule-id-query-inspector
products:
  - Kibana
---

# Get the Elasticsearch query for a rule

- Server: `https://{kibana_url}`

## Paths

- `GET` `/api/alerting/rule/{id}/query_inspector`
- `GET` `/s/{space_id}/api/alerting/rule/{id}/query_inspector`

#### Path parameters

- `id` (string) — required
  The identifier for the rule.

## Description

Refer to [Spaces](https://www.elastic.co/docs/deploy-manage/manage-spaces) for more information.

Returns the Elasticsearch query that a rule executes, and optionally its response.

## Authentication

- Api key: `Authorization: <value>`
  You must create an API key and use the encoded value in the request header. To learn about creating keys, go to [API keys](https://www.elastic.co/docs/current/serverless/api-keys).

## Query string parameters

- `mode` (enum · default: build) — optional
  The inspection mode. Use "build" to return only the query, or "execute" to run the query and include the response.
  Values: `build`, `execute`
- `alert_id` (string) — optional
  The alert document ID. When provided, the query inspector uses the evaluation time range from the alert instead of the current time.

## Responses

#### `200`

Indicates a successful call.

`application/json`

- `queries` (object[] · max items: 1000)
  - `index` (string)
  - `label` (string)
  - `request` (map string to object)
  - `response` (map string to object)
#### `400`

Indicates the rule type is not supported or the request parameters are invalid.

#### `404`

Indicates a rule with the specified rule ID does not exist.

## Code examples

#### Console

```console
GET kbn:/api/alerting/rule/ID/query_inspector
```

#### curl

```curl
curl -X GET "${KIBANA_URL}/api/alerting/rule/${ID}/query_inspector" \
  -H "Authorization: ApiKey ${API_KEY}"
```

## Response examples

#### A query inspector response for a custom threshold rule

```json
{
  "queries": [
    {
      "index": "metrics-*",
      "request": {
        "aggs": {
          "groupings": {
            "composite": {
              "size": 500,
              "sources": []
            }
          }
        },
        "query": {
          "bool": {
            "filter": [
              {
                "range": {
                  "@timestamp": {
                    "gte": "2026-01-01T00:00:00.000Z",
                    "lte": "2026-01-01T00:05:00.000Z"
                  }
                }
              }
            ]
          }
        },
        "size": 0
      }
    }
  ]
}
```

