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.
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.
What's an API?
For a basic introduction to APIs, read How Do APIs Work? on the Prismic Blog.
The six Prismic API endpoints
Prismic has six API endpoints:
Custom Type API
Integration Fields Write API
The most important endpoints are the Entry API and the Document API. They work together to deliver most Prismic content.
Refs and the Entry API
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?
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.
Here's a diagram of the whole process:
Where does the ref come from? The Entry API.
The Entry API is the gateway to your repository. When you query the Entry API, you get back some basic information about your repository, including the ref for the current version of your content, called the "master ref."
The Entry API endpoint is located at:
(Replace your-repo-name with your repository's name.)
You can put the Entry API endpoint into your web browser's address bar. After you hit "enter", you should see something like this:
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:
There's a lot of information here. But the most important property is the first one, the refs property. You must use the master ref to query your current content from the Document API.
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.
Predicates 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:
Let's break that down:
This is an HTTPS request
This is the name of the repository we're querying
This specifies that we're using the Prismic CDN, which is a special infrastructure to make queries faster
This is the Prismic domain
This is the Document API endpoint
The question mark marks the beginning of our query
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 predicates. Predicates are query filters.
Prismic has 29 predicates:
- 7 predicates for filtering logically
- 3 predicates for filtering numerically
- 17 predicates for filtering by date
- 1 predicate for filtering by similarity
- 1 predicate for filtering by location
The logical predicate 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 predicate:
Predicates 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 predicates and how to use them in the Predicate Reference.
You can test your predicates in Prismic's API Browser. To get to the API Browser, use the Entry API endpoint for your repository, but remove the .cdn segment, like so:
In the API Browser, you can construct queries with predicates. 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.
You can also add options to your API query. To learn more, see the Query Options 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 Entry 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:
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:
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:
But, be warned! GraphQL is an advanced technology, and the syntax can be quite confusing.
Third-party data and the Integration Fields 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 Fields API comes in.
Your developer can send data from a third-party service to the Integration Fields 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 does not have a write API to edit the content of a Prismic repository. This is a deliberate product decision. Write APIs introduce security and stability issues.
We believe that many of the features enabled by a write API — such as editing content models or integrating third-party data — could be better served by more specific features, and we've used that philosophy to guide our product development. That's why we have the Integration Fields Write API and the Custom Types API, as described above.
If you have a use case for a write API, we'd love to hear from you. Please drop us a line on the forum to tell us what you're thinking.
There is a way to programmatically edit resources on the Prismic server. That is with our Import/Export feature, which is not an API. Import/Export allows you to create and edit documents in bulk. This is very technical work, so we recommend that a developer take care of this.
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?
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.