
Language 🇺🇸 English
Sanity Installation
Add Live Commenting to Sanity Sites
This guide adds the FastComments live commenting widget to a site whose content is managed in Sanity. Sanity stores content and serves it through its APIs but does not render the public site, so the widget is added to the frontend application that renders Sanity documents. Each comment thread is identified by the document's _id, so the thread stays attached to the document when its slug or URL changes.
Sanity does not provide comments for site visitors. Sanity Studio includes a Comments feature, available on the Growth plan, which lets Studio users comment on fields and text while editing content. These comments are for content teams and are not displayed on the public site.
This tutorial requires a FastComments account. You can create an account here.
Step 1: Query the Document's _id 
Every Sanity document has an _id, and the ID cannot be modified after the document is created. Using the _id as the FastComments thread ID keeps comments attached to the document when its slug, title, or URL changes.
The frontend usually loads a document by its slug. The examples in this guide use a document type named post with a slug field. Replace them with the names of your own document type and field.
Create a client with @sanity/client, for example in sanity/client.ts. Replace the project ID and dataset with the values of your Sanity project:

Query the document by slug and select its _id with GROQ:

Drafts and the Published _id
Sanity stores an unpublished draft as a separate document whose ID is the published ID with the drafts. prefix, for example drafts.ecfef291-60f0-4609-bbfc-263d11a48c43. The published document's _id has no prefix. The frontend must use the published _id so that the draft and the published page show the same thread.
- With API version
2025-02-19and later, queries use thepublishedperspective by default and return only published documents, so_idhas no prefix. - With the
draftsperspective, used for previews,_idis the published ID and the draft ID is in_originalId. - With the
rawperspective, which is the default for API versions before2025-02-19, an authenticated query can return draft documents alongside published ones. Setperspective: 'published'increateClient, or remove thedrafts.prefix from_idbefore passing it to FastComments.
Step 2: Add the Widget in Next.js 
This step uses the Next.js App Router and the FastComments Next.js library, fastcomments-nextjs, which supports Next.js 13 to 16 with React 18 or 19. Install the library and the Sanity client in the Next.js project:

Render the Widget on the Post Page
The page is a Server Component. It queries the post from Sanity by slug and passes the post's _id to the comment widget as urlId. Create app/posts/[slug]/page.tsx, using the client from Step 1:

If you are logged in to FastComments, the snippet above contains your tenant ID. Otherwise, replace demo with your tenant ID.
The example imports the client from sanity/client.ts in the project root. Adjust the import path to the location of your client file. Replace https://example.com/posts/ with the public URL of your post pages.
urlIdidentifies the comment thread. It is set to the post's_id.urlis the link to the page that FastComments uses in notifications.keymakes React mount a new widget when the reader navigates from one post to another.- The
FastCommentscomponent declares'use client'itself, so it can be rendered from a Server Component without a wrapper.
In Next.js 14 and earlier, params is a plain object instead of a Promise, and await on it returns the same object.
EU Accounts
If your FastComments account is in the EU region, add region="eu" to the FastComments component.
Add the Widget Without a Framework Library 
Frontends that do not use React, such as Astro, Nuxt, or server-rendered templates, can load the widget with the FastComments script. The template that renders the post writes the document's _id into a data-document-id attribute, and the script passes it to the widget as urlId:

If you are logged in to FastComments, the snippet above contains your tenant ID. Otherwise, replace demo with your tenant ID.
Replace SANITY_DOCUMENT_ID with the template expression for the published _id of the document. For example, in an Astro component the attribute is written as data-document-id={post._id}, and in a Vue template as :data-document-id="post._id".
If your FastComments account is in the EU region, load the script from https://cdn-eu.fastcomments.com/js/embed-v2.min.js and add region: 'eu' to the object passed to window.FastCommentsUI.
FastComments also provides component libraries for other frameworks, including Astro, Vue, and Svelte. With these libraries, pass the _id as the urlId option of the comment widget.
Enable or Disable Comments per Document 
A boolean field on the document type lets editors turn comments off for individual documents. The frontend reads the field and renders the widget only when comments are enabled.
Add the Field to the Schema
Add a commentsEnabled field to the document type's schema in your Studio project:

initialValue sets the field to true on documents created in Studio after the field is added. Existing documents do not contain the commentsEnabled key until an editor sets the field, and GROQ returns null for the missing key.
Check the Field in the Frontend
Add the field to the GROQ projection:

In the Next.js page from Step 2, render the FastComments component only when the field is not false:

If you are logged in to FastComments, the snippet above contains your tenant ID. Otherwise, replace demo with your tenant ID.
The check uses !== false so that documents without a value for the field still show comments. To hide comments on documents without a value instead, use post.commentsEnabled === true.
Hiding the widget does not delete the document's existing comments. They are shown again when the field is set back to true.
Domain Errors 
The widget may show an authorization error like the following:
This error means the domain of the page is not in the list of domains allowed for your FastComments account.
Add the domain of the frontend that renders your Sanity content on the domain configuration page of your account. The domain where Sanity Studio is hosted is not needed, because the widget runs on the frontend pages.
Customization 
Styling and configuration options for the widget are described in the Customization Documentation.
With the Next.js library, configuration options are passed as props of the FastComments component. With the script embed, they are added to the object passed to window.FastCommentsUI.