Filters

General Usage

Request using query parameters

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

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

Find all available relational operators (like equal or greaterThan, etc.) at the API Reference.

Sample Request

GET https://api.cmft.io/v1/demo/documents?filters[0][*.fields.rating][equal]=5&key=8MIO994Ley6bqyAlQAHqutiDh4g5Heck

Query Parameters

{
  "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"
      }
    }
  ]
}

Request using stringified JSON

// 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)
  })

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.

// 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)
  })

Using multiple filters in query

// 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)
  })

Filter documents by content type

// 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)
  })

Filter documents by tags

// 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)
  })

Last updated