The Response Object

After you set up your Custom Types and have queried your content from the API, it's time to integrate that content into your templates. First we'll go over the response object returned from the API, then we'll discuss how to retrieve your documents.

The Response Object

Let's start by taking a look at the Response Object returned when querying the API. Here is a simple example of response object with one document that contains a couple of fields.

Copy
{
  "page": 1,
  "results_per_page": 20,
  "results_size": 1,
  "total_results_size": 1,
  "total_pages": 1,
  "next_page": null,
  "prev_page": null,
  "results": [
    {
      "id": "WKxlPCUEEIZ10AHU",
      "uid": "example-page",
      "type": "page",
      "href": "https://your-repo-name.prismic.io/api/v2/documents/search...",
      "tags": [],
      "first_publication_date": "2017-01-13T11:45:21.000Z",
      "last_publication_date": "2017-02-21T16:05:19.000Z",
      "slugs": [
        "example-page"
      ],
      "linked_documents": [],
      "lang": "en-us",
      "alternate_languages": [
        {
          "id": "WZcAEyoAACcA0LHi",
          "uid": "example-page-french",
          "type": "page",
          "lang": "fr-fr"
         }
      ],
      "data": {
        "title": [
          {
            "type": "heading1",
            "text": "Example Page",
            "spans": []
          }
        ],
        "date": "2017-01-13"
      }
    }
  ]
}

At the topmost level of the response object, you mostly have information about the number of results returned from the query and the pagination of the results.

page

The current page of the pagination of the results

results_per_page

The number of documents per page of the pagination

results_size

The number of documents on this page of the pagination results

total_results_size

The total number of documents returned from the query

total_pages

The total number of pages in the pagination of the results

next_page

The next page number in the pagination

prev_page

The previous page number in the pagination

results

The documents and their content for this page of the pagination of the results

Note: When using certain helper functions such as getSingle(), getByUID() or getByID(), the first document of the results array will automatically be returned. 

The Query Results

The actual content of the returned documents can be found under results. This will always be an array of the documents, even if there is only one document returned.

Let's say that you saved your response object in a variable named response. This would mean that your documents could be accessed with the following:

Copy
const documents = response.results;

And if you only returned one document, it would be accessed with the following:

Copy
const document = response.results[0];

Note: As mentioned above, this is not the case when using certain helper functions such as getSingle(), getByUID() or getByID(). These will automatically return the first document of the results array.

Each document will contain information such as it's document ID, UID, type, tags, first publication date, and last publication date.

The content for each document will be found inside data. In the example above you have title and date.