Class: Api

Api

new Api(url) → {Api}

The kit's main entry point; initialize your API like this: Prismic.api(url, {accessToken: "XXX"})
Parameters:
Name Type Description
url string The mandatory URL of the prismic.io API endpoint (like: https://lesbonneschoses.prismic.io/api)
options.callback function Optional callback function that is called after the API was retrieved, which will be called with two parameters: a potential error object and the API object
options.accessToken string The accessToken, necessary if the API is set as private
options.req string The NodeJS request (only use in a NodeJS context)
options.requestHandler function Environment specific HTTP request handling function
options.apiCache object A cache object with get/set functions for caching API responses
options.apiDataTTL int How long (in seconds) to cache data used by the client to make calls (e.g. refs). Defaults to 5 seconds
Source:
Returns:
- The Api object that can be manipulated
Type
Api

Methods

currentExperiment() → {Experiment}

The current experiment, or null
Source:
Returns:
Type
Experiment

form(formId) → {SearchForm}

Returns a useable form from its id, as described in the RESTful description of the API. For instance: api.form("everything") works on every repository (as "everything" exists by default) You can then chain the calls: api.form("everything").query('[[:d = at(document.id, "UkL0gMuvzYUANCpf")]]').ref(ref).submit()
Parameters:
Name Type Description
formId string The id of a form, like "everything", or "products"
Source:
Returns:
- the SearchForm that can be used.
Type
SearchForm

forms(formId) → {SearchForm}

Parameters:
Name Type Description
formId string The id of a form, like "everything", or "products"
Deprecated:
  • use form() now
Source:
Returns:
- the SearchForm that can be used.
Type
SearchForm

get(callback) → {Promise}

Fetches data used to construct the api client, from cache if it's present, otherwise from calling the prismic api endpoint (which is then cached).
Parameters:
Name Type Description
callback function Callback to receive the data. Optional, you can use the promise result.
Source:
Returns:
Promise holding the data or error
Type
Promise

getBookmark(bookmark, additional, callback(err,) → {Promise}

Retrieve the document with the given bookmark
Parameters:
Name Type Description
bookmark string name
additional object parameters
callback(err, function response)
Source:
Returns:
Type
Promise

getByID(id, additional, callback(err,)

Retrieve the document with the given id
Parameters:
Name Type Description
id string
additional object parameters
callback(err, function doc)
Source:

getByIDs(ids, additional, callback(err,)

Retrieve multiple documents from an array of id
Parameters:
Name Type Description
ids array
additional object parameters
callback(err, function response)
Source:

getByUID(type, uid, additional, callback(err,)

Retrieve the document with the given uid
Parameters:
Name Type Description
type string the custom type of the document
uid string
additional object parameters
callback(err, function response)
Source:

getSingle(type, additional, callback(err,)

Retrieve the singleton document with the given type
Parameters:
Name Type Description
type string the custom type of the document
additional object parameters
callback(err, function response)
Source:

master() → {string}

The ID of the master ref on this prismic.io API. Do not use like this: searchForm.ref(api.master()). Instead, set your ref once in a variable, and call it when you need it; this will allow to change the ref you're viewing easily for your entire page.
Source:
Returns:
Type
string

previewSession(token, linkResolver, defaultUrl, callback) → {Promise}

Return the URL to display a given preview
Parameters:
Name Type Description
token string as received from Prismic server to identify the content to preview
linkResolver function the link resolver to build URL for your site
defaultUrl string the URL to default to return if the preview doesn't correspond to a document (usually the home page of your site)
callback function to get the resulting URL (optional, you can get it from the Promise result)
Source:
Returns:
Type
Promise

query(the, additional, callback(err,)

Query the repository
Parameters:
Name Type Description
the string | array | Predicate query itself
additional object parameters. In NodeJS, pass the request as 'req'.
callback(err, function response)
Source:

queryFirst(the, additional, callback(err,)

Retrieve the document returned by the given query
Parameters:
Name Type Description
the string | array | Predicate query
additional object parameters. In NodeJS, pass the request as 'req'.
callback(err, function doc)
Source:

quickRoutes()

Retrieve quick routes definitions
Source:

ref(label) → {string}

Returns the ref ID for a given ref's label. Do not use like this: searchForm.ref(api.ref("Future release label")). Instead, set your ref once in a variable, and call it when you need it; this will allow to change the ref you're viewing easily for your entire page.
Parameters:
Name Type Description
label string the ref's label
Source:
Returns:
Type
string

refresh(callback) → {Promise}

Cleans api data from the cache and fetches an up to date copy.
Parameters:
Name Type Description
callback function Optional callback function that is called after the data has been refreshed
Source:
Returns:
Type
Promise

request()

Fetch a URL corresponding to a query, and parse the response as a Response object
Source:

response()

JSON documents to Response object
Source: