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.
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.
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:
The Repository API endpoint is located at:
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:
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:
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?
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.
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:
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:
[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:
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.
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.
To learn more about orderings and other options you can add to your API query, check out the Rest 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.
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:
https://your-repo-name.cdn.prismic.io/api/tags
This endpoint only returns a list of tags.
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:
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:
https://your-repo-name.prismic.io/graphql
But, be warned! GraphQL is an advanced technology, and the syntax can be quite confusing.
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.
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.
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 field 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.
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.