> ## Documentation Index
> Fetch the complete documentation index at: https://www.pierview.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Sanity CMS Integration

> Automatically publish AI-generated content to your Sanity Content Lake

## Overview

The Sanity integration lets you automatically generate and publish SEO-optimized content directly to your Sanity project. Set up a content schedule, connect your Sanity dataset, and new articles will be created as documents in your Content Lake, ready to review and publish from Sanity Studio.

Content is converted from markdown to **Portable Text** so it renders correctly in rich-text fields.

## Step 1: Create a Sanity API Token

1. Open Sanity Studio and click your **project name** in the top-left corner. A dropdown will appear, click **Manage project**.

<Frame>
  <img src="https://mintcdn.com/pierview-dceb0b3c/sDCAOg7aIa1kItYN/images/sanity/sanity-manage-project.png?fit=max&auto=format&n=sDCAOg7aIa1kItYN&q=85&s=9dfcba7b1bd11b0b204d50893cdf061b" alt="Click the project name then Manage project" width="828" height="816" data-path="images/sanity/sanity-manage-project.png" />
</Frame>

2. In the management portal, click the **API** tab in the top navigation.

3. Select **Tokens** from the left sidebar, then click **Add API token**.

<Frame>
  <img src="https://mintcdn.com/pierview-dceb0b3c/sDCAOg7aIa1kItYN/images/sanity/sanity-api-token.png?fit=max&auto=format&n=sDCAOg7aIa1kItYN&q=85&s=614bed049d3ba9b3c199a1ba2519de3c" alt="API Tokens page with Add API token button" width="2606" height="1106" data-path="images/sanity/sanity-api-token.png" />
</Frame>

4. Give the token a name (e.g. `Pierview`), select **Editor** under Permissions, then click **Save**.

<Frame>
  <img src="https://mintcdn.com/pierview-dceb0b3c/9fpWeIAzELUP7uNp/images/sanity/sanity-editor-permission.png?fit=max&auto=format&n=9fpWeIAzELUP7uNp&q=85&s=0b36f2c33257616e785ca64e92eddce0" alt="Selecting Editor permission when creating a Sanity API token" width="1176" height="1296" data-path="images/sanity/sanity-editor-permission.png" />
</Frame>

<Warning>
  The token needs **Editor** (write) access to create documents. Viewer-only tokens will not work.
</Warning>

5. Copy the token, you'll paste it into Pierview shortly.

6. While you're here, click the **Datasets** tab to confirm which dataset you want to publish content to. The dataset name (e.g. `production`) is what you'll enter in Pierview.

<Frame>
  <img src="https://mintcdn.com/pierview-dceb0b3c/2dmQZtPRUCoLZgkk/images/sanity/sanity-datasets.png?fit=max&auto=format&n=2dmQZtPRUCoLZgkk&q=85&s=c243d6ebe6234e37003832b2577e822a" alt="Datasets tab showing production and testing datasets" width="2556" height="1214" data-path="images/sanity/sanity-datasets.png" />
</Frame>

7. Note your **Project ID** (shown at the top of the management page at [sanity.io/manage](https://www.sanity.io/manage)), you'll need it in the next step.

## Step 2: Connect Pierview to Sanity

The setup is a 3-step wizard: credentials → document type → field mapping.

**Credentials**

1. In Pierview, go to [Settings → Integrations → CMS Integrations](https://pierview.ai/dashboard/configuration/integrations) and click **Connect** next to Sanity.

<Frame>
  <img src="https://mintcdn.com/pierview-dceb0b3c/iiTwOo1MCLkL11Lr/images/pierview-main-cms-integrations.png?fit=max&auto=format&n=iiTwOo1MCLkL11Lr&q=85&s=9a9f161c95ae899b17734a44a069d888" alt="CMS Integrations page showing Framer and Sanity connect buttons" width="1539" height="806" data-path="images/pierview-main-cms-integrations.png" />
</Frame>

2. Enter your **Project ID**, **Dataset** (default: `production`), and paste in your **API Token**.

3. Click **Next**, Pierview will verify the credentials and fetch your document types.

**Document Type**

4. Choose the **document type** that content should be published as (e.g. `post`, `article`, `blogPost`). The dropdown is populated from your actual Sanity schema.

5. Click **Next**.

**Field Mapping**

6. Map the four fields to the corresponding field names in your chosen document type:

   | Pierview Field | What it sets                     | Required |
   | -------------- | -------------------------------- | -------- |
   | Title          | The document's title             | Yes      |
   | Content        | The article body (Portable Text) | Yes      |
   | Slug           | The URL slug                     | No       |
   | Date           | Publication date                 | No       |

7. Click **Save** to finish.

## You're Connected!

Any content scheduled for generation will automatically be created in your Sanity Content Lake as a **draft**, where you can review it in Sanity Studio and publish it when ready.

You can also publish content on demand, go to any generated article and click **Publish** in the top-right sidebar to send it to Sanity immediately.

<Info>
  Content is published as a draft by default. You'll need to publish it from Sanity Studio to make it live.
</Info>

## Troubleshooting

<AccordionGroup>
  <Accordion title="Articles not appearing in Sanity Studio">
    * Verify your Project ID and Dataset are correct
    * Check that your API token is still valid and hasn't been revoked in [sanity.io/manage](https://www.sanity.io/manage)
    * Make sure the integration is enabled (toggle is on in Pierview)
    * Check that the document type you selected exists in your Sanity schema
  </Accordion>

  <Accordion title="Permission denied / 403 error">
    Your API token may not have write access. Go to [sanity.io/manage](https://www.sanity.io/manage) → **API** → **Tokens** and create a new token with **Editor** permission, then update it in Pierview settings.
  </Accordion>

  <Accordion title="Content body is empty or missing formatting">
    Make sure the Content field in your Sanity schema is a **Portable Text** (array) field, not a plain string. Pierview converts markdown to Portable Text blocks, plain string fields will not display the formatted content correctly.
  </Accordion>

  <Accordion title="Document type not showing in the dropdown">
    The dropdown is populated from your Sanity project using a GROQ query. If your document type is missing, make sure at least one document of that type exists in the dataset, or that the type is defined and your API token has read access to the dataset.
  </Accordion>

  <Accordion title="Slug field not being set">
    Slug in Sanity is typically an object with a `current` field (e.g. `{ _type: "slug", current: "my-article" }`). Pierview handles this automatically, just make sure the field name in your mapping matches the field name in your schema exactly.
  </Accordion>
</AccordionGroup>
