The Prismic API

Learn how the Prismic API works.


Prismic comprises three technologies:

  • A content management system (software for handling text and images)
  • An application called Slice Machine (an application for building slices)
  • An API (an access point for data)

These three technologies come together to form a powerful headless website-building platform.

What's an API?

For a basic introduction to APIs, read How Do APIs Work? on the Prismic Blog.

In order to understand what a headless website builder is and how Prismic works, it helps to understand APIs and how the Prismic API works.

Traditional website builders use the same software to both store content and generate the website that displays the content. A headless website builder doesn’t generate the website. This is a popular approach because it can be more flexible, sustainable, maintainable, scalable, and performant.

So, if a headless website builder doesn't generate the website, how does the content from a headless CMS get into the website? Via an API.

On this page, we'll explain how the Prismic API works.

Use Prismic's development kits for real-world API queries

This article provides a theoretical overview of how the Prismic API works. However, we don't recommend performing requests to the Prismic API manually in your app. We have official and community kits for building and sending API requests. We recommend using these kits. Learn more in the Technologies section.

The six Prismic API endpoints

Prismic has six API endpoints:

Repository API

Read-only

https://your-repo-name.cdn.prismic.io/api/v2/

Document API

Read-only

https://your-repo-name.cdn.prismic.io/api/v2/documents/search

Tag API

Read-only

https://your-repo-name.cdn.prismic.io/api/tags

GraphQL API

Read-only

https://your-repo-name.cdn.prismic.io/graphql

Custom Type API

Read/write

https://customtypes.prismic.io

Integration Field Write API

Write-only

https://if-api.prismic.io/if/write/your-repo-name--catalog-name

The most important endpoints are the Repository API and the Document API. They work together to deliver most Prismic content.

Refs and the Repository API

The Repository API is the gateway to your repository. When you query the Repository API, you get back some basic information about your repository, including the ref for the current version of your content, called the "master ref.” A ref is an identifier for a specific version of your content, like draft, scheduled, or — most importantly — newest. Every time you publish changes, a new ref is generated.

Every request for content includes a ref that states what version of the content to send back. That way, if you request content twice with the same ref, the API knows it can reuse cached data. Even though you make two requests to the API, the result is, on average, many times faster than querying uncached content.

When you make changes to your content, Prismic may store older refs. This ensures that a ref will work even if content has been changed since the request was initiated. However, in general, you should always use the most recent ref, as old refs will get purged eventually.

Here's a diagram of the whole process:

A diagram of the Prismic API
How a basic Prismic API query works.

The Repository API endpoint is located at:

Copy
https://your-repo-name.cdn.prismic.io/api/v2

(Replace your-repo-name with your repository's name.)

You can put the Repository API endpoint into your web browser's address bar. After you hit "enter", you should see something like this:

A screenshot of the Repository API endpoint in a browser.
JSON from the Repository API endpoint without formatting.

This is the raw JSON API response. It's unformatted, meaning it doesn't have any line breaks. You can install a browser extension to format it, like JSON Formatter for Chrome, JSON Peep for Safari, or JSONView for Firefox. Once formatted, the JSON should look more like this:

A screenshot of the Repository API endpoint in a browser.
JSON from the Repository API endpoint with formatting.

Why are refs so important?

The Prismic API is fast. Really fast. It has to be. It processes 20,000,000,000 requests each month, powering thousands of websites. One of the reasons the API is so fast is because it uses a system of refs and caching.

When a client requests a document from Prismic, the Prismic API checks the database for a document matching the description. It responds with what it finds. On average, that request-response cycle takes 250ms.

But what if the Prismic API has received the same request before?

A person standing at a bus stop

Let's use a metaphor. Imagine you're standing near a bus stop, and someone asks you when the next bus is coming. You pull out the timetable, check the times, and give them an answer. All in, it might take you a minute. But what if someone else comes along and asks the same question. You can give them an answer immediately because you already know.

Eventually, the bus will come and go. If a third person comes and asks when the next bus is coming, you'll need to check the timetable again.

The Prismic API works similarly. If the API receives a request it has already received, it sends back the same data. In that case, the request-response cycle takes a fraction of the time: 50ms.

How does the API know if it can send back the same data? Using refs. A ref is an identifier for a specific version of your content, like draft, scheduled, or — most importantly — newest. Every time you publish changes, a new ref is generated.

Every request for content includes a ref that states what version of the content to send back. That way, if you request content twice with the same ref, the API knows it can reuse cached data. Even though you make two requests to the API, the result is, on average, many times faster than querying uncached content.

Filters and the Document API

Prismic has two APIs to query content: the Document API, which uses Rest, and the GraphQL API. We'll discuss the GraphQL API later. For now, we'll query content from the Document API.

A basic query to the Document API looks like this:

Copy
https://prismicio-docs-v3.cdn.prismic.io/api/v2/documents/search?ref=Yj2uKBEAACIAOf9Q

Let's break that down:

https

This is an HTTPS request

prismicio-docs-v3

This is the name of the repository we're querying

cdn

This specifies that we're using the Prismic CDN, which is a special infrastructure to make queries faster

prismic.io

This is the Prismic domain

api/v2/documents/search

This is the Document API endpoint

?

The question mark marks the beginning of our query

ref=Yj2uKBEAACIAOf9Q

This is the ref that we'll use to query content

This query will return the 20 most recently changed documents from the API as JSON.

To get specific documents, you can use filters. Filters are a way to refine your query.

Prismic has 29 filters:

  • 7 filters for filtering logically
  • 3 filters for filtering numerically
  • 17 filters for filtering by date
  • 1 filter for filtering by similarity
  • 1 filter for filtering by location

The logical filter at is the most commonly used. It searches for a matching value. To search for the blog post with the unique ID (UID) of "hello-world", you would use this filter:

Copy
[at(my.blog_post.uid,"hello-world")]

Filters check every document and compare a specified property on that document against the provided value. If the document passes the comparison, the API will include the document in its results.

You can learn more about all of the filters and how to use them in the Filter Reference.

You can test your filters in Prismic's API Browser. To get to the API Browser, use the Repository API endpoint for your repository, but remove the .cdn segment, like so:

Copy
https://your-repo-name.prismic.io/api/v2

In the API Browser, you can construct queries with filters. For instance, the following query will search for any documents in the Prismic documentation repository that are tagged as either "Nuxt.js" or "Next.js", are not marked as "beta", and have a meta titled defined.

The Prismic API Browser
Constructing a query in the Prismic API Browser.

Then you can sort your queries using orderings. For instance, the following query will search for all documents in the Prismic documentation repository and order them by the date and time they were first published.

Sorting a query in the Prismic API Browser.
Sorting a query in the Prismic API Browser.

To learn more about orderings and other options you can add to your API query, check out the Document API Technical Reference.

To make a query to the API, we strongly recommend using a development kit. The development kit will take care of the query to the Repository API, the ref, and much more.

Tags and the Tag API

Prismic has a built-in tagging system. You can open any document in your repository, click on the tags bar at the top of the document, and type in a tag. You can use these tags to filter your documents in the document list.

If you need to fetch a list of all of the tags in your repository, you can query the Tag API endpoint:

Copy
https://your-repo-name.cdn.prismic.io/api/tags

This endpoint only returns a list of tags.

Selective fetching and the GraphQL API

GraphQL APIs are a popular alternative to Rest APIs. GraphQL APIs allow for more advanced and programmatic API requests. Every Prismic repository has a GraphQL endpoint alongside the Rest Document API endpoint:

Copy
https://your-repo-name.cdn.prismic.io/graphql

When you send a GET request to a Rest API, you generally receive an entire document as a response. It's like ordering a set menu at a restaurant. When you send a request to a GraphQL API, you ask for each specific data point — like ordering items à la carte.

One big advantage of GraphQL is that you always know what you will get. With a set menu, you don't necessarily know what's coming unless you've eaten at this restaurant before. Even then, the menu might change without warning. When you order à la carte, you know exactly what will be on your plate. For developers, this helps reduce bugs and improve performance.

You can construct your own GraphQL queries and see what the response looks like in the GraphQL Explorer:

Copy
https://your-repo-name.prismic.io/graphql

But, be warned! GraphQL is an advanced technology, and the syntax can be quite confusing.

Third-party data and the Integration Field API

Most content in Prismic is authored in the Prismic interface. The Prismic interface is intuitive, stable, and secure. This helps ensure that any changes to content are safe. However, sometimes, you need to incorporate content from another source — like e-commerce items from a product catalog. That's where the Integration Field API comes in.

Your developer can send data from a third-party service to the Integration Field API. If an Integration Field is included in your custom type, you can select data from the API to add to your document. With the API integration, your developer can keep the third-party data up-to-date without editing the document. This ensures that the API integration can't corrupt information in your repository, since the third-party data is safely sandboxed.

Web development with the Custom Type API

The Custom Types API is more advanced. Every document in Prismic belongs to a custom type. The custom type defines the structure of the content. A blog post has a different structure from a recipe or a sales page. You could have a "blog post" custom type, a "recipe" custom type, and a "sales page" custom type. Each one would have a different structure. Your developer needs to know this structure to build website pages that present this data. One way to see the structure is by viewing it in the Prismic interface. Another way to see (and edit) the structure is with the Custom Type API. This API allows for faster development workflows and advanced developer integrations. One such advanced integration is Slice Machine, our developer tool for building custom types.

Does Prismic have a Write API?

An API that lets you modify resources on the server is called a write API. Prismic has a Migration API that will allow developers to upload or edit documents in a repository. Changes will be saved in a draft state and must be published manually.

Similarly, Prismic has an Import/Export tool for uploading content.

To add third-party content to a repository, we recommend using the integration field, which has a write API.

How to build with the Prismic API

For more information on how to build with Prismic, visit the documentation for your web development framework:


Was this article helpful?
Not really
Yes, Thanks

Can't find what you're looking for? Spot an error in the documentation? Get in touch with us on our Community Forum or using the feedback form above.