---
title: "Run an RQL query"
description: "Runs one RQL (Respan Query Language) query over your project's spans, traces, sessions, users, scores and verdicts, and returns one page of rows. It is the same query language as the SQL editor on Metrics. Set `start_time` and `end_time`, or filter on time in `WHERE`, so large projects do not time out. Runs are limited to 30 seconds and 120 per minute per user."
---

**POST** `https://api.respan.ai/api/query/`

Runs one RQL (Respan Query Language) query over your project's spans, traces, sessions, users, scores and verdicts, and returns one page of rows. It is the same query language as the SQL editor on Metrics. Set `start_time` and `end_time`, or filter on time in `WHERE`, so large projects do not time out. Runs are limited to 30 seconds and 120 per minute per user.

## Servers

`https://api.respan.ai`: Respan API Server

## Authentication

### RespanApiKey

Use your Respan API key for Respan API authentication. Enter only the Respan API key value; clients send Authorization: Bearer &lt;RESPAN_API_KEY&gt;. For /api/responses, provider credentials such as Perplexity, OpenAI, or Azure OpenAI go in Settings -&gt; Providers or respan_params.credential_override in the request body, not in this authentication field.

HTTP scheme: `bearer`; format: `Respan API key`.


```http
Authorization: Bearer $RESPAN_API_KEY
```


## Request body

A request body is required.

### application/json


| Field | Type | Requirement | Description and constraints |
| --- | --- | --- | --- |
| `$` | `object` | Required body | Reference: `#/components/schemas/RqlQueryRequest`<br />Additional properties are not allowed |
| `query` | `string or object` | Required in parent | The query: RQL text, or its JSON tree. Text is at most 10,000 characters. |
| `query (oneOf 1)` | `string` | Schema variant | - |
| `query (oneOf 2)` | `object` | Schema variant | Additional properties are allowed |
| `start_time` | `string` | Optional | Optional. Rows at or after this time. Without it the query reads all your history unless it filters on time itself.<br />Format: `date-time` |
| `end_time` | `string` | Optional | Optional. Rows before this time. Must be after `start_time`.<br />Format: `date-time` |
| `timezone` | `string` | Optional | Optional IANA zone for `bucket` and `date_trunc`. UTC by default.<br />Maximum length: `64` |
| `page_size` | `integer` | Optional | Rows per page.<br />Default: `100`<br />Minimum: `1`<br />Maximum: `1000` |
| `cursor` | `string` | Optional | `next_cursor` of the previous page. Send it with the same query and time range. |
| `is_dry_run` | `boolean` | Optional | Check and translate the query without running it. The response then has no `rows`.<br />Default: `false` |


<details class="api-schema-source">
<summary>OpenAPI request schema</summary>


```json
{
  "$ref": "#/components/schemas/RqlQueryRequest"
}
```

</details>

##### Example


```json
{
  "query": "SELECT model, count() AS requests, avg(cost) AS avg_cost FROM spans GROUP BY model ORDER BY requests DESC LIMIT 3",
  "start_time": "2026-11-16T00:00:00Z",
  "end_time": "2026-11-23T00:00:00Z",
  "page_size": 100
}
```


## Examples

### Requests and cost per model

Response captured from a live call on 2026-11-23.

#### Request


```json
{
  "query": "SELECT model, count() AS requests, avg(cost) AS avg_cost FROM spans GROUP BY model ORDER BY requests DESC LIMIT 3",
  "start_time": "2026-11-16T00:00:00Z",
  "end_time": "2026-11-23T00:00:00Z",
  "page_size": 100
}
```


#### Response


```json
{
  "body": {
    "columns": [
      {
        "name": "model",
        "type": "string"
      },
      {
        "name": "requests",
        "type": "integer"
      },
      {
        "name": "avg_cost",
        "type": "float"
      }
    ],
    "query": {
      "from": "spans",
      "select": [
        {
          "name": "model",
          "expr": {
            "field": "model"
          }
        },
        {
          "name": "requests",
          "expr": {
            "call": "count",
            "args": []
          }
        },
        {
          "name": "avg_cost",
          "expr": {
            "call": "avg",
            "args": [
              {
                "field": "cost"
              }
            ]
          }
        }
      ],
      "order_by": [
        {
          "expr": {
            "ref": "requests"
          },
          "desc": true
        }
      ],
      "limit": 3
    },
    "sql": "SELECT model, count() AS requests, avg(cost) AS avg_cost FROM spans GROUP BY model ORDER BY requests DESC LIMIT 3",
    "rows": [
      [
        "respan-uncensored/deepseek-4.1-flash",
        209,
        0
      ],
      [
        "claude-sonnet-5.5",
        179,
        0.009622201117318431
      ],
      [
        "openai/gpt-5.1",
        125,
        0.0061490020000000015
      ]
    ],
    "next_cursor": null,
    "execution_time_ms": 60
  }
}
```


## Responses

### 200

One page of the result.

#### application/json


| Field | Type | Requirement | Description and constraints |
| --- | --- | --- | --- |
| `$` | `object` | Body | Reference: `#/components/schemas/RqlQueryResponse` |
| `columns` | `array<object>` | Required in parent | - |
| `columns[]` | `object` | Array item | Reference: `#/components/schemas/RqlQueryColumn` |
| `columns[].name` | `string` | Required in parent | - |
| `columns[].type` | `string` | Required in parent | boolean, integer, float, string, time, interval, map, null or unknown. |
| `rows` | `array<array<any>>` | Optional | One array per row, in `columns` order. Absent on a dry run. |
| `rows[]` | `array<any>` | Array item | - |
| `rows[][]` | `any` | Array item | - |
| `next_cursor` | `string or null` | Optional | Send as `cursor` for the next page. Null on the last page. |
| `query` | `object` | Required in parent | The query as its JSON tree.<br />Additional properties are allowed |
| `sql` | `string` | Required in parent | The query as canonical RQL. |
| `execution_time_ms` | `integer` | Optional | - |


<details class="api-schema-source">
<summary>OpenAPI response schema</summary>


```json
{
  "$ref": "#/components/schemas/RqlQueryResponse"
}
```

</details>

### 400

The query or request is invalid.

#### application/json


| Field | Type | Requirement | Description and constraints |
| --- | --- | --- | --- |
| `$` | `object` | Body | Reference: `#/components/schemas/RqlQueryError` |
| `code` | `string` | Required in parent | Stable and machine-readable, such as `syntax_error`, `unsupported_feature`, `invalid_query`, `unknown_entity`, `unknown_field`, `unknown_function`, `invalid_cursor` or `timeout`. |
| `detail` | `string` | Required in parent | - |
| `line` | `integer` | Optional | 1-based, when the error points into the query text. |
| `column` | `integer` | Optional | - |


<details class="api-schema-source">
<summary>OpenAPI response schema</summary>


```json
{
  "$ref": "#/components/schemas/RqlQueryError"
}
```

</details>

### 403

The API key is missing, invalid or expired.

#### application/json


| Field | Type | Requirement | Description and constraints |
| --- | --- | --- | --- |
| `$` | `object` | Body | - |
| `detail` | `string` | Required in parent | - |


<details class="api-schema-source">
<summary>OpenAPI response schema</summary>


```json
{
  "type": "object",
  "required": [
    "detail"
  ],
  "properties": {
    "detail": {
      "type": "string"
    }
  }
}
```

</details>

##### Example


```json
{
  "detail": "Your API key is invalid or expired, please check your API key in the platform settings."
}
```


## OpenAPI source

The operation and referenced components below retain the complete OpenAPI definitions, including constraints and examples.

<!-- openapi-operation:start -->
<details class="api-schema-source">
<summary>OpenAPI operation</summary>


```json
{
  "summary": "Run an RQL query",
  "operationId": "runRqlQuery",
  "tags": [
    "Query"
  ],
  "description": "Runs one RQL (Respan Query Language) query over your project's spans, traces, sessions, users, scores and verdicts, and returns one page of rows. It is the same query language as the SQL editor on Metrics. Set `start_time` and `end_time`, or filter on time in `WHERE`, so large projects do not time out. Runs are limited to 30 seconds and 120 per minute per user.",
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "$ref": "#/components/schemas/RqlQueryRequest"
        },
        "example": {
          "query": "SELECT model, count() AS requests, avg(cost) AS avg_cost FROM spans GROUP BY model ORDER BY requests DESC LIMIT 3",
          "start_time": "2026-11-16T00:00:00Z",
          "end_time": "2026-11-23T00:00:00Z",
          "page_size": 100
        }
      }
    }
  },
  "responses": {
    "200": {
      "description": "One page of the result.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/RqlQueryResponse"
          }
        }
      }
    },
    "400": {
      "description": "The query or request is invalid.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/RqlQueryError"
          }
        }
      }
    },
    "403": {
      "$ref": "#/components/responses/InvalidApiKey"
    }
  },
  "security": [
    {
      "RespanApiKey": []
    }
  ],
  "x-fern-examples": [
    {
      "name": "Requests and cost per model",
      "description": "Response captured from a live call on 2026-11-23.",
      "request": {
        "query": "SELECT model, count() AS requests, avg(cost) AS avg_cost FROM spans GROUP BY model ORDER BY requests DESC LIMIT 3",
        "start_time": "2026-11-16T00:00:00Z",
        "end_time": "2026-11-23T00:00:00Z",
        "page_size": 100
      },
      "response": {
        "body": {
          "columns": [
            {
              "name": "model",
              "type": "string"
            },
            {
              "name": "requests",
              "type": "integer"
            },
            {
              "name": "avg_cost",
              "type": "float"
            }
          ],
          "query": {
            "from": "spans",
            "select": [
              {
                "name": "model",
                "expr": {
                  "field": "model"
                }
              },
              {
                "name": "requests",
                "expr": {
                  "call": "count",
                  "args": []
                }
              },
              {
                "name": "avg_cost",
                "expr": {
                  "call": "avg",
                  "args": [
                    {
                      "field": "cost"
                    }
                  ]
                }
              }
            ],
            "order_by": [
              {
                "expr": {
                  "ref": "requests"
                },
                "desc": true
              }
            ],
            "limit": 3
          },
          "sql": "SELECT model, count() AS requests, avg(cost) AS avg_cost FROM spans GROUP BY model ORDER BY requests DESC LIMIT 3",
          "rows": [
            [
              "respan-uncensored/deepseek-4.1-flash",
              209,
              0
            ],
            [
              "claude-sonnet-5.5",
              179,
              0.009622201117318431
            ],
            [
              "openai/gpt-5.1",
              125,
              0.0061490020000000015
            ]
          ],
          "next_cursor": null,
          "execution_time_ms": 60
        }
      }
    }
  ]
}
```

</details>
<!-- openapi-operation:end -->

<!-- openapi-path-item:start -->
<details class="api-schema-source">
<summary>OpenAPI path metadata</summary>


```json
{}
```

</details>
<!-- openapi-path-item:end -->

<!-- openapi-components:start -->
<details class="api-schema-source">
<summary>Referenced OpenAPI components</summary>


```json
{
  "responses": {
    "InvalidApiKey": {
      "description": "The API key is missing, invalid or expired.",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "required": [
              "detail"
            ],
            "properties": {
              "detail": {
                "type": "string"
              }
            }
          },
          "example": {
            "detail": "Your API key is invalid or expired, please check your API key in the platform settings."
          }
        }
      }
    }
  },
  "schemas": {
    "RqlQueryColumn": {
      "type": "object",
      "required": [
        "name",
        "type"
      ],
      "properties": {
        "name": {
          "type": "string"
        },
        "type": {
          "type": "string",
          "description": "boolean, integer, float, string, time, interval, map, null or unknown."
        }
      }
    },
    "RqlQueryError": {
      "type": "object",
      "required": [
        "code",
        "detail"
      ],
      "properties": {
        "code": {
          "type": "string",
          "description": "Stable and machine-readable, such as `syntax_error`, `unsupported_feature`, `invalid_query`, `unknown_entity`, `unknown_field`, `unknown_function`, `invalid_cursor` or `timeout`."
        },
        "detail": {
          "type": "string"
        },
        "line": {
          "type": "integer",
          "description": "1-based, when the error points into the query text."
        },
        "column": {
          "type": "integer"
        }
      }
    },
    "RqlQueryRequest": {
      "type": "object",
      "required": [
        "query"
      ],
      "properties": {
        "query": {
          "description": "The query: RQL text, or its JSON tree. Text is at most 10,000 characters.",
          "oneOf": [
            {
              "type": "string"
            },
            {
              "type": "object",
              "additionalProperties": true
            }
          ]
        },
        "start_time": {
          "type": "string",
          "format": "date-time",
          "description": "Optional. Rows at or after this time. Without it the query reads all your history unless it filters on time itself."
        },
        "end_time": {
          "type": "string",
          "format": "date-time",
          "description": "Optional. Rows before this time. Must be after `start_time`."
        },
        "timezone": {
          "type": "string",
          "maxLength": 64,
          "description": "Optional IANA zone for `bucket` and `date_trunc`. UTC by default."
        },
        "page_size": {
          "type": "integer",
          "minimum": 1,
          "maximum": 1000,
          "default": 100,
          "description": "Rows per page."
        },
        "cursor": {
          "type": "string",
          "description": "`next_cursor` of the previous page. Send it with the same query and time range."
        },
        "is_dry_run": {
          "type": "boolean",
          "default": false,
          "description": "Check and translate the query without running it. The response then has no `rows`."
        }
      },
      "additionalProperties": false
    },
    "RqlQueryResponse": {
      "type": "object",
      "required": [
        "columns",
        "query",
        "sql"
      ],
      "properties": {
        "columns": {
          "type": "array",
          "items": {
            "$ref": "#/components/schemas/RqlQueryColumn"
          }
        },
        "rows": {
          "type": "array",
          "description": "One array per row, in `columns` order. Absent on a dry run.",
          "items": {
            "type": "array",
            "items": {}
          }
        },
        "next_cursor": {
          "type": "string",
          "nullable": true,
          "description": "Send as `cursor` for the next page. Null on the last page."
        },
        "query": {
          "type": "object",
          "additionalProperties": true,
          "description": "The query as its JSON tree."
        },
        "sql": {
          "type": "string",
          "description": "The query as canonical RQL."
        },
        "execution_time_ms": {
          "type": "integer"
        }
      }
    }
  },
  "securitySchemes": {
    "RespanApiKey": {
      "type": "http",
      "scheme": "bearer",
      "bearerFormat": "Respan API key",
      "description": "Use your Respan API key for Respan API authentication. Enter only the Respan API key value; clients send Authorization: Bearer <RESPAN_API_KEY>. For /api/responses, provider credentials such as Perplexity, OpenAI, or Azure OpenAI go in Settings -> Providers or respan_params.credential_override in the request body, not in this authentication field.",
      "x-fern-bearer": {
        "name": "respanApiKey",
        "env": "RESPAN_API_KEY"
      }
    }
  }
}
```

</details>
<!-- openapi-components:end -->
