> For the complete documentation index, see [llms.txt](https://docs.redivis.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.redivis.com/api/rest-api/queries/post.md).

# post

## Overview

This endpoint is used for executing SQL queries against data stored in Redivis.

{% hint style="warning" %}
Note that API-initiated queries are subject to certain [quotas and limits](https://docs.redivis.com/reference/platform-essentials/quotas-and-limits.md#queries).
{% endhint %}

## HTTP Request

```http
POST /api/v1/queries
```

{% hint style="info" %}
This endpoint extends the [general API structure](https://docs.redivis.com/api/rest-api/general-structure.md)
{% endhint %}

## Request body

Provide a JSON object with information about the query.

<table><thead><tr><th width="187.1484375">Property name</th><th width="150.29427083333331">Type</th><th>Description</th></tr></thead><tbody><tr><td><strong>query</strong></td><td><strong>string</strong></td><td><p><strong>Required.</strong> The SQL query, following the standard Redivis SQL syntax. All table references in the query must be fully qualified, unless a <code>defaultWorkflow</code> or <code>defaultDataset</code> is provided (see below).</p><p>The query cannot be more than 256k characters.</p></td></tr><tr><td><strong>timeoutMs</strong></td><td><strong>integer</strong></td><td><strong>Optional, defaults to 30,000.</strong> The maximum amount of time, in milliseconds, the request should wait for the query to finish.<br><br>If the timeout is exceeded and the query is still running, the query will be returned with a status of <code>"running"</code> , and you will need to poll the query until its status is <code>"completed"</code> or <code>"failed"</code>.<br><br>The timeout must be between 0 and 60,000. It is generally simpler and more performant to have a higher timeout, as opposed to receiving a running query and then polling that for completion.</td></tr><tr><td><strong>defaultWorkflow</strong></td><td><strong>string</strong></td><td><p><strong>Optional.</strong> Cannot be present if <code>defaultDataset</code> is specified.</p><p>Should be specified as the fully qualified <a href="https://docs.redivis.com/api/referencing-resources.md">workflow reference</a>. Any tables in your query that aren't fully qualified will be assumed to be a part of this workflow.</p></td></tr><tr><td><strong>defaultDataset</strong></td><td><strong>string</strong></td><td><p><strong>Optional.</strong> Cannot be present if <code>defaultWorkflow</code> is specified.</p><p>Should be specified as the fully qualified <a href="https://docs.redivis.com/api/referencing-resources.md">dataset reference</a>. Any tables in your query that aren't fully qualified will be assumed to be a part of this dataset.</p></td></tr><tr><td><strong>listRows</strong></td><td><strong>object</strong></td><td><p><strong>Optional.</strong> If present, the query's rows are returned in place of the query when it finishes within <code>timeoutMs</code>, saving a separate request to <a href="https://docs.redivis.com/api/rest-api/queries/listrows.md">listRows</a>. See <a href="#returning-rows">returning rows</a> below.</p><p>Takes the same options as the <a href="https://docs.redivis.com/api/rest-api/queries/listrows.md">listRows</a> endpoint, all of which are optional:</p><ul><li><code>format</code> (string): <code>jsonl</code>, <code>csv</code>, or <code>arrow</code>. Defaults to <code>jsonl</code>.</li><li><code>maxResults</code> (integer): The maximum number of rows to return. Defaults to all rows.</li><li><code>selectedVariables</code> (array of strings): The variables to return, case insensitive. Defaults to all variables.</li><li><code>eosSentinel</code> (boolean): For the <code>arrow</code> format, end the stream with an empty record batch. Defaults to <code>false</code>.</li></ul></td></tr></tbody></table>

## Returning rows

When `listRows` is specified, the response is the query's rows if the query completes within `timeoutMs` and its output can be listed. The rows are exactly what [listRows](https://docs.redivis.com/api/rest-api/queries/listrows.md) would return for the same options.

Otherwise, the response is the query, just as if `listRows` were omitted. This happens when:

* The query is still running when `timeoutMs` is reached, or it failed.
* The output is too large to list in a single response (more than 100MB, scaled by `maxResults` if specified). Read it through a [read session](https://docs.redivis.com/api/rest-api/readsessions/post.md) instead.
* `selectedVariables` names a variable that isn't in the query's output.

Either way, the request succeeds with a `200` status. To tell which you received, check for the `Content-Location` header, which is only present when the response is the query's rows. Don't rely on the `Content-Type` alone: the JSON lines content type, `application/jsonl`, begins with `application/json`.

```javascript
const response = await fetch('https://redivis.com/api/v1/queries', {
	method: 'POST',
	headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
	body: JSON.stringify({ query: 'SELECT 1 AS a, 2 AS b', listRows: { format: 'csv' } }),
});
if (response.headers.get('Content-Location')) {
	const csv = await response.text(); // The query's rows
} else {
	const query = await response.json(); // The query; poll it, then read its rows
}
```

{% hint style="info" %}
The [client libraries](https://docs.redivis.com/api/client-libraries/redivis-js.md) do this for you whenever reading a query's rows also runs it.
{% endhint %}

## Authorization

Data access to all tables referenced by the query is required. If a table's data is not public, your access token must have one of the following scopes:

* data.data
* data.edit

[Learn more about authorization.](https://docs.redivis.com/api/rest-api/authorization.md)

## Response headers

| Header             |                                                                                                                                      |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------ |
| `Location`         | The URI of the query, e.g. `/api/v1/queries/:queryId`. Always present once the query has been created, including on error responses. |
| `Content-Location` | The URI of the query's rows, e.g. `/api/v1/queries/:queryId/rows`. Only present when the response is the query's rows.               |

## Response body

By default, returns the JSON-encoded "get" representation of a [query resource](https://docs.redivis.com/api/resource-definitions/query.md#get).

If `listRows` was specified and the query's rows are returned instead (see [returning rows](#returning-rows)), the response body follows the [table.listRows specification](https://docs.redivis.com/api/rest-api/tables/listrows.md#response-body) for the requested `format`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.redivis.com/api/rest-api/queries/post.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
