# Single Documents

## Basic document query

Demonstrates how to query a single `document`.

### Query by Document ID

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

```javascript
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)
  })
```

{% endtab %}

{% tab title="CURL" %}

```bash
curl 'https://api.cmft.io/v1/<repo>/documents/<documentID>' \
  -H 'Authorization: <apiKey>'
```

{% endtab %}

{% tab title="URL" %}

```http
https://api.cmft.io/v1/<repo>/documents/<documentID>?key=<apiKey>
```

{% endtab %}
{% endtabs %}

### 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.

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

```javascript
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)
  })
```

{% endtab %}

{% tab title="CURL" %}

```bash
curl 'https://api.cmft.io/v1/<repo>/alias/<myAliasName>' \
  -H 'Authorization: <apiKey>'
```

{% endtab %}

{% tab title="URL" %}

```http
https://api.cmft.io/v1/<repo>/alias/<myAliasName>?key=<apiKey>
```

{% endtab %}
{% endtabs %}

## Sample Request by ID

<mark style="color:blue;">`GET`</mark> `https://api.cmft.io/v1/demo/documents/968390655497867264?key=8MIO994Ley6bqyAlQAHqutiDh4g5Heck`

#### Path Parameters

| Name | Type   | Description |
| ---- | ------ | ----------- |
| key  | string | API Key     |

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

```javascript
{
  "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"
  }
}
```

{% endtab %}
{% endtabs %}

## 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.

{% hint style="info" %}
`embedAssets` works for single documents and document lists as well.
{% endhint %}

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

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

{% endtab %}

{% tab title="CURL" %}

```bash
curl 'https://api.cmft.io/v1/<repo>/documents/<documentID>&embedAssets=true' \
  -H 'Authorization: <apiKey>'
```

{% endtab %}

{% tab title="URL" %}

```http
https://api.cmft.io/v1/<repo>/documents/<documentID>?embedAssets=true&key=<apiKey>
```

{% endtab %}
{% endtabs %}

## Sample Request with Assets included

<mark style="color:blue;">`GET`</mark> `https://api.cmft.io/v1/demo/documents/968390655497867264?embedAssets=true&key=8MIO994Ley6bqyAlQAHqutiDh4g5Heck`

#### Path Parameters

| Name        | Type    | Description                         |
| ----------- | ------- | ----------------------------------- |
| embedAssets | boolean | Truthy if assets should be included |
| key         | string  | API Key                             |

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

```
```

{% endtab %}
{% endtabs %}

## 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](https://docs.comfortable.io/apis/rest-api/filters/reducing-payloads).

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

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

{% endtab %}

{% tab title="CURL" %}

```bash
curl 'https://api.cmft.io/v1/<repo>/documents/<documentID>&fields=fields(title,text,images)' \
  -H 'Authorization: <apiKey>'
```

{% endtab %}

{% tab title="URL" %}

```http
https://api.cmft.io/v1/<repo>/documents/<documentID>?fields=fields(title,text,images)&key=<apiKey>
```

{% endtab %}
{% endtabs %}

## Sample Request

<mark style="color:blue;">`GET`</mark> `https://api.cmft.io/v1/demo/documents/968390655497867264?fields=fields(title,text,images)&key=8MIO994Ley6bqyAlQAHqutiDh4g5Heck`

#### Path Parameters

| Name   | Type   | Description                                   |
| ------ | ------ | --------------------------------------------- |
| fields | string | The fields that should be returned by the API |
| key    | string | API Key                                       |

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

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

{% endtab %}
{% endtabs %}

## 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.

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

```javascript
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)
  })
```

{% endtab %}

{% tab title="CURL" %}

```bash
curl 'https://api.cmft.io/v1/<repo>/documents/<documentID>&locale=de' \
  -H 'Authorization: <apiKey>'
```

{% endtab %}

{% tab title="URL" %}

```http
https://api.cmft.io/v1/<repo>/documents/<documentID>?key=<apiKey>&locale=de
```

{% endtab %}
{% endtabs %}
