- API References
@prismicio/client - v7
Overview
@prismicio/client
is Prismic’s SDK for fetching and working with content.
Fetch content with a client and many query methods.
Retrieve metadata about a Prismic repository using repository methods.
Migrate content to Prismic using a write-capable client.
Convert content to other formats using content helpers.
Code confidently knowing all APIs are type-safe with TypeScript.
This page documents all APIs provided by @prismicio/client
.
Install
@prismicio/client
can be installed in any JavaScript project.
npm install --save @prismicio/client
API
createClient()
Creates a Prismic client that can fetch content from a Prismic repository.
const client = createClient(repositoryName, config);
Parameters
repositoryName
string
The repository name from which content is fetched.
config
options
Options that configure content requests. See the config
parameter in createClient()
.
const client = createClient("example-prismic-repo", {
accessToken: "example-access-token",
});
accessToken
string
The secure token for accessing the API. Only needed if your repository is private.
routes
Route[]
A list of route resolver objects that define how a
document’s url
property is resolved.
type
string
The API ID of a document type.
path
string
The resolved path of the document with optional placeholders.
resolvers
Record<string, string>
A map of path placeholders to API IDs of the content relationships in the route’s document.
uid
string
A specific UID to which this route is scoped. The route is only defined for the document whose UID matches the given UID.
lang
string
A specific language to which this route is scoped. The route is only defined for documents whose locale matches the given locale.
brokenRoute
string
The url
value used for broken link fields. A link is broken when its
linked document has been unpublished or deleted.
defaultParams
object
Default parameters that will be sent with each query. These parameters can be overridden on each query if needed. See query parameters.
fetchOptions
RequestInit
Options provided to the client’s fetch()
on all network requests. These
options will be merged with internally required options. They can also be
overriden on a per-query basis using the query’s fetchOptions
parameter.
documentAPIEndpoint
string
The full Documents API V2 endpoint for the repository. Useful if you are using a proxy.
fetch
typeof fetch
The function used to make network requests to the Prismic Documents API.
In environments where a global fetch
function does not exist, this
function must be provided.
ref
string | (() => string | Promise<string>)
The ref used to query documents. It can optionally be a function that returns a ref.
Return type
A Prismic Client
that can fetch content from the configured Prismic repository.
See the available query methods below.
createWriteClient()
Creates a Prismic client that can write content to a Prismic repository. It includes all methods supported in createClient()
.
const client = createWriteClient(repositoryName, config);
Parameters
repositoryName
string
The repository name to which content is written.
config
object
writeToken
string
A Prismic write token that allows for writing content to the repository.
migrationAPIKey
string
A Prismic Migration API key that allows for working with the Migration API.
A random demo API key with strict rate limiting.
assetAPIEndpoint
string
The Prismic Asset API endpoint.
"https://asset-api.prismic.io/"
migrationAPIEndpoint
string
The Prismic Migration API endpoint.
"https://migration.prismic.io/"
Return type
A Prismic WriteClient
that can write content to the configured Prismic repository.
The client should be used with the createMigration()
function and the migrate()
client method.
createMigration()
Creates a Migration
instance that holds a set of migration procedures to execute, like “create a document” or “create an asset.” The migration can be executed with a write client’s migrate()
method.
const migration = createMigration();
Return value
A Migration
instance that can hold migration procedures.
Query methods
Fetch content from a Prismic repository using the client’s query methods.
The following example fetches all blog posts from the fr-fr
locale using the getAllByType()
method.
const client = createClient("example-prismic-repo");
const blogPosts = await client.getAllByType("blog_post", {
lang: "fr-fr",
});
Query parameters
All query methods accept optional parameters.
filters
string[]
One or more filters to filter documents for the query.
graphQuery
string
Specify which fields to retrieve and what content to retrieve from linked documents. See GraphQuery.
orderings
Ordering[]
Orders the results by the specified field(s). You can specify as many fields as you want.
field
string
The field used to order results.
direction
"asc" | "desc"
The order of the sorting for this field.
"asc"
lang
string
The locale from which to return results. Use "*"
to fetch content from
all locales.
page
number
The results page number when a query returns multiple pages of results.
pageSize
number
The maximum number of documents that the API will return for your query.
after
string
A document ID. Only documents listed after the document ID are returned.
Can be used along with the orderings
parameter.
accessToken
string
The secure token for accessing the API. Only needed if your repository is private.
brokenRoute
string
The route populated in the url
property for broken links. A link is
broken when its linked document has been unpublished or deleted.
fetchLinks
string[]
A list of fields to retrieve from linked documents. Prefer the
graphQuery
option over fetchLinks
.
integrationFieldsRef
string
The ref used to populate integration fields.
ref
string
The ref used to query documents.
routes
Route[]
Orders the results by the specified field(s). You can specify as many
fields as you want. See Route
.
fetch
string[]
Make queries faster by only retrieving the specified fields. Use the
graphQuery
option instead.
predicates
string[]
filters
.get()
Fetches documents based on the params
argument. Results are paginated.
await client.get(params);
Parameters
Return value
A paginated list of documents.
results
PrismicDocument[]
A paginated list of documents matching the query.
page
number
The page number for the page of results.
total_pages
number
The total number of pages.
results_size
number
The number of results in the page of results.
results_per_page
number
The maximum number of results per page.
total_results_size
number
The total number of results within all pages.
next_page
string | null
The Documents API V2 URL to the next page, if one exists.
prev_page
string | null
The Documents API V2 URL to the previous page, if one exists.
getFirst()
Fetches the first document returned based on the params
argument.
await client.getFirst(params);
Parameters
Return value
A PrismicDocument
if one is found, null
otherwise.
Possible errors
- Throws
NotFoundError
if a matching document cannot be found.
getByID()
Fetches a document with a specific ID.
await client.getByID(id, params);
Parameters
id
string
Return value
A PrismicDocument
if one is found, null
otherwise.
Possible errors
- Throws
NotFoundError
if a matching document cannot be found.
getByIDs()
Fetches documents with specific IDs. Results are paginated.
await client.getByIDs(ids, params);
Parameters
ids
string[]
Return value
A paginated list of documents.
results
PrismicDocument[]
A paginated list of documents matching the query.
page
number
The page number for the page of results.
total_pages
number
The total number of pages.
results_size
number
The number of results in the page of results.
results_per_page
number
The maximum number of results per page.
total_results_size
number
The total number of results within all pages.
next_page
string | null
The Documents API V2 URL to the next page, if one exists.
prev_page
string | null
The Documents API V2 URL to the previous page, if one exists.
getAllByIDs()
Fetches documents with specific IDs. This method may make multiple network requests to fetch all matching documents.
await client.getAllByIDs(ids, params);
Parameters
ids
string[]
Return value
An array of matching PrismicDocument
documents.
getByUID()
Fetches a document with a specific UID and type.
await client.getByUID(type, uid, params);
Parameters
type
string
uid
string
Return value
A PrismicDocument
if one is found.
Possible errors
- Throws
NotFoundError
if a matching document cannot be found.
getByUIDs()
Fetches documents with specific UIDs and a specific type. Results are paginated.
await client.getByUIDs(type, uids, params);
Parameters
type
string
uids
string[]
Return value
A paginated list of documents.
results
PrismicDocument[]
A paginated list of documents matching the query.
page
number
The page number for the page of results.
total_pages
number
The total number of pages.
results_size
number
The number of results in the page of results.
results_per_page
number
The maximum number of results per page.
total_results_size
number
The total number of results within all pages.
next_page
string | null
The Documents API V2 URL to the next page, if one exists.
prev_page
string | null
The Documents API V2 URL to the previous page, if one exists.
getAllByUIDs()
Fetches documents with specific UIDs and a specific type. This method may make multiple network requests to fetch all matching documents.
await client.getAllByUIDs(type, uids, params);
Parameters
type
string
uids
string[]
Return value
An array of matching PrismicDocument
documents.
getSingle()
Fetches a specific single type document.
await client.getSingle(type, params);
Parameters
type
string
Return value
A PrismicDocument
if one is found, null
otherwise.
Possible errors
- Throws
NotFoundError
if a matching document cannot be found.
getByType()
Fetches documents with a specific type. Results are paginated.
await client.getByType(type, params);
Parameters
type
string
Return value
A paginated list of documents.
results
PrismicDocument[]
A paginated list of documents matching the query.
page
number
The page number for the page of results.
total_pages
number
The total number of pages.
results_size
number
The number of results in the page of results.
results_per_page
number
The maximum number of results per page.
total_results_size
number
The total number of results within all pages.
next_page
string | null
The Documents API V2 URL to the next page, if one exists.
prev_page
string | null
The Documents API V2 URL to the previous page, if one exists.
getAllByType()
Fetches documents with a specific type. This method may make multiple network requests to fetch all matching documents.
await client.getAllByType(type, params);
Parameters
type
string
Return value
An array of matching PrismicDocument
documents.
getByTag()
Fetches documents with a specific tag. Results are paginated.
await client.getByTag(tag, params);
Parameters
tag
string
Return value
A paginated list of documents.
results
PrismicDocument[]
A paginated list of documents matching the query.
page
number
The page number for the page of results.
total_pages
number
The total number of pages.
results_size
number
The number of results in the page of results.
results_per_page
number
The maximum number of results per page.
total_results_size
number
The total number of results within all pages.
next_page
string | null
The Documents API V2 URL to the next page, if one exists.
prev_page
string | null
The Documents API V2 URL to the previous page, if one exists.
getAllByTag()
Fetches documents with a specific tag. This method may make multiple network requests to fetch all matching documents.
await client.getAllByTag(tag, params);
Parameters
tag
string
Return value
An array of matching PrismicDocument
documents.
getByEveryTag()
Fetches documents with every tag from a list of tags. Results are paginated.
await client.getByEveryTag(tags, params);
Parameters
tags
string[]
Return value
A paginated list of documents.
results
PrismicDocument[]
A paginated list of documents matching the query.
page
number
The page number for the page of results.
total_pages
number
The total number of pages.
results_size
number
The number of results in the page of results.
results_per_page
number
The maximum number of results per page.
total_results_size
number
The total number of results within all pages.
next_page
string | null
The Documents API V2 URL to the next page, if one exists.
prev_page
string | null
The Documents API V2 URL to the previous page, if one exists.
getAllByEveryTag()
Fetches documents with every tag from a list of tags. This method may make multiple network requests to fetch all matching documents.
await client.getAllByEveryTag(tags, params);
Parameters
tags
string[]
Return value
An array of matching PrismicDocument
documents.
getBySomeTags()
Fetches documents with at least one tag from a list of tags. Results are paginated.
await client.getBySomeTags(tags, params);
Parameters
tags
string[]
Return value
A paginated list of documents.
results
PrismicDocument[]
A paginated list of documents matching the query.
page
number
The page number for the page of results.
total_pages
number
The total number of pages.
results_size
number
The number of results in the page of results.
results_per_page
number
The maximum number of results per page.
total_results_size
number
The total number of results within all pages.
next_page
string | null
The Documents API V2 URL to the next page, if one exists.
prev_page
string | null
The Documents API V2 URL to the previous page, if one exists.
getAllBySomeTags()
Fetches documents with at least one tag from a list of tags. This method may make multiple network requests to fetch all matching documents.
await client.getAllBySomeTags(tags, params);
Parameters
tags
string[]
Return value
An array of matching PrismicDocument
documents.
queryLatestContent()
Configures the client to query the latest published content. This is the client’s default mode.
client.queryLatestContent();
queryContentFromReleaseByID()
Configures the client to query content from a release with a specific ID.
client.queryContentFromReleaseByID(releaseID);
Parameters
releaseID
string
queryContentFromReleaseByLabel()
Configures the client to query content from a release with a specific label. A release’s label is its name shown in the Page Builder.
client.queryContentFromReleaseByLabel(releaseLabel);
Parameters
releaseLabel
string
queryContentFromRef()
Configures the client to query content from a specific ref.
client.queryContentFromRef(ref);
Parameters
ref
string | (() => string | Promise<string>)
The ref used to query documents. It can optionally be a function that returns a ref.
Repository methods
Fetch repository metadata using the client’s repository methods.
getRepository()
Fetches metadata about the client’s Prismic repository.
await client.getRepository();
Return value
refs
Ref[]
id
string
ref
string
The value used to select a content version.
label
string
The ref’s name. The master ref is always named “Master.”
isMasterRef
boolean
Determines if the ref is the master ref. The master ref contains the latest published content.
scheduledAt
string
If the ref is associated with a release, this value is the timestamp at which the release will be automatically published, if set.
languages
Language[]
The repository’s enabled languages.
id
string
name
string
is_master
boolean
Determines if the language is the default language of the repository.
types
Record<string, string>
The repository’s content model API IDs mapped to their labels.
integrationFieldsRef
string | null
The value used to select a content version for integration field data.
license
string
Licensing information for the repository’s content.
oauth_initiate
string
The URL used to begin the OAuth process for the repository.
oauth_token
string
The token used for the OAuth process for the repository.
version
string
The version of the API. You probably don’t need this.
bookmarks
unknown
experiments
unknown
forms
unknown
getRefs()
Fetches the repository’s active refs.
await client.getRefs();
Return value
An array of ref objects:
id
string
ref
string
The value used to select a content version.
label
string
The ref’s name. The master ref is always named “Master.”
isMasterRef
boolean
Determines if the ref is the master ref. The master ref contains the latest published content.
scheduledAt
string
If the ref is associated with a release, this value is the timestamp at which the release will be automatically published, if set.
getRefByLabel()
Fetches a ref by its label. A release ref’s label is its name shown in the Page Builder.
client.getRefByLabel(releaseLabel);
Parameters
releaseLabel
string
Possible errors
- Throws
PrismicError
if a matching release cannot be found.
getMasterRef()
Fetches the repository’s master ref.
client.getMasterRef();
getReleases()
Fetches the repository’s active releases.
await client.getReleases();
Return value
An array of ref objects:
id
string
ref
string
The value used to select a content version.
label
string
The ref’s name. The master ref is always named “Master.”
isMasterRef
boolean
Determines if the ref is the master ref. The master ref contains the latest published content.
scheduledAt
string
If the ref is associated with a release, this value is the timestamp at which the release will be automatically published, if set.
getReleaseByID()
Fetches a release with a specific ID.
await client.getReleaseByID(releaseID);
Parameters
releaseID
string
Return value
id
string
ref
string
The value used to select a content version.
label
string
The ref’s name. The master ref is always named “Master.”
isMasterRef
boolean
Determines if the ref is the master ref. The master ref contains the latest published content.
scheduledAt
string
If the ref is associated with a release, this value is the timestamp at which the release will be automatically published, if set.
Possible errors
- Throws
PrismicError
if a matching release cannot be found.
getReleaseByLabel()
Fetches a release by its label. A release ref’s label is its name shown in the Page Builder.
await client.getReleaseByLabel(releaseLabel);
Parameters
releaseLabel
string
Return value
id
string
ref
string
The value used to select a content version.
label
string
The ref’s name. The master ref is always named “Master.”
isMasterRef
boolean
Determines if the ref is the master ref. The master ref contains the latest published content.
scheduledAt
string
If the ref is associated with a release, this value is the timestamp at which the release will be automatically published, if set.
Possible errors
- Throws
PrismicError
if a matching release cannot be found.
getTags()
Fetches the repository’s document tags.
client.getTags();
Returns
An array of string
tags.
buildQueryURL()
Builds a Documents API query URL with a set of parameters.
client.buildQueryURL(params);
resolvePreviewURL()
Fetches a previewed document’s URL using a preview token and document ID. The token and document ID are provided in the URL opened after clicking the Page Builder’s Preview the page button.
await client.resolvePreviewURL(params);
Parameters
params
Parameters that determine how the preview URL is resolved.
previewToken
string
The preview token provided by the preview session.
documentID
string
The previewed document’s ID used to determine the destination URL.
defaultURL
string
The URL used if a document URL cannot be resolved.
"/"
linkResolver
(field: ContentRelationshipField) => string | null | undefined
A link resolver used to convert a content relationship field. If the link resolver returns a value, it takes priority over a route resolver.
Return value
The previewed document’s URL as a string
, or defaultURL
if a matching document cannot be found.
graphQLFetch()
A preconfigured fetch()
function for Prismic’s GraphQL API that can be provided to GraphQL clients. It adds the required prismic-ref
and Authorization
headers to all requests.
client.graphQLFetch;
migrate()
Executes a set of migration procedures added to a Migration
instance created by createMigration()
.
client.migrate(migration, params);
Parameters
params
object
Parameters that affect how the migration is executed.
reporter
(event: Event) => void
A function called when events occur during the migration.
type
string
The event type.
data
object
Metadata about the event.
React to migration events
You can react to events during the migration process using the reporter
option. Each event provides metadata about the event.
The following example logs the event type when an event occurs.
client.migrate(migration, {
reporter(event) {
console.log(event.type);
},
});
The following events are supported:
start
- The start of the migration process.end
- The end of the migration process.assets:creating
- An asset is being created.assets:created
- All assets were created.documents:masterLocale
- Fetching the master locale for a document.documents:creating
- A document is being created.documents:created
- All documents were created.documents:updating
- A document is being updated.documents:updated
- All documents were updated.
Migration methods
You can migrate content to Prismic using a special write-capable version of the client.
These methods are available on the Migration
instance returned by createMigration()
.
createAsset()
Creates a new asset in a Migration
instance. The asset will not be created in your repository until the migration is executed with migrate()
.
migration.createAsset(asset);
migration.createAsset(field);
migration.createAsset(file, filename, params);
Parameters
One of the three signatures must be used:
file
Blob | File | URL
Binary data for the asset. It can also be a URL pointing to a hosted asset that will be downloaded.
filename
string
The filename shown in the repository’s media library.
params
object
Metadata about the image.
notes
string
Notes shown in the repository’s media library.
credits
string
Credits shown in the repository’s media library.
tags
string[]
Return value
A reference to the asset that can be referenced in migration documents. The asset won’t be created until the migration is executed with migrate()
.
createDocument()
Creates a new document in a Migration
instance. The document will not be created in your repository until the migration is executed with migrate()
.
migration.createDocument(document, title, params);
Parameters
document
PrismicDocument
A Prismic document in API v2 format.
params
object
masterLanguageDocument
PrismicMigrationDocument | PrismicDocument | ContentRelationshipField | (() => PrismicMigrationDocument | PrismicDocument | ContentRelationshipField)
The document used as the new document’s master language.
Return value
A reference to the document that can be referenced in other migration documents. The document won’t be created until the migration is executed with migrate()
.
updateDocument()
Updates an existing document in a Migration
instance. The document will not be updated in your repository until the migration is executed with migrate()
.
migration.updateDocument(document, title);
Parameters
document
PrismicDocument
The Prismic document in API v2 format with updated content.
Return value
A reference to the document that can be referenced in other migration documents. The document won’t be created until the migration is executed with migrate()
.
createDocumentFromPrismic()
Creates a new document in a Migration
instance based on an existing Prismic document. Assets and content relationships will be created and resolved automatically. The document and its assets will not be created in your repository until the migration is executed with migrate()
.
migration.createDocumentFromPrismic(document, title);
Parameters
document
PrismicDocument
A Prismic document in API v2 format.
Return value
A reference to the document that can be referenced in other migration documents. The document won’t be created until the migration is executed with migrate()
.
getByUID()
Gets a document with a specific UID and type from within a Migration
instance. Use this method to retrieve a document that has not yet been migrated using migrate()
.
migration.getByUID(type, uid);
Parameters
type
string
uid
string
Return value
A PrismicMigrationDocument
if one is found, undefined
otherwise.
getSingle()
Gets a specific single type document from within a Migration
instance. Use this method to retrieve a document that has not yet been migrated using migrate()
.
migration.getSingle(type);
Parameters
type
string
Return value
A PrismicMigrationDocument
if one is found, undefined
otherwise.
Content helpers
@prismicio/client
provides helpers that manipulate Prismic content. The helpers can be used throughout your website or in scripts.
Content helpers can be imported directly from @prismicio/client
:
import { asDate } from "@prismicio/client";
asDate()
Converts a date or timestamp field to a JavaScript Date
.
asDate(field);
Parameters
field
DateField | TimestampField
Return value
field
as a Date
, or null
if the field is empty.
asLink()
Converts a link field, link to media field, content relationship field, or Prismic document to a URL.
asLink(fieldOrDocument, params);
Parameters
fieldOrDocument
LinkField | LinkToMediaField | ContentRelationshipField | PrismicDocument
config
object
Configuration that affects how a link is converted.
linkResolver
(field: ContentRelationshipField) => string | null | undefined
A link resolver used to convert a content relationship field. If the link resolver returns a value, it takes priority over a route resolver.
Return value
The field’s or document’s URL as a string
, or null
if the field is empty.
asLinkAttrs()
Converts a link field, link to media field, content relationship field, or Prismic document to a set of link attributes. The attributes are intended to be passed to <a>
HTML elements.
asLinkAttrs(fieldOrDocument, params);
Parameters
fieldOrDocument
LinkField | LinkToMediaField | ContentRelationshipField | PrismicDocument
config
object
Configuration that affects how a link is converted.
linkResolver
(field: ContentRelationshipField) => string | null | undefined
A link resolver used to convert a content relationship field. If the link resolver returns a value, it takes priority over a route resolver.
rel
(metadata) => string | undefined
A function that determines the rel
attribute value. The function is
provided metadata about the link.
Return value
An object of <a>
HTML attributes.
target
"_blank" | undefined
The link’s
target
value.
"_blank"
if the field’s “Open in new tab” option is checked.
asHTML()
Converts a rich text field to HTML. The output can be customized using a set of serialization functions.
asHTML(field, config);
Parameters
field
RichTextField
config
object
Configuration that affects how the rich text is converted.
linkResolver
(field: ContentRelationshipField) => string | null | undefined
A link resolver used to convert a content relationship field. If the link resolver returns a value, it takes priority over a route resolver.
serializer
object | function
A map of rich text block types to functions that return HTML. A single function can also be provided, but an object is recommended.
Standard HTML elements (e.g. heading1
becomes a <h1>
).
Return value
The rich text field as an HTML string
.
asText()
Converts a rich text field to plain text.
asText(field, config);
Parameters
field
RichTextField
config
object
Configuration that affects how the rich text is converted.
separator
string
The string inserted between rich text blocks.
" "
Return value
The rich text field as a plain string
.
asImageSrc()
Converts an image field to an image URL. The image can be transformed using imgix’s Rendering API.
asImageSrc(field, transformations);
Parameters
field
ImageField
transformations
object
imgix transformations to apply. See imgix’s Rendering API for a list of supported transformations.
Return value
The image field’s URL as a string
.
asImageWidthSrcSet()
Converts an image field to a srcset
designed for <img>
HTML elements. The srcset
uses width descriptors, like 400w
and 600w
.
The image can be transformed using imgix’s Rendering API.
asImageWidthSrcSet(field, config);
Parameters
field
ImageField
config
object
Configuration that affects how the image is converted.
widths
"thumbnails" | number[]
The width descriptors to be used in the srcset
. If "thumbnails"
is
provided, the widths of the image’s responsive
views are used.
[640, 750, 828, 1080, 1200, 1920, 2048, 3840]
...transformations
object
imgix transformations to apply. See imgix’s Rendering API for a list of supported transformations.
Return value
An object of <img>
HTML attributes.
asImagePixelDensitySrcSet()
Converts an image field to a srcset
designed for <img>
HTML elements. The srcset
uses pixel density descriptors, like 1x
and 2x
.
The image can be transformed using imgix’s Rendering API.
asImagePixelDensitySrcSet(field, config);
Parameters
field
ImageField
config
object
Configuration that affects how the image is converted.
pixelDensities
number[]
The pixel density descriptors to be used in the srcset
.
[1, 2, 3]
...transformations
object
imgix transformations to apply. See imgix’s Rendering API for a list of supported transformations.
Return value
An object of <img>
HTML attributes.
mapSliceZone()
Converts slices in a slice zone to objects of your choosing. It is designed to add data, remove unused content, or preprocess content on a server.
mapSliceZone(slices, mappers, context);
Parameters
slices
SliceZone
mappers
Record<string, (args: Metadata) => any>
A map of slice type IDs to mapper functions. The return value of each function becomes the slice’s new value.
slice
Slice
index
number
The index of slice
in the slice zone.
slices
SliceZone
The slices
argument passed to mapSliceZone()
.
context
any
The context
argument passed to mapSliceZone()
.
context
any
Arbitrary data provided to each function in mappers
.
Return value
The provided slice zone with each slice converted according to the functions in the mappers
argument.
Mapping to UI component props
When using the <SliceZone>
component from @prismicio/react, @prismicio/vue, or @prismicio/svelte, properties returned by the mapper function become the slice component’s props.
The following example shows how mapSliceZone()
can populate a React component’s slice
and lang
props.
const slices = mapSliceZone(document.data.slices, {
current_language: ({ slice }) => {
// The slice's component will be rendered like:
// <CurrentLanguage slice={slice} lang={document.lang} />
return { slice, lang: document.lang };
},
});
function CurrentLanguage({ slice, lang }) {
return <p>The current language is: {lang}</p>;
}
isFilled
A set of functions that determine if a field has content or not. Each field type has its own function within isFilled
.
isFilled.richText(field);
The following functions are available:
isFilled.color(field)
isFilled.contentRelationship(field)
isFilled.date(field)
isFilled.embed(field)
isFilled.geoPoint(field)
isFilled.group(field)
isFilled.image(field)
isFilled.imageThumbnail(field)
isFilled.integrationField(field)
isFilled.keyText(field)
isFilled.link(field)
isFilled.linkToMedia(field)
isFilled.number(field)
isFilled.richText(field)
isFilled.select(field)
isFilled.table(field)
isFilled.timestamp(field)
isFilled.title(field)
Fields that are marked as repeatable, such as a repeatable list of links, can be checked with isFilled.repeatable()
.
isFilled.repeatable(field);
Slice zones can also be checked for at least one slice.
isFilled.sliceZone(slices);
Parameters
field
RichTextField | ImageField | ...
A field to check. The field type must match the isFilled
checker.
Return value
true
if the field has content, false
otherwise.
Query filters
You can adjust which documents are returned by query methods using filters. @prismicio/client
provides helpers to write filters.
import { filter } from "@prismicio/client";
Each filter type has its own function within filter
.
The following example uses the not()
filter to fetch all Page documents except the document with the home
UID.
await client.getAllByType("page", {
filters: [filter.not("my.page.uid", "home")],
});
at()
Checks that a field or document metadata property matches a value exactly.
filter.at(path, value);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
value
string | number | boolean | Date | string[]
The value to compare against. A string[]
is only allowed when path
is
"document.tags"
.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
"document.type"
"document.tags"
"document.id"
"my.[document-type].uid"
"my.[document-type].[key-text-field]"
"my.[document-type].[select-field]"
"my.[document-type].[number-field]"
"my.[document-type].[date-field]"
"my.[document-type].[boolean-field]"
"my.[document-type].[content-relationship-field]"
not()
Checks that a field or document metadata property doesn’t match a value exactly.
filter.not(path, value);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
value
string | number | boolean | Date | string[]
The value to compare against. A string[]
is only allowed when path
is
"document.tags"
.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
"document.type"
"document.tags"
"document.id"
"my.[document-type].uid"
"my.[document-type].[key-text-field]"
"my.[document-type].[select-field]"
"my.[document-type].[number-field]"
"my.[document-type].[date-field]"
"my.[document-type].[boolean-field]"
"my.[document-type].[content-relationship-field]"
any()
Includes documents with a field or metadata property matching a value from a given list.
filter.any(path, values);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
values
(string | number | boolean | Date)[]
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
"document.type"
"document.tags"
"document.id"
"my.[document-type].uid"
"my.[document-type].[key-text-field]"
"my.[document-type].[select-field]"
"my.[document-type].[number-field]"
"my.[document-type].[date-field]"
"my.[document-type].[boolean-field]"
"my.[document-type].[content-relationship-field]"
in()
Includes documents with an ID or UID matching a value from a given list. This filter is much more efficient at that than the any()
filter.
filter.in(path, values);
Parameters
path
string
A path to an ID or UID property. See the supported paths below.
values
string[]
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
"document.id"
"my.[document-type].uid"
fulltext()
Includes documents with a matching string anywhere in the document. It can also check within a specific rich text, text, or select field.
filter.fulltext(path, searchTerm);
Parameters
path
"document" | string
A path to a field or "document"
to search the whole document. See the supported paths below.
searchTerm
string
The search term to find.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
"document"
"my.[document-type].uid"
"my.[document-type].[rich-text-field]"
"my.[document-type].[text-field]"
"my.[document-type].[select-field]"
has()
Includes documents that have a value for a specific field.
filter.has(path);
Parameters
path
string
A path to a field. See the supported paths below.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
"my.[document-type].[field]"
Groups and slices are not supported.
missing()
Includes documents that do not have a value for a specific field. The inverse of has()
.
filter.missing(path);
Parameters
path
string
A path to a field. See the supported paths below.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
"my.[document-type].[field]"
Groups and slices are not supported.
similar()
Includes documents that have similar content to a given document. This filter allows you to build content discovery features, like a “Related posts” section.
filter.similar(documentID, specificity);
Parameters
documentID
string
The ID of the document used to compare other documents against.
specificity
number
The maximum number of documents that a term may appear in to be considered relevant.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
geopointNear()
Includes documents with a geopoint field value near the given coordinates.
filter.geopointNear(path, latitude, longitude, radius);
Parameters
latitude
number
The latitude coordinate.
longitude
number
The longitude coordinate.
radius
number
The search radius in kilometers.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
"my.[document-type].[geopoint-field]"
numberLessThan()
Includes documents with a number field value less than a given number.
filter.numberLessThan(path, number);
Parameters
number
number
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
"my.[document-type].[number-field]"
numberGreaterThan()
Includes documents with a number field value greater than a given number.
filter.numberGreaterThan(path, number);
Parameters
number
number
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
"my.[document-type].[number-field]"
numberInRange()
Includes documents with a number field value within a given range.
filter.numberInRange(path, lowerLimit, upperLimit);
Parameters
lowerLimit
number
upperLimit
number
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
"my.[document-type].[number-field]"
dateAfter()
Includes documents with a date field, timestamp field, or timestamp metadata property after a given date.
filter.dateAfter(path, date);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
date
string | number | Date
The date to compare. The value can be an ISO 8601 date or timestamp (e.g.
2002-08-28
or 2002-08-28T15:30:00+0300
), a 13-digit Epoch timestamp
(e.g. 1030545000000
), or a
Date
object.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
document.first_publication_date
document.last_publication_date
my.[custom-type].[date-field]
my.[custom-type].[timestamp-field]
dateBefore()
Includes documents with a date field, timestamp field, or timestamp metadata property before a given date.
filter.dateBefore(path, date);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
date
string | number | Date
The date to compare. The value can be an ISO 8601 date or timestamp (e.g.
2002-08-28
or 2002-08-28T15:30:00+0300
), a 13-digit Epoch timestamp
(e.g. 1030545000000
), or a
Date
object.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
document.first_publication_date
document.last_publication_date
my.[custom-type].[date-field]
my.[custom-type].[timestamp-field]
dateBetween()
Includes documents with a date field, timestamp field, or timestamp metadata property between given dates.
filter.dateBetween(path, startDate, endDate);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
startDate
string | number | Date
The start date to compare. The value can be an ISO 8601 date or timestamp
(e.g. 2002-08-28
or 2002-08-28T15:30:00+0300
), a 13-digit Epoch
timestamp (e.g. 1030545000000
), or a
Date
object.
endDate
string | number | Date
The end date to compare. The value can be an ISO 8601 date or timestamp
(e.g. 2002-08-28
or 2002-08-28T15:30:00+0300
), a 13-digit Epoch
timestamp (e.g. 1030545000000
), or a
Date
object.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
document.first_publication_date
document.last_publication_date
my.[custom-type].[date-field]
my.[custom-type].[timestamp-field]
dateDayOfMonth()
Includes documents with a date field, timestamp field, or timestamp metadata property that is on a specific day of the month.
filter.dateDayOfMonth(path, dayOfMonth);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
dayOfMonth
number
The day of the month to compare.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
document.first_publication_date
document.last_publication_date
my.[custom-type].[date-field]
my.[custom-type].[timestamp-field]
dateDayOfMonthAfter()
Includes documents with a date field, timestamp field, or timestamp metadata property that is after a specific day of the month.
filter.dateDayOfMonthAfter(path, dayOfMonth);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
dayOfMonth
number
The day of the month to compare.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
document.first_publication_date
document.last_publication_date
my.[custom-type].[date-field]
my.[custom-type].[timestamp-field]
dateDayOfMonthBefore()
Includes documents with a date field, timestamp field, or timestamp metadata property that is before a specific day of the month.
filter.dateDayOfMonthBefore(path, dayOfMonth);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
dayOfMonth
number
The day of the month to compare.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
document.first_publication_date
document.last_publication_date
my.[custom-type].[date-field]
my.[custom-type].[timestamp-field]
dateDayOfWeek()
Includes documents with a date field, timestamp field, or timestamp metadata property that is on a specific day of the week.
filter.dateDayOfWeek(path, dayOfWeek);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
dayOfWeek
string | number
The day of the week to compare. See the supported days of the week below.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
document.first_publication_date
document.last_publication_date
my.[custom-type].[date-field]
my.[custom-type].[timestamp-field]
Supported days of the week
The following days of the week are supported by this filter:
"monday"
,"mon"
,1
"tuesday"
,"tue"
,2
"wednesday"
,"wed"
,3
"thursday"
,"thu"
,4
"friday"
,"fri"
,5
"saturday"
,"sat"
,6
"sunday"
,"sun"
,7
Strings can be capitalized (e.g. "Monday"
) or all caps (e.g. "MONDAY"
).
dateDayOfWeekAfter()
Includes documents with a date field, timestamp field, or timestamp metadata property that is after a specific day of the week.
filter.dateDayOfWeekAfter(path, dayOfWeek);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
dayOfWeek
string | number
The day of the week to compare. See the supported days of the week below.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
document.first_publication_date
document.last_publication_date
my.[custom-type].[date-field]
my.[custom-type].[timestamp-field]
Supported days of the week
The following days of the week are supported by this filter:
"monday"
,"mon"
,1
"tuesday"
,"tue"
,2
"wednesday"
,"wed"
,3
"thursday"
,"thu"
,4
"friday"
,"fri"
,5
"saturday"
,"sat"
,6
"sunday"
,"sun"
,7
Strings can be capitalized (e.g. "Monday"
) or all caps (e.g. "MONDAY"
).
dateDayOfWeekBefore()
Includes documents with a date field, timestamp field, or timestamp metadata property that is before a specific day of the week.
filter.dateDayOfWeekBefore(path, dayOfWeek);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
dayOfWeek
string | number
The day of the week to compare. See the supported days of the week below.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
document.first_publication_date
document.last_publication_date
my.[custom-type].[date-field]
my.[custom-type].[timestamp-field]
Supported days of the week
The following days of the week are supported by this filter:
"monday"
,"mon"
,1
"tuesday"
,"tue"
,2
"wednesday"
,"wed"
,3
"thursday"
,"thu"
,4
"friday"
,"fri"
,5
"saturday"
,"sat"
,6
"sunday"
,"sun"
,7
Strings can be capitalized (e.g. "Monday"
) or all caps (e.g. "MONDAY"
).
dateMonth()
Includes documents with a date field, timestamp field, or timestamp metadata property that is within a specific month.
filter.dateMonth(path, month);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
month
string | number
The month to compare. See the supported months below.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
document.first_publication_date
document.last_publication_date
my.[custom-type].[date-field]
my.[custom-type].[timestamp-field]
Supported months
The following months are supported by this filter:
"january"
,"jan"
,1
"february"
,"feb"
,2
"march"
,"mar"
,3
"april"
,"apr"
,4
"may"
,"may"
,5
"june"
,"jun"
,6
"july"
,"jul"
,7
"august"
,"aug"
,8
"september"
,"sep"
,9
"october"
,"oct"
,10
"november"
,"nov"
,11
"december"
,"dec"
,12
Strings can be capitalized (e.g. "Monday"
) or all caps (e.g. "MONDAY"
).
dateMonthAfter()
Includes documents with a date field, timestamp field, or timestamp metadata property that is after a specific month.
filter.dateMonthAfter(path, month);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
month
string | number
The month to compare. See the supported months below.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
document.first_publication_date
document.last_publication_date
my.[custom-type].[date-field]
my.[custom-type].[timestamp-field]
Supported months
The following months are supported by this filter:
"january"
,"jan"
,1
"february"
,"feb"
,2
"march"
,"mar"
,3
"april"
,"apr"
,4
"may"
,"may"
,5
"june"
,"jun"
,6
"july"
,"jul"
,7
"august"
,"aug"
,8
"september"
,"sep"
,9
"october"
,"oct"
,10
"november"
,"nov"
,11
"december"
,"dec"
,12
Strings can be capitalized (e.g. "Monday"
) or all caps (e.g. "MONDAY"
).
dateMonthBefore()
Includes documents with a date field, timestamp field, or timestamp metadata property that is before a specific month.
filter.dateMonthBefore(path, month);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
month
string | number
The month to compare. See the supported months below.
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
document.first_publication_date
document.last_publication_date
my.[custom-type].[date-field]
my.[custom-type].[timestamp-field]
Supported months
The following months are supported by this filter:
"january"
,"jan"
,1
"february"
,"feb"
,2
"march"
,"mar"
,3
"april"
,"apr"
,4
"may"
,"may"
,5
"june"
,"jun"
,6
"july"
,"jul"
,7
"august"
,"aug"
,8
"september"
,"sep"
,9
"october"
,"oct"
,10
"november"
,"nov"
,11
"december"
,"dec"
,12
Strings can be capitalized (e.g. "Monday"
) or all caps (e.g. "MONDAY"
).
dateYear()
Includes documents with a date field, timestamp field, or timestamp metadata property that is within a specific year.
filter.dateYear(path, year);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
year
number
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
document.first_publication_date
document.last_publication_date
my.[custom-type].[date-field]
my.[custom-type].[timestamp-field]
dateHour()
Includes documents with a date field, timestamp field, or timestamp metadata property that is within a specific hour.
filter.dateHour(path, hour);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
hour
number
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
document.first_publication_date
document.last_publication_date
my.[custom-type].[date-field]
my.[custom-type].[timestamp-field]
dateHourAfter()
Includes documents with a date field, timestamp field, or timestamp metadata property that is after a specific hour.
filter.dateHourAfter(path, hour);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
hour
number
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
document.first_publication_date
document.last_publication_date
my.[custom-type].[date-field]
my.[custom-type].[timestamp-field]
dateHourBefore()
Includes documents with a date field, timestamp field, or timestamp metadata property that is before a specific hour.
filter.dateHourBefore(path, hour);
Parameters
path
string
A path to a field or document metadata property. See the supported paths below.
hour
number
Return value
The filter as a string
. It can be used in a query method’s filters
parameter.
Supported paths
The following paths are supported by this filter:
document.first_publication_date
document.last_publication_date
my.[custom-type].[date-field]
my.[custom-type].[timestamp-field]