> For the complete documentation index, see [llms.txt](https://docs.specifyapp.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.specifyapp.com/reference/sdtf-engine/sdtf-query-language.md).

# SDTF Query Language

The SDTF Query Language is a JSON structure describing several match criteria used to select nodes - tokens, groups and collections - within the token tree.

### Examples

{% tabs %}
{% tab title="Collection and tokens" %}
Get a collection exactly named "Colors" and all its children of kind: "token"

```json
{
  "where": {
    "collection": "^Colors$",
    "select": {
      "collection": true,
      "children": {
        "tokens": true
      }
    }
  }
}
```

{% endtab %}

{% tab title="Tokens by type" %}
Select all tokens of type: "color". See all available types in [Supported types](/concepts/specify-design-token-format.md#supported-types).

```json
{
  "where": {
    "token": ".*",
    "withTypes": { "include": ["color"] },
    "select": true
  }
}
```

{% endtab %}

{% tab title="Tokens in a particular location" %}
Select the tokens of type: "color", only within a group named "components" nested in a collection named "Colors".

```json
{
  "where": {
    "collection": "^Colors$",
    "andWhere": {
      "group": "^components$",
      "andWhere": {
        "token": ".*",
        "withTypes": { "include": ["color"] },
        "select": {
          "token": true,
          "parents": true
        }
      }
    }
  }
}
```

Notice it also selects the containing "components" group and "Colors" collection thanks to `select.parents: true`.
{% endtab %}
{% endtabs %}

## Query Language Structure

Every `SDTFQuery` holds a single a `where` property being:

* &#x20;an object: to select one branch of the graph
* an array of objects: to select many branches of the graph - equivalent to an OR statement.

```javascript
type SDTFQuery = { where: Where | Array<Where> }
```

The `where` property splits in 3 kinds: token, group, collection - offering a dedicated set of options to match against the given kind.

### Where Token

```typescript
type WhereToken = {
  token:
    | string // regex compatible
    | {
      name?: string; // regex compatible
      description?: string; // regex compatible
    };
  select:
    | true
    | {
      token?: boolean;
      parents?:
        | true
        | {
          groups?: true;
          collections?: true;
        };
    };
  withTypes?: {
    include?: Array<TokenTypeName>;
    exclude?: Array<TokenTypeName>;
  };
  withModes?: {
    include?: Array<string>;
    exclude?: Array<string>;
  }
};
```

### Where Group

```typescript
type WhereGroup = {
  group:
    | string // regex compatible
    | {
      name?: string; // regex compatible
      description?: string; // regex compatible
    };
  select:
    | true
    | {
    group?: boolean;
    parents?:
      | true
      | {
        groups?: true;
        collections?: true;
      };
    children:
      | true
      | {
        groups?: true;
        collections?: true;
        tokens?: true;
      };
  };
};
```

### Where Collection

```typescript
type WhereCollection = {
  collection:
    | string // regex compatible
    | {
      name?: string; // regex compatible
      description?: string; // regex compatible
    };
  select:
    | true
    | {
      collection?: boolean;
      parents?:
        | true
        | {
          groups?: true;
        };
      children:
        | true
        | {
          groups?: true;
          tokens?: true;
        };
    };
  withModes?: {
    include?: Array<string>;
    exclude?: Array<string>;
  }
};
```

## Recursion

The `where` property can alternatively holds a `andWhere` property instead of the `select` property.

Doing so, the `andWhere` property can receive any `where` operator described earlier.

> Note: since Collections cannot be nested, we cannot nest a `andWhere` Collection operator into another `where` or `andWhere` Collection

```json
{
  "where": {
    "collection": "^colors$",
    "andWhere": {
      "group": "^base",
      "andWhere": {
        "token": ".*",
        "withTypes": { "include": ["color"] },
        "select": {
          "token": true,
          "parents": true
        }
      }
    }
  }
}
```

## Query Result

Once executed by the engine, the query returns a [QueryResult](/reference/sdtf-engine/sdtf-queryresult.md) that helps to work with the matched tree nodes.


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.specifyapp.com/reference/sdtf-engine/sdtf-query-language.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
