Skip to content

How to patch Sanity draft documents when syncing from external API?

13repliesLast updated: Nov 13, 2026

Hi guys, I something that I'm struggling to understand, I think it's more of a conceptual thing with how Sanity treats drafts. I'm trying to sync documents from an external API, and it's working great using a Netlify function. In this case it's categories that come from a custom portal that the business uses, and will each sync over to sanity as documents whenever they are created / edited / deleted. I've got this working and it's super quick and great. However, I'm adding a few extra fields to each document within Sanity, for example a category description. I've noticed that if I have unpublished changes from within Sanity on a category, and I make changes in the external portal, it doesn't sync. I believe this is to do with the
_id
changing to
drafts.xxxxx
when there's a draft, so the patch I'm trying to run on the document doesn't do anything, as it's being patched to the regular
_id
, not the temporary draft
_id
. My question is, what's the best way to patch to a draft? Is it possible? I've looked high and low through the docs but I just can't see it mentioned. Below is the sync function for reference. Thanks very much for taking the time to read this.
AI Update

Yes, you can patch a draft. A draft is its own document with the ID drafts.<id>, and you patch it by that ID like any other document. The reliable way to sync is to check which of the published document and the draft exist, then patch every one that does, in a single transaction. Patch both because publishing writes the draft's contents onto the published document. If you update only the published document, an editor's next publish overwrites your synced values with the stale draft. If you update only the draft, the change isn't live until someone publishes.

Why patching blindly doesn't work

Sync function pattern

import {createClient} from '@sanity/client'

const client = createClient({
  projectId: 'YOUR_PROJECT_ID',
  dataset: 'YOUR_DATASET',
  apiVersion: '2025-12-15',
  useCdn: false,
  token: process.env.SANITY_API_WRITE_TOKEN, // keep server-side
})

export async function syncCategory(externalCategory) {
  const id = `category-${externalCategory.id}`
  const draftId = `drafts.${id}`

  // Only the fields owned by the external portal.
  // Fields edited in Studio (e.g. description) are left alone.
  const fields = {
    title: externalCategory.name,
    externalId: externalCategory.id,
  }

  // Which of the two documents exist right now?
  const existingIds = await client.fetch(
    '*[_id in [$id, $draftId]]._id',
    {id, draftId}
  )

  if (existingIds.length === 0) {
    // New category: create the published document
    await client.createIfNotExists({_id: id, _type: 'category', ...fields})
    return
  }

  // Patch every existing copy atomically
  const tx = existingIds.reduce(
    (t, docId) => t.patch(docId, (p) => p.set(fields)),
    client.transaction()
  )
  await tx.commit()
}

Notes:

Alternative: patch by query in one request

The Mutation API also accepts a GROQ query in place of an ID, and only patches the documents that match (mutation patterns):

const {dataset} = client.config()
await client.request({
  uri: `/data/mutate/${dataset}`,
  method: 'POST',
  body: {
    mutations: [
      {
        patch: {
          query: '*[_id in [$id, $draftId]]',
          params: {id, draftId},
          set: fields,
        },
      },
    ],
  },
})

Other things to know

Show original thread
13 replies

Was this answer helpful?

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Related contributions