> For the complete documentation index, see [llms.txt](https://docs.veza.com/4yItIzMvkpAvMVFAamTf/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.veza.com/4yItIzMvkpAvMVFAamTf/developers/api/query-builder/createassessmentquery.md). Agents can also search and read this documentation through its [MCP server](https://docs.veza.com/4yItIzMvkpAvMVFAamTf/~gitbook/mcp).

# Save a query

Save a new query from a query spec, with its name, visibility, labels, and risk settings.

Saves a new query from a query spec, with a name, a description, and optional labels and risk settings. The saved query is listed on the [Saved Queries](https://docs.veza.com/4yItIzMvkpAvMVFAamTf/features/search/saved-queries.md) page, and you can add it to reports and dashboards and attach rules to it. To run a query spec without saving it, use [Get query spec results](https://docs.veza.com/4yItIzMvkpAvMVFAamTf/developers/api/query-builder/getassessmentqueryspecresult.md). In Veza, you save a query from [Query Builder](https://docs.veza.com/4yItIzMvkpAvMVFAamTf/features/search/query-builder.md).

## Endpoint

```sh
POST /api/v1/assessments/queries
```

This endpoint requires one of these built-in [roles](https://docs.veza.com/4yItIzMvkpAvMVFAamTf/administration/administration/users/roles.md): **Administrator**, **Access Reviews Admin**, **Operator**, or **NHI Security Admin**. Requests from other roles return `PermissionDenied`. OAuth2 client-credentials tokens can't call this endpoint.

{% openapi src="/files/Aco9gj4MY2XoNiD4zIjr" path="/api/v1/assessments/queries" method="post" %}
[openapi.yaml](https://1967633068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZDkWMxox3pekd0NsZJ%2Fuploads%2Fgit-blob-c531fb16ceb10edc6ff44c5ffa8282a43a35a636%2Fopenapi.yaml?alt=media)
{% endopenapi %}

## Request

Send the query as the JSON body. Each field is a top-level key. `name`, `description`, `query_type`, and `source_node_types` are required for a single query; a union query sends `union_specs` instead of `source_node_types`. For every field, see [Query Builder parameters](https://docs.veza.com/4yItIzMvkpAvMVFAamTf/developers/api/query-builder/parameters.md#query-parameters).

The request drops any key that isn't a field of the request, without an error. A query spec copied from the Query Builder **View Query Spec API** option can carry `snapshot_id`, `page_size`, `page_token`, or `customized_variables`; saving drops them. It can also carry `"query_type": "SYSTEM_CREATED"`, which saving rejects. Change it to `SOURCE_TO_DESTINATION`.

These defaults apply to fields the request omits:

* `visibility` saves as `PUBLIC`.
* `risk_level` saves as `NONE`, and `risk_profiles` then saves as `["INFORMATIONAL"]`.
* `save_mode` saves as `QUERY_BUILDER`.
* `analysis_type` saves as `UNSET`, or as `QUERY_PIPELINE` when the query uses another saved query's results.

The caller becomes the query's creator and only owner. To change its owners, use [Update query owners](https://docs.veza.com/4yItIzMvkpAvMVFAamTf/developers/api/query-builder/updateassessmentquerypermissions.md).

## Examples

Set `VEZA_URL` and `VEZA_TOKEN` as described in [API tokens](https://docs.veza.com/4yItIzMvkpAvMVFAamTf/developers/api/authentication/api-tokens.md).

Save a private query that marks every Okta user who can reach the Snowflake `BILLING` role as a high risk:

```bash
curl -X POST "$VEZA_URL/api/v1/assessments/queries" \
  -H "Authorization: Bearer $VEZA_TOKEN" \
  -H "Content-Type: application/json" \
  --data '{
    "name": "Okta users with the Snowflake BILLING role",
    "description": "Okta users who can use the Snowflake BILLING role.",
    "query_type": "SOURCE_TO_DESTINATION",
    "node_relationship_type": "EFFECTIVE_ACCESS",
    "source_node_types": {
      "nodes": [{ "node_type": "OktaUser" }]
    },
    "relates_to_exp": {
      "specs": [
        {
          "node_types": {
            "nodes": [
              {
                "node_type": "SnowflakeRole",
                "condition_expression": {
                  "specs": [{ "fn": "EQ", "property": "name", "value": "BILLING" }],
                  "operator": "AND"
                }
              }
            ]
          },
          "direction": "ANY_DIRECTION"
        }
      ],
      "operator": "AND"
    },
    "risk_level": "HIGH",
    "risk_profiles": ["PRIVILEGE_ACCESS"],
    "risk_explanation": "These users can read billing data in Snowflake.",
    "labels": ["snowflake"],
    "visibility": "PRIVATE"
  }'
```

Example response (constructed from `CreateAssessmentQueryResponse`). The response returns the saved query in `value`. Read the query ID from `value.id`; the top-level `id` is deprecated. The query is shortened here; a response has many more fields of the saved query.

```json
{
  "id": "<query_id>",
  "value": {
    "id": "<query_id>",
    "name": "Okta users with the Snowflake BILLING role",
    "description": "Okta users who can use the Snowflake BILLING role.",
    "result_type": "NUMBER",
    "query_type": "SOURCE_TO_DESTINATION",
    "source_node_types": {
      "nodes": [{ "node_type": "OktaUser" }],
      "nodes_operator": "AND"
    },
    "relates_to_exp": {
      "specs": [
        {
          "node_types": {
            "nodes": [
              {
                "node_type": "SnowflakeRole",
                "condition_expression": {
                  "specs": [{ "fn": "EQ", "property": "name", "value": "BILLING", "not": false }],
                  "child_expressions": [],
                  "operator": "AND",
                  "not": false
                }
              }
            ],
            "nodes_operator": "AND"
          },
          "direction": "ANY_DIRECTION"
        }
      ],
      "child_expressions": [],
      "operator": "AND",
      "not": false
    },
    "created_by": "<user_id>",
    "visibility": "PRIVATE",
    "owners": ["<user_id>"],
    "node_relationship_type": "EFFECTIVE_ACCESS",
    "integration_types": ["okta", "snowflake"],
    "labels": ["snowflake"],
    "created_at": "2026-11-18T21:05:12.418Z",
    "updated_at": "2026-11-18T21:05:12.418Z",
    "source_type": "OktaUser",
    "destination_types": ["SnowflakeRole"],
    "risk_level": "HIGH",
    "risk_explanation": "These users can read billing data in Snowflake.",
    "risk_remediation": "",
    "risk_profiles": ["PRIVILEGE_ACCESS"],
    "analysis_type": "UNSET",
    "save_mode": "QUERY_BUILDER",
    "variables": [],
    "disabled": false,
    "edited": false
  }
}
```

## Errors and limits

The requests in the following table return `InvalidArgument` (HTTP 400), with the reason in `details[].field_violations`:

| Field                         | Cause                                                      | Message                                                                                                |
| ----------------------------- | ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
| `Name`                        | A saved query with the same name exists                    | `Cannot create a query with name <name>. A query with this name already exists.`                       |
| `name`, `description`         | The field is empty                                         | `Cannot be empty`                                                                                      |
| `name`                        | One character                                              | `Must be longer than 1`                                                                                |
| `name`                        | Longer than 255 characters                                 | `Must have less than 256 characters`                                                                   |
| `description`                 | Longer than 16,383 characters                              | `Must have less than 16384 characters`                                                                 |
| `body`                        | Both `union_specs` and the single-query fields, or neither | `query must contain either union_specs or the single-query fields (source_node_types, relates_to_exp)` |
| `query_type`                  | Omitted, or `SYSTEM_CREATED`                               | `Must be one of: SOURCE_TO_DESTINATION, DESTINATION_NODES`                                             |
| `risk_suppressed_constraints` | Set when `risk_level` is `NONE`                            | `Can only be set if risk_level is not NONE`                                                            |
| `risk_explanation`            | Set when `risk_level` is `NONE`                            | `Risk explanation cannot be set when RiskLevel is NONE.`                                               |
| `risk_remediation`            | Set when `risk_level` is `NONE`                            | `Risk remediation cannot be set when RiskLevel is NONE.`                                               |
| `risk_profiles`               | `INFORMATIONAL` with a `risk_level` other than `NONE`      | `INFORMATIONAL risk profile can only be assigned to queries with NONE risk level`                      |
| `risk_profiles`               | Another profile with `risk_level` `NONE`                   | `non-INFORMATIONAL risk profiles cannot be assigned to queries with NONE risk level`                   |
| `risk_profiles`               | More than one profile                                      | `List has a max length of 1`                                                                           |
| `vql_query`                   | Set when `save_mode` is `QUERY_BUILDER`                    | `Must be empty`                                                                                        |
| `vql_query`                   | Empty when `save_mode` is `VQL_EDITOR`                     | `Cannot be empty`                                                                                      |

A `cloned_from_query_id` that matches no saved query returns `NotFound` (HTTP 404). For shared status codes and the error body, see [Troubleshooting](https://docs.veza.com/4yItIzMvkpAvMVFAamTf/developers/api/errors.md).

To change a saved query, see [Update a query](https://docs.veza.com/4yItIzMvkpAvMVFAamTf/developers/api/query-builder/updateassessmentquery.md). To read it, see [Get a saved query](https://docs.veza.com/4yItIzMvkpAvMVFAamTf/developers/api/query-builder/getassessmentquery.md), and to delete it, see [Delete a query](https://docs.veza.com/4yItIzMvkpAvMVFAamTf/developers/api/query-builder/deleteassessmentquery.md).


---

# 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.veza.com/4yItIzMvkpAvMVFAamTf/developers/api/query-builder/createassessmentquery.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.
