# Filters

## General Usage

## Request using query parameters

Query a `collection` and apply a filter for a specific field.

{% tabs %}
{% tab title="URL" %}

```http
https://api.cmft.io/v1/<repo>/collections/<collection>?key=<apiKey>&filters[0][*.fields.rating][equal]=5
```

{% endtab %}

{% tab title="CURL" %}

```bash
curl -g 'https://api.cmft.io/v1/<repo>/collections/<collection>?filters[0][*.fields.category][equal]=spaceships' \
  -H 'Authorization: <apiKey>'
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
window.fetch('https://api.cmft.io/v1/<repo>/collections/<collection>?filters[0][*.fields.category][equal]=spaceships', {
  method: 'get',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': '<apiKey>'
  }
})
  .then(function (response) { return response.json() })
  .then(function (data) {
    console.log(data)
  })
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
Find all available[ relational operators](/apis/rest-api/filters/filters.md#relational-operators) (like `equal` or `greaterThan`, etc.) at the [API Reference](/apis/rest-api/filters/filters.md).
{% endhint %}

## Sample Request

<mark style="color:blue;">`GET`</mark> `https://api.cmft.io/v1/demo/documents?filters[0][*.fields.rating][equal]=5&key=8MIO994Ley6bqyAlQAHqutiDh4g5Heck`

#### Query Parameters

| Name    | Type   | Description |
| ------- | ------ | ----------- |
| key     | string |             |
| filters | array  |             |

{% tabs %}
{% tab title="200 " %}

```javascript
{
  "status": 200,
  "meta": {
    "count": 1,
    "limit": 25,
    "offset": 0,
    "total": 1
  },
  "data": [
    {
      "fields": {
        "title": "Beautiful Bicycle",
        "description": {
          "html": "<p>Aluminium Frame; Disc Brakes; Shimano XTR;</p>",
          "plain": "Aluminium Frame; Disc Brakes; Shimano XTR;"
        },
        "price": 1399,
        "relatedProducts": [],
        "rating": "5",
        "inStock": true,
        "images": [
          {
            "meta": {
              "id": "969465200912769024",
              "contentType": "_asset"
            }
          }
        ]
      },
      "meta": {
        "id": "969464870959452160",
        "contentType": "products",
        "repository": "968388392691830784",
        "revision": 4,
        "tags": [],
        "createdAt": "2018-07-09T12:13:57.000Z",
        "updatedAt": "2018-07-09T12:30:01.000Z"
      }
    }
  ]
}
```

{% endtab %}
{% endtabs %}

## Request using stringified JSON &#x20;

{% tabs %}
{% tab title="JavaScript" %}

```javascript
// Define query
var query = {
  "filters": [
    {
      "*.fields.category": {
        "equal": "spaceships"
      }
    }
  ]
};

// Stringify and URI Encode the Object
query = encodeURIComponent(JSON.stringify(query));

// Make the request
window.fetch(`https://api.cmft.io/v1/<repo>/collections/<collection>?query=${query}`, {
  method: 'get',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': '<apiKey>'
  }
})
  .then(function (response) { return response.json() })
  .then(function (data) {
    console.log(data)
  })
```

{% endtab %}
{% endtabs %}

## Request using JSON POST Body

For large queries that would exceed the length limit of GET request, use the POST method to perform a request.

{% tabs %}
{% tab title="JavaScript" %}

```javascript
// Define query
var query = {
  "filters": [
    {
      "*.fields.category": {
        "equal": "spaceships"
      }
    }
  ]
};

// Stringify Object
query = JSON.stringify(query);

// Make the request  
window.fetch('https://api.cmft.io/v1/<repo>/collections/<collection>', {
  method: 'post',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': '<apiKey>'
  },
  body: JSON.stringify(query)
})
  .then(function (response) { return response.json() })
  .then(function (data) {
    console.log(data)
  })
```

{% endtab %}

{% tab title="CURL" %}

```bash
curl 'https://api.cmft.io/v1/<repo>/collections/<collection>' \
  -H 'Authorization: <apiKey>' \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
        "filters": {
          "*.fields.category": {
            "equal": "spaceships"
          }
        }
      }'
```

{% endtab %}
{% endtabs %}

## Using multiple filters in query

{% tabs %}
{% tab title="JavaScript" %}

```javascript
// Define query
var query = {
  "filters": [
    {
      "*.fields.category": {
          "equal": "spaceships"
        }
    },
    {
      "and": {
        "*.fields.color": {
          "equal": "red"
        }
      }
    },
    {
      "or": {
        "*.meta.id": {
          "equal": "314159265358979323"
        }
      }
    }
  ]
};

// Stringify and URI Encode the Object
query = encodeURIComponent(JSON.stringify(query));

// Submit the query
window.fetch(`https://api.cmft.io/v1/<repo>/collections/<collection>?query=${query}`, {
  method: 'get',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': '<apiKey>'
  }
})
  .then(function (response) { return response.json() })
  .then(function (data) {
    console.log(data)
  })
```

{% endtab %}

{% tab title="CURL" %}

```bash
curl -g 'https://api.cmft.io/v1/<repo>/aircrafts?filters[0][*.fields.category][equal]=spaceships&filters[1][and][*.fields.color][equal]=red' \
  -H 'Authorization: <apiKey>'
```

{% endtab %}

{% tab title="URL" %}

```http
https://api.cmft.io/v1/<repo>/aircrafts?key=<apiKey>&filters[0][*.fields.category][equal]=spaceships&filters[1][and][*.fields.color][equal]=red
```

{% endtab %}
{% endtabs %}

## Filter documents by content type

{% tabs %}
{% tab title="JavaScript" %}

```javascript
// Define query
var query = {
	"filters": [
    {
      "*.meta.contentType": {
          "equal": "aircrafts"
        }
    }
  ]
};

// Stringify and URI Encode the Object
query = encodeURIComponent(JSON.stringify(query));

// Submit the query
window.fetch(`https://api.cmft.io/v1/<repo>/documents?query=${query}`, {
  method: 'get',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': '<apiKey>'
  }
})
  .then(function (response) { return response.json() })
  .then(function (data) {
    console.log(data)
  })
```

{% endtab %}

{% tab title="CURL" %}

```bash
curl -g 'https://api.cmft.io/v1/<repo>/documents?filters[0][*.meta.contentType][equal]=aircrafts' \
  -H 'Authorization: <apiKey>'
```

{% endtab %}

{% tab title="URL" %}

```http
https://api.cmft.io/v1/<repo>/documents?key=<apiKey>&filters[0][*.meta.contentType][equal]=aircrafts
```

{% endtab %}
{% endtabs %}

## Filter documents by tags

{% tabs %}
{% tab title="JavaScript" %}

```javascript
// Define query
var query = {
  "includeTags": [
    "rocket-powered",
    "motorjet"
  ]
};

// Stringify and URI Encode the Object
query = encodeURIComponent(JSON.stringify(query));

// Submit the query
window.fetch(`https://api.cmft.io/v1/<repo>/documents?query=${query}`, {
  method: 'get',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': '<apiKey>'
  }
})
  .then(function (response) { return response.json() })
  .then(function (data) {
    console.log(data)
  })
```

{% endtab %}

{% tab title="URL" %}

```http
https://api.cmft.io/v1/<repo>/documents?key=<apiKey>&includeTags=rocket-powered,flux-capacitor
```

{% endtab %}

{% tab title="CURL" %}

```bash
curl 'https://api.cmft.io/v1/<repo>/documents?includeTags=rocket-powered,motorjet' \
  -H 'Authorization: <apiKey>'
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: 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:

```
GET https://docs.comfortable.io/apis/rest-api/examples/filters.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
