Query Documents by ID or UID

This page gives examples of how to query for a document either by its ID or its UID. It provides examples for both using the query helper functions and doing the query manually.

Before Reading

This page assumes that you've already imported the Prismic library and retrieved the api object. If you want to learn how to do this, then refer to the How to Query the API page.

Querying by Language

Note that if you are trying to query a document that isn't in the master language of your repository, you will need to specify the language code or wildcard language value. You can read how to do this on the Query by Language page.

If you are using one of the query helper functions below, you do not need to do this.

Query by ID

With the helper function

Here is an example showing how to query a document by its id using the helper function.

Copy
api.getByID('WAjgAygABN3B0a-a').then(function(document) {
    // document contains the document content
});

Without the helper function

Here is an example of how to query a document by its id without the helper function.

Copy
api.query(
    Prismic.Predicates.at('document.id', 'WAjgAygAAN3B0a-a')
).then(function(response) {
    var document = response.results[0];
    // document contains the document content
});

Query multiple documents by their IDs

With the helper function

Here is an example of how to query multiple documents by their ids using the helper function.

Copy
var ids = ['WAjgAygAAN3B0a-a', 'WC7GECUAAHBHQd-Y', 'WEE_gikAAC2feA-z'];

api.getByIDs(ids).then(function(response) {
    // response is the response object, response.results holds the documents
});

Without the helper function

You can also query multiple documents by their ids without the helper function by using the in predicate.

Copy
var ids = ['WAjgAygAAN3B0a-a', 'WC7GECUAAHBHQd-Y', 'WEE_gikAAC2feA-z'];

api.query(
    Prismic.Predicates.in('document.id', ids)
).then(function(response) {
    // response is the response object, response.results holds the documents
});

Query by UID

With the helper function

Here is an example of how to query a document of the type "page" by its uid "about-us".

Copy
api.getByUID('page', 'about-us').then(function(document) {
    // document contains the document content
});

Without the helper function

Here is an example of how to query a document of the type "page" by its uid "about-us" without using the helper function.

Copy
api.query(
    Prismic.Predicates.at('my.page.uid', 'about-us')
).then(function(response) {
    var document = response.results[0];
    // document contains the document content
});