Collections

Fetching Collections

This demonstrates a basic query for a collection.

All examples can be used with/documents Endpoint as well.

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

Sample Request

GET https://api.cmft.io/v1/demo/collections/news?key=8MIO994Ley6bqyAlQAHqutiDh4g5Heck

Query Parameters

Name
Type
Description

key

string

API Key

{
  "status": 200,
  "meta": {
    "count": 2,
    "limit": 25,
    "offset": 0,
    "total": 2
  },
  "data": [
    {
      "fields": {
        "title": "Hello World!",
        "date": "2018-07-06T13:00:00.000Z",
        "text": {
          "html": "<p>Lorem ipsum dolor sit amet, consetetur <strong>sadipscing elitr, sed diam</strong> nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</p>",
          "plain": "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod\ntempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At\nvero eos et accusam et justo duo dolores et ea rebum."
        },
        "images": [
          {
            "meta": {
              "id": "968393840635547648",
              "contentType": "_asset"
            }
          }
        ],
        "relatedNews": [],
        "author": [
          {
            "meta": {
              "id": "968388973430968320",
              "contentType": "author"
            }
          }
        ]
      },
      "meta": {
        "id": "968390655497867264",
        "contentType": "news",
        "repository": "968388392691830784",
        "revision": 5,
        "tags": [],
        "createdAt": "2018-07-06T13:05:24.000Z",
        "updatedAt": "2018-07-06T13:23:08.000Z"
      }
    },
    {
      "fields": {
        "title": "Simple Blog Post",
        "date": "2018-07-04T22:00:00.000Z",
        "text": {
          "html": "<h1>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</h1>",
          "plain": "LOREM IPSUM DOLOR SIT AMET, CONSETETUR SADIPSCING ELITR, SED DIAM NONUMY EIRMOD\nTEMPOR INVIDUNT UT LABORE ET DOLORE MAGNA ALIQUYAM ERAT, SED DIAM VOLUPTUA. AT\nVERO EOS ET ACCUSAM ET JUSTO DUO DOLORES ET EA REBUM."
        },
        "images": [
          {
            "meta": {
              "id": "968393848009134080",
              "contentType": "_asset"
            }
          }
        ],
        "relatedNews": [
          {
            "meta": {
              "id": "968390655497867264",
              "contentType": "news"
            }
          }
        ],
        "author": [
          {
            "meta": {
              "id": "968388973430968320",
              "contentType": "author"
            }
          }
        ]
      },
      "meta": {
        "id": "968395804664537088",
        "contentType": "news",
        "repository": "968388392691830784",
        "revision": 1,
        "tags": [],
        "createdAt": "2018-07-06T13:25:51.000Z",
        "updatedAt": "2018-07-06T13:25:51.000Z"
      }
    }
  ]

Response Shaping

Let's say we want to display a news page with a teaser view for each article. To reduce the payload we're going to do a request and tell the API to include only the fields title , text and image for each document.

// Define query
var query = {
  "fields": "fields(title,text,image)"
};

// Stringify and URI Encode the Object
query = encodeURIComponent(JSON.stringify(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)
  })

Sample Request

GET https://api.cmft.io/v1/demo/collections/news?fields=fields(title,text,image)&key=8MIO994Ley6bqyAlQAHqutiDh4g5Heck

Path Parameters

Name
Type
Description

key

string

API Key

{
  "status": 200,
  "meta": {
    "count": 2,
    "limit": 25,
    "offset": 0,
    "total": 2
  },
  "data": [
    {
      "fields": {
        "title": "Hello World!",
        "text": {
          "html": "<p>Lorem ipsum dolor sit amet, consetetur <strong>sadipscing elitr, sed diam</strong> nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</p>",
          "plain": "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod\ntempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At\nvero eos et accusam et justo duo dolores et ea rebum."
        }
      }
    },
    {
      "fields": {
        "title": "Simple Blog Post",
        "text": {
          "html": "<h1>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</h1>",
          "plain": "LOREM IPSUM DOLOR SIT AMET, CONSETETUR SADIPSCING ELITR, SED DIAM NONUMY EIRMOD\nTEMPOR INVIDUNT UT LABORE ET DOLORE MAGNA ALIQUYAM ERAT, SED DIAM VOLUPTUA. AT\nVERO EOS ET ACCUSAM ET JUSTO DUO DOLORES ET EA REBUM."
        }
      }
    }
  ]
}

Reduced scopes

This example query makes a collection request that will only return the meta scope for each document.

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

Sample Request

GET https://api.cmft.io/v1/demo/collections/news?fields=meta&key=8MIO994Ley6bqyAlQAHqutiDh4g5Heck

Path Parameters

Name
Type
Description

key

string

API Key

{
  "status": 200,
  "meta": {
    "count": 2,
    "limit": 25,
    "offset": 0,
    "total": 2
  },
  "data": [
    {
      "meta": {
        "id": "968390655497867264",
        "contentType": "news",
        "repository": "968388392691830784",
        "revision": 5,
        "tags": [],
        "createdAt": "2018-07-06T13:05:24.000Z",
        "updatedAt": "2018-07-06T13:23:08.000Z"
      }
    },
    {
      "meta": {
        "id": "968395804664537088",
        "contentType": "news",
        "repository": "968388392691830784",
        "revision": 1,
        "tags": [],
        "createdAt": "2018-07-06T13:25:51.000Z",
        "updatedAt": "2018-07-06T13:25:51.000Z"
      }
    }
  ]
}

Reduced fields for relations

This query returns a full list of documents, but for the relations the payload is limited to the fields name and id.

// Define query
var query = {
  "fields": "includes(news(fields(title),meta(id))),fields,meta"
};

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

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

The parameter explained

includes( // the 1st level scope 'includes'
  news( // select 'news' in 'includes'
    fields( // select the content fields for the related type
      title
    ),
    meta( // select the meta fields for the related type
      id
    )
  )
)

Sample Request

GET https://api.cmft.io/v1/demo/collections/news?includes=1&fields=includes(news(fields(title),meta(id))),fields,meta

Path Parameters

Name
Type
Description

key

string

API Key

{
  "status": 200,
  "meta": {
    "count": 2,
    "limit": 25,
    "offset": 0,
    "total": 2
  },
  "data": [
    {
      "fields": {
        "title": "Hello World!",
        "date": "2018-07-06T13:00:00.000Z",
        "text": {
          "html": "<p>Lorem ipsum dolor sit amet, consetetur <strong>sadipscing elitr, sed diam</strong> nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</p>",
          "plain": "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod\ntempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At\nvero eos et accusam et justo duo dolores et ea rebum."
        },
        "images": [
          {
            "meta": {
              "id": "968393840635547648",
              "contentType": "_asset"
            }
          }
        ],
        "relatedNews": [],
        "author": [
          {
            "meta": {
              "id": "968388973430968320",
              "contentType": "author"
            }
          }
        ]
      },
      "meta": {
        "id": "968390655497867264",
        "contentType": "news",
        "repository": "968388392691830784",
        "revision": 5,
        "tags": [],
        "createdAt": "2018-07-06T13:05:24.000Z",
        "updatedAt": "2018-07-06T13:23:08.000Z"
      }
    },
    {
      "fields": {
        "title": "Simple Blog Post",
        "date": "2018-07-04T22:00:00.000Z",
        "text": {
          "html": "<h1>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</h1>",
          "plain": "LOREM IPSUM DOLOR SIT AMET, CONSETETUR SADIPSCING ELITR, SED DIAM NONUMY EIRMOD\nTEMPOR INVIDUNT UT LABORE ET DOLORE MAGNA ALIQUYAM ERAT, SED DIAM VOLUPTUA. AT\nVERO EOS ET ACCUSAM ET JUSTO DUO DOLORES ET EA REBUM."
        },
        "images": [
          {
            "meta": {
              "id": "968393848009134080",
              "contentType": "_asset"
            }
          }
        ],
        "relatedNews": [
          {
            "meta": {
              "id": "968390655497867264",
              "contentType": "news"
            }
          }
        ],
        "author": [
          {
            "meta": {
              "id": "968388973430968320",
              "contentType": "author"
            }
          }
        ]
      },
      "meta": {
        "id": "968395804664537088",
        "contentType": "news",
        "repository": "968388392691830784",
        "revision": 1,
        "tags": [],
        "createdAt": "2018-07-06T13:25:51.000Z",
        "updatedAt": "2018-07-06T13:25:51.000Z"
      }
    }
  ],
  "includes": {
    "news": [
      {
        "fields": {
          "title": "Hello World!"
        },
        "meta": {
          "id": "968390655497867264"
        }
      }
    ]
  }

Applying filters to collection queries

You can apply additional filters on all documents in a collection. Find examples with multiple filters on the filters examples page.

Filter by checkbox state

In this example we're going to check the availability of a product based on a simple checkbox field.

// Define query
var query = {
  "filters": [
    {
      "*.fields.inStock": true
    }
  ]
};

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

Sample Request

GET https://api.cmft.io/v1/demo/collections/products?filters[0][*.fields.inStock][equal]=true&key=8MIO994Ley6bqyAlQAHqutiDh4g5Heck

Path Parameters

Name
Type
Description

key

string

API Key

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

Collection queries with localisation

Lets query a collection in a specific Language

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

Sample Request

GET https://api.cmft.io/v1/demo/collections/news?locale=en&key=8MIO994Ley6bqyAlQAHqutiDh4g5Heck

Path Parameters

Name
Type
Description

key

string

API Key

{
  "status": 200,
  "meta": {
    "count": 2,
    "limit": 25,
    "offset": 0,
    "total": 2
  },
  "data": [
    {
      "fields": {
        "title": "Hello World!",
        "date": "2018-07-06T13:00:00.000Z",
        "text": {
          "html": "<p>Lorem ipsum dolor sit amet, consetetur <strong>sadipscing elitr, sed diam</strong> nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</p>",
          "plain": "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod\ntempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At\nvero eos et accusam et justo duo dolores et ea rebum."
        },
        "images": [
          {
            "meta": {
              "id": "968393840635547648",
              "contentType": "_asset"
            }
          }
        ],
        "relatedNews": [],
        "author": [
          {
            "meta": {
              "id": "968388973430968320",
              "contentType": "author"
            }
          }
        ]
      },
      "meta": {
        "id": "968390655497867264",
        "contentType": "news",
        "repository": "968388392691830784",
        "revision": 5,
        "tags": [],
        "createdAt": "2018-07-06T13:05:24.000Z",
        "updatedAt": "2018-07-06T13:23:08.000Z"
      }
    },
    {
      "fields": {
        "title": "Simple Blog Post",
        "date": "2018-07-04T22:00:00.000Z",
        "text": {
          "html": "<h1>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</h1>",
          "plain": "LOREM IPSUM DOLOR SIT AMET, CONSETETUR SADIPSCING ELITR, SED DIAM NONUMY EIRMOD\nTEMPOR INVIDUNT UT LABORE ET DOLORE MAGNA ALIQUYAM ERAT, SED DIAM VOLUPTUA. AT\nVERO EOS ET ACCUSAM ET JUSTO DUO DOLORES ET EA REBUM."
        },
        "images": [
          {
            "meta": {
              "id": "968393848009134080",
              "contentType": "_asset"
            }
          }
        ],
        "relatedNews": [
          {
            "meta": {
              "id": "968390655497867264",
              "contentType": "news"
            }
          }
        ],
        "author": [
          {
            "meta": {
              "id": "968388973430968320",
              "contentType": "author"
            }
          }
        ]
      },
      "meta": {
        "id": "968395804664537088",
        "contentType": "news",
        "repository": "968388392691830784",
        "revision": 1,
        "tags": [],
        "createdAt": "2018-07-06T13:25:51.000Z",
        "updatedAt": "2018-07-06T13:25:51.000Z"
      }
    }
  ]
}

Query all Languages

Sample Request

GET https://api.cmft.io/v1/demo/collections/news?locale=all&key=8MIO994Ley6bqyAlQAHqutiDh4g5Heck

Path Parameters

Name
Type
Description

key

string

API Key

{
  "status": 200,
  "meta": {
    "count": 2,
    "limit": 25,
    "offset": 0,
    "total": 2
  },
  "data": [
    {
      "fields": {
        "title": {
          "en": "Hello World!",
          "de": ""
        },
        "date": "2018-07-06T13:00:00.000Z",
        "text": {
          "en": {
            "html": "<p>Lorem ipsum dolor sit amet, consetetur <strong>sadipscing elitr, sed diam</strong> nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</p>",
            "plain": "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod\ntempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At\nvero eos et accusam et justo duo dolores et ea rebum."
          },
          "de": {
            "html": "",
            "plain": ""
          }
        },
        "images": {
          "en": [
            {
              "meta": {
                "id": "968393840635547648",
                "contentType": "_asset"
              }
            }
          ],
          "de": []
        },
        "relatedNews": [],
        "author": [
          {
            "meta": {
              "id": "968388973430968320",
              "contentType": "author"
            }
          }
        ]
      },
      "meta": {
        "id": "968390655497867264",
        "contentType": "news",
        "repository": "968388392691830784",
        "revision": 5,
        "tags": [],
        "createdAt": "2018-07-06T13:05:24.000Z",
        "updatedAt": "2018-07-06T13:23:08.000Z"
      }
    },
    {
      "fields": {
        "title": {
          "en": "Simple Blog Post",
          "de": ""
        },
        "date": "2018-07-04T22:00:00.000Z",
        "text": {
          "en": {
            "html": "<h1>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</h1>",
            "plain": "LOREM IPSUM DOLOR SIT AMET, CONSETETUR SADIPSCING ELITR, SED DIAM NONUMY EIRMOD\nTEMPOR INVIDUNT UT LABORE ET DOLORE MAGNA ALIQUYAM ERAT, SED DIAM VOLUPTUA. AT\nVERO EOS ET ACCUSAM ET JUSTO DUO DOLORES ET EA REBUM."
          },
          "de": {
            "html": "",
            "plain": ""
          }
        },
        "images": {
          "en": [
            {
              "meta": {
                "id": "968393848009134080",
                "contentType": "_asset"
              }
            }
          ],
          "de": []
        },
        "relatedNews": [
          {
            "meta": {
              "id": "968390655497867264",
              "contentType": "news"
            }
          }
        ],
        "author": [
          {
            "meta": {
              "id": "968388973430968320",
              "contentType": "author"
            }
          }
        ]
      },
      "meta": {
        "id": "968395804664537088",
        "contentType": "news",
        "repository": "968388392691830784",
        "revision": 1,
        "tags": [],
        "createdAt": "2018-07-06T13:25:51.000Z",
        "updatedAt": "2018-07-06T13:25:51.000Z"
      }
    }
  ]
}

Last updated