> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getcollate.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Saved Queries

> Save, list, update, and execute reusable queries through the Collate Query Runner API

# Saved Queries

A saved query is a named, reusable SQL statement tied to a database service. List, retrieve, update, and delete operations enforce ownership. The execute route has a separate access limitation described below.

| Method | Endpoint | Description |
| - | - | - |
| `POST` | `/v1/queryRunner/savedQueries` | Create a saved query under a service, owned by the calling user. |
| `GET` | `/v1/queryRunner/savedQueries` | List saved queries owned by the calling user, optionally filtered with `?service=`. |
| `GET` | `/v1/queryRunner/savedQueries/{id}` | Get a saved query by ID. Owner only. |
| `PUT` | `/v1/queryRunner/savedQueries/{id}` | Update a saved query's name or query text. Owner only. |
| `DELETE` | `/v1/queryRunner/savedQueries/{id}` | Delete a saved query. Owner only. |
| `POST` | `/v1/queryRunner/savedQueries/{id}/execute` | Validate that the query is read-only and return execution metadata, including its SQL text. |

`POST /v1/queryRunner/savedQueries` takes `serviceName`, `name`, and `queryText`, all required. `PUT` accepts `name` and/or `queryText` to update.

<Note>
  **Note**: `POST .../execute` checks the query is read-only and returns metadata about it. It doesn't run the query itself.
</Note>

To actually execute it, pass its `queryText` to [Query Execution](/ai-2-0/developer-guide/api-reference/query-runner/execution).

<Warning>
  In Collate 2.0.2, the execute route does not check saved-query ownership. An authenticated caller with access to the database service who knows a saved-query ID can receive its read-only SQL text. Do not treat saved-query text as owner-only until this route enforces ownership.
</Warning>

## Example

The following request saves a query.

<RequestExample>
  ```bash Create a saved query theme={null}
  curl -X POST "{base_url}/api/v1/queryRunner/savedQueries" \
    -H "Authorization: Bearer {access_token}" \
    -H "Content-Type: application/json" \
    -d @- <<'JSON'
  {
      "serviceName": "snowflake_production",
      "name": "Monthly Active Users",
      "queryText": "SELECT date_trunc('month', event_ts), count(distinct user_id) FROM events GROUP BY 1"
  }
  JSON
  ```
</RequestExample>
