How to Query the API

In order to retrieve the content from your repository, you will need to query the repository API. When you create your query you will specify exactly what it is you are looking for. You could query the repository for all the documents of certain type or retrieve the one specific document you need.

Let’s take a look at how to put together queries for whatever case you need.

Check out the Integrating with an existing Node.js project page to learn how to get set up to query documents.

The Basics

When retrieving content from your Prismic repository using Node.js, here’s what a typical query looks like:

var Prismic = require('@prismicio/client');

    api.query('document.type', 'blog-post'),
        { orderings : '[ desc]' }
    ).then(function(response) {
        // response is the response object, response.results holds the documents

What is initApi?

The `initApi` is a function which returns the API object. The function is shown in full on the Integrate with an existing project page.

Should I call `initApi` on every query?

The `initAPI` call is required to get the latest version of your content. You should call `initApi` to retrieve the API object on every request, page, or screen. It is recommended to assign the API object to a variable, and use it for all queries of the same screen.

Why does `initApi` call require a `req` parameter?

Prismic has the capacity to do unlimited different preview sessions for different drafts or releases. To offer this and other powerful functionalities, Prismic uses a cookie that it assigns to the `req` object.

Simply providing `req` to the API enables unlimited previews and other upcoming powerful features.

This is the basic format of a query. In the query you have two parts, the Predicate and the options.


Predicates is a powerful way to query your content. In the above example we had the following predicate:

You will find a list and description of the available paths on the Query Predicates Reference page.

Copy'document.type', 'blog-post')

The predicate(s) will define which documents are retrieved from the content repository. This particular example will retrieve all of the documents of the type "blog-post".

The first part, "document.type" is the path, or what the query will be looking for. The second part the predicate in the above example is "blog-post" this is the value that the query is looking for.

You can combine more than one predicate together to refine your query. You just need to put all your predicates into a comma-separated array like the following example:

['document.type', 'blog-post'),'document.tags', ['featured']) ]

This particular query will retrieve all the documents of the "blog-post" type that also have the tag "featured".


In the second part of the query, you can include the options needed for that query. In the above example we had the following options

{ orderings : '[ desc]' }

This specifies how the returned list of documents will be ordered. You can include more than one option, by comma separating them as shown below:

{ pageSize : 10, page : 2 }

Here’s another example of a more advanced query with multiple predicates and multiple options:

var Prismic = require('@prismicio/client');

api.query(['document.type', 'blog-post'),'document.tags', ['featured'])
], { pageSize : 25, page : 1, orderings : '[ desc]' }
).then(function(response) {
    // response is the response object, response.results holds the documents

Whenever you query your content, you end up with the response object stored in the defined variable. In this case the response object is stored in the response variable.

Pagination of API Results

When querying a Prismic repository, your results will be paginated. By default, there are 20 documents per page in the results. You can read more about how to manipulate the pagination in the Pagination for Results page.

Was this article helpful?
Not really
Yes, Thanks

Can't find what you're looking for? Get in touch with us on our Community Forum.