Single Documents
Basic document query
Demonstrates how to query a single document
.
Query by Document ID
window.fetch('https://api.cmft.io/v1/<repo>/documents/<documentID>', {
method: 'get',
headers: {
'Content-Type': 'application/json',
'Authorization': '<apiKey>'
}
})
.then(function (response) { return response.json() })
.then(function (data) {
console.log(data)
})
Query by Document Alias
You can create an alias for a single document by linking it in the Content Tree. Once an alias is created, it is possible to switch out the document that's connected to that alias.
window.fetch('https://api.cmft.io/v1/<repo>/alias/<myAliasName>', {
method: 'get',
headers: {
'Content-Type': 'application/json',
'Authorization': '<apiKey>'
}
})
.then(function (response) { return response.json() })
.then(function (data) {
console.log(data)
})
Sample Request by ID
GET
https://api.cmft.io/v1/demo/documents/968390655497867264?key=8MIO994Ley6bqyAlQAHqutiDh4g5Heck
Path Parameters
key
string
API Key
{
"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"
}
}
Including Assets for Documents
When querying documents, add embedAssets=true
as URL parameter or as a query option to include assets by default, within a documents fields.
This eliminates the need to perform single asset queries and reduces the number of API calls.
// Define query
var query = {
"embedAssets": true
};
​
// Stringify and URI Encode the Object
query = encodeURIComponent(JSON.stringify(query));
window.fetch(`https://api.cmft.io/v1/<repo>/documents/<documentID>?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 with Assets included
GET
https://api.cmft.io/v1/demo/documents/968390655497867264?embedAssets=true&key=8MIO994Ley6bqyAlQAHqutiDh4g5Heck
Path Parameters
embedAssets
boolean
Truthy if assets should be included
key
string
API Key
Response Shaping: Document query with reduced fields
Let's say we need a document, but not all of its content. In this query example we're going to request a document only with the fields title
, text
and image
.
To learn more about defined fields, take a look at the fields query reference page.
// 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>/documents/<documentID>?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/documents/968390655497867264?fields=fields(title,text,images)&key=8MIO994Ley6bqyAlQAHqutiDh4g5Heck
Path Parameters
fields
string
The fields that should be returned by the API
key
string
API Key
{
"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."
},
"images": [
{
"meta": {
"id": "968393840635547648",
"contentType": "_asset"
}
}
]
}
}
Document query with localisation
Example for requesting a document in one specific language (German). This will return all fields that have translation enabled and completed, in German.
window.fetch('https://api.cmft.io/v1/<repo>/documents/<documentID>?locale=de', {
method: 'get',
headers: {
'Content-Type': 'application/json',
'Authorization': '<apiKey>'
}
})
.then(function (response) { return response.json() })
.then(function (data) {
console.log(data)
})
Last updated