> For the complete documentation index, see [llms.txt](https://docs.concurrence.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.concurrence.com/developer-guide/platform-api/functions/workspace-data-queries.md). Agents can also search and read this documentation through its [MCP server](https://docs.concurrence.com/~gitbook/mcp).

# Workspace Data Queries

Register, manage, and invoke parameterized SQL queries against workspace custom tables.

Workspace data queries let you register parameterized SQL templates that the agent can invoke as tools during conversations. Unlike platform functions (which execute against an external data warehouse), data queries run directly against the custom tables you have provisioned in your workspace.

The agent runtime registers each query as a tool named `wsq_<name>`, so the agent can call it mid-conversation to fetch data from your workspace's custom tables.

## Prerequisites

Your workspace must have at least one custom table provisioned through [Workspace Tables](https://docs.concurrence.com/developer-guide/platform-api/data-world-model/workspace-tables.md) before you can deploy a data query.

`query_id` (a UUID returned by create) is the immutable route handle. `name` is a mutable, workspace-unique label that registers the agent tool `wsq_<name>`.

## Permissions

* **List / Get**: Requires `Workspace.view` permission.
* **Invoke**: Requires `Workspace.view` permission for a query that only reads, and `Workspace.update` permission for a query that writes.
* **Create / Update / Delete**: Requires `Workspace.update` permission.

## Create a Data Query

{% openapi src="<https://platform.api-us.concurrence.com/v1/openapi.json>" path="/v1/{workspace\_id}/data\_queries" method="post" %}
<https://platform.api-us.concurrence.com/v1/openapi.json>
{% endopenapi %}

Creates a new data query and returns it with a server-generated `query_id` (`201 Created`). Creating a query whose `name` already exists in the workspace returns `409 Conflict`. To change an existing query, use `PATCH` on its `query_id` (see [Update a Data Query](#update-a-data-query)).

### Deploy-Time Validation

The platform validates the query before storing it, on create and on every update:

* The SQL must pass the [workspace SQL rules](https://docs.concurrence.com/developer-guide/platform-api/data-world-model/workspace-tables.md#sql-validation-rules): one `SELECT`, `INSERT`, `UPDATE`, `DELETE` or `TRUNCATE` statement that calls only built-in functions that run no SQL text and change no session state. SQL that breaks them returns `422` with the reason.
* Every `:name` placeholder in the SQL must have a matching entry in `parameters`, and every declared parameter must appear as a placeholder in the SQL.
* Default values must match their declared type.

### Reads, Writes and Refusals

A data query can read or write. An `INSERT`, `UPDATE`, `DELETE` or `TRUNCATE` template is accepted, and invoking one needs the `Workspace.update` permission.

The SQL is checked again each time the query runs, from the invoke operation or as an agent tool, so a query whose SQL does not pass fails with the same message, as a `422` on invoke. An update checks the SQL the query ends up with, so an update to any field returns `422` if that SQL does not pass. Send a corrected `sql_template` with it.

On create, as on `/query`, the refusal is in `detail` as a list of validation errors (see the [refused request example](https://docs.concurrence.com/developer-guide/platform-api/data-world-model/workspace-tables.md#refused-request-example)). On update and on invoke, `detail` is the message as a string.

### Declarations <a href="#declarations" id="declarations"></a>

`declarations` states what the query's calls return, so the platform doesn't guess from the SQL. Its one declaration, `reads`, says whose patient record a call returns, in the same shape as an [integration endpoint's](https://docs.concurrence.com/developer-guide/platform-api/integrations.md#endpoint-read-declarations):

* `{"patient_in": "none"}`: practice data, such as FAQ articles, locations, or opening hours. On voice, such a result doesn't count as a patient record in the call, so while nothing else in the call has brought one in, the [reply checks](https://docs.concurrence.com/operations-and-safety/runtime-safety#reply-checks) don't treat a public answer as record details shared with a caller who hasn't verified.
* `{"patient_in": "params", "params": [...]}`: the record of the patient the named parameters identify. Each name must be one of the query's `parameters`.
* `{"patient_in": "response", "records": "...", "patient": "..."}`: the patient records in the rows the query returns. `records` points into the rows, a list of objects keyed by column, so the empty pointer is every row, and `patient` points to each row's patient identifier.

A `params` or `response` read, an undeclared read, and every write still count as a record on voice. A create without `declarations` declares nothing, and reads return `{"reads": null}`. An update merges `declarations` by key: a declaration it names replaces the stored one, `null` undeclares it, and one it leaves out keeps what is stored. An unknown declaration returns `422`, and so do a `params` read that names a parameter the query doesn't declare and `none` on a query whose SQL writes, on create or on any update that leaves the query that way.

### Example Request

```json
{
  "name": "active_patients_by_location",
  "description": "Returns active patients at a given location, optionally filtered by status.",
  "sql_template": "SELECT patient_id, name, status FROM custom.patients WHERE location_id = :location_id AND (:status IS NULL OR status = :status) LIMIT :max_rows",
  "parameters": [
    {
      "name": "location_id",
      "type": "string",
      "description": "UUID of the location to filter by."
    },
    {
      "name": "status",
      "type": "string",
      "description": "Optional patient status filter.",
      "default": null
    },
    {
      "name": "max_rows",
      "type": "integer",
      "description": "Maximum number of rows to return.",
      "default": 50
    }
  ],
  "timeout_ms": 5000,
  "declarations": {
    "reads": { "patient_in": "response", "records": "", "patient": "/patient_id" }
  }
}
```

### Example Response

```json
{
  "id": "9f8e7d6c-5b4a-3210-fedc-ba9876543210",
  "name": "active_patients_by_location",
  "description": "Returns active patients at a given location, optionally filtered by status.",
  "sql_template": "SELECT patient_id, name, status FROM custom.patients WHERE location_id = :location_id AND (:status IS NULL OR status = :status) LIMIT :max_rows",
  "parameters": [
    {
      "name": "location_id",
      "type": "string",
      "description": "UUID of the location to filter by."
    },
    {
      "name": "status",
      "type": "string",
      "description": "Optional patient status filter.",
      "default": null
    },
    {
      "name": "max_rows",
      "type": "integer",
      "description": "Maximum number of rows to return.",
      "default": 50
    }
  ],
  "timeout_ms": 5000,
  "declarations": {
    "reads": { "patient_in": "response", "records": "", "patient": "/patient_id" }
  },
  "last_invoked_at": null,
  "deployed_at": "2026-07-15T10:30:00+00:00",
  "deployed_by": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
}
```

## Update a Data Query

{% openapi src="<https://platform.api-us.concurrence.com/v1/openapi.json>" path="/v1/{workspace\_id}/data\_queries/{query\_id}" method="patch" %}
<https://platform.api-us.concurrence.com/v1/openapi.json>
{% endopenapi %}

Partially updates an existing data query, addressed by its immutable `query_id`. Any subset of `name`, `description`, `sql_template`, `parameters`, `timeout_ms`, and `declarations` may be supplied. Omitted fields are left unchanged, and `declarations` merges by key, as [Declarations](#declarations) describes. The `name` is mutable but must remain unique within the workspace. The same deploy-time validation applies to any updated SQL.

## List Data Queries

{% openapi src="<https://platform.api-us.concurrence.com/v1/openapi.json>" path="/v1/{workspace\_id}/data\_queries" method="get" %}
<https://platform.api-us.concurrence.com/v1/openapi.json>
{% endopenapi %}

Returns the workspace's registered data queries, ordered newest first. Results are paginated: when `has_more` is `true`, pass the returned `continuation_token` on the next request to fetch the following page.

### Example Response

```json
{
  "items": [
    {
      "id": "3c4d5e6f-7a8b-4c9d-8e0f-1a2b3c4d5e6f",
      "name": "opening_hours_by_location",
      "description": "Returns a location's opening hours for each day of the week.",
      "sql_template": "SELECT day_of_week, opens_at, closes_at FROM custom.opening_hours WHERE location_id = :location_id",
      "parameter_count": 1,
      "timeout_ms": 5000,
      "declarations": { "reads": { "patient_in": "none" } },
      "last_invoked_at": null,
      "deployed_at": "2026-07-15T10:30:00+00:00",
      "deployed_by": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
    }
  ],
  "has_more": false,
  "continuation_token": null
}
```

List items carry a `parameter_count` instead of the full `parameters` array. Fetch a single query by ID to get its parameter definitions.

## Get a Data Query

{% openapi src="<https://platform.api-us.concurrence.com/v1/openapi.json>" path="/v1/{workspace\_id}/data\_queries/{query\_id}" method="get" %}
<https://platform.api-us.concurrence.com/v1/openapi.json>
{% endopenapi %}

Returns a single data query by its `query_id`.

## Delete a Data Query

{% openapi src="<https://platform.api-us.concurrence.com/v1/openapi.json>" path="/v1/{workspace\_id}/data\_queries/{query\_id}" method="delete" %}
<https://platform.api-us.concurrence.com/v1/openapi.json>
{% endopenapi %}

Removes a registered data query, addressed by its `query_id`. Deleting a query cascades to its parameter rows automatically. Returns `204 No Content` on success.

## Invoke a Data Query

{% openapi src="<https://platform.api-us.concurrence.com/v1/openapi.json>" path="/v1/{workspace\_id}/data\_queries/{query\_id}/invoke" method="post" %}
<https://platform.api-us.concurrence.com/v1/openapi.json>
{% endopenapi %}

Executes a registered data query, addressed by its `query_id`, with the provided arguments. An unknown or missing required argument returns `422`, and so does stored SQL that does not pass the [workspace SQL rules](https://docs.concurrence.com/developer-guide/platform-api/data-world-model/workspace-tables.md#sql-validation-rules); fix the call or the query rather than retrying it. `503` means execution failed.

### Example Request

```json
{
  "input": {
    "location_id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
    "max_rows": 10
  }
}
```

### Example Response

```json
{
  "result": [
    {
      "patient_id": "abc-123",
      "name": "Jane Doe",
      "status": "active"
    }
  ],
  "duration_ms": 42.5,
  "row_count": 1
}
```

## Operation Grants

An operation grant lets one credential perform an `action` (`read`, `write`, or `invoke`) on one operation of kind `data_query` or `function` until `expires_at`. A grant records the operation definition hash it was issued against and can be revoked.

### Create an Operation Grant

Creates a grant for a `credential_id` and an operation, identified by `operation_id` or by `function_name`.

{% openapi src="<https://platform.api-us.concurrence.com/v1/openapi.json>" path="/v1/{workspace\_id}/operation\_grants" method="post" %}
<https://platform.api-us.concurrence.com/v1/openapi.json>
{% endopenapi %}

### List Operation Grants

Lists the workspace's operation grants.

{% openapi src="<https://platform.api-us.concurrence.com/v1/openapi.json>" path="/v1/{workspace\_id}/operation\_grants" method="get" %}
<https://platform.api-us.concurrence.com/v1/openapi.json>
{% endopenapi %}

### Invoke an Operation Grant

Runs the granted operation with an `input` object of up to 32 properties. The response carries `result`, `row_count`, and `duration_ms`.

{% openapi src="<https://platform.api-us.concurrence.com/v1/openapi.json>" path="/v1/{workspace\_id}/operation\_grants/{grant\_id}/invoke" method="post" %}
<https://platform.api-us.concurrence.com/v1/openapi.json>
{% endopenapi %}

### Revoke an Operation Grant

Revokes a grant and stops invocation through it.

{% openapi src="<https://platform.api-us.concurrence.com/v1/openapi.json>" path="/v1/{workspace\_id}/operation\_grants/{grant\_id}" method="delete" %}
<https://platform.api-us.concurrence.com/v1/openapi.json>
{% endopenapi %}

## Agent Tool Registration

When a conversation session starts, the platform loads all data queries for the workspace and registers each one as a tool available to the agent. The tool name follows the pattern `wsq_<name>` (for example, a query named `active_patients_by_location` becomes the tool `wsq_active_patients_by_location`). The agent sees the query's description and input schema, and can call it like any other tool.

Data query tools are constrained to tables provisioned for the workspace. This is distinct from platform function tools (prefixed `fn_`), which use their configured external data source.

A query that runs past its `timeout_ms` fails as a timeout. On a `gpt_realtime` call with an English agent, when it is the only result the turn has to report, the caller then hears an authored line, "Sorry, that took longer than expected and I wasn't able to finish just now. Could you try asking again?", in place of the model's own wording. Elsewhere the agent answers the timeout itself. See [Tool-Wait Progress Hints](https://docs.concurrence.com/developer-guide/platform-api/conversations/voice-agent.md#tool-wait-progress-hints) for what a caller hears while a query runs.


---

# 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.concurrence.com/developer-guide/platform-api/functions/workspace-data-queries.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.
