How to create a draft document instead of published using Sanity client mutation?

7 replies
Last updated: Apr 30, 2020
Hi is there a way when using the client to create a new document via mutation, that the document is created as a draft instead of automatically published?
AI Update

Unfortunately, there isn't a built-in way to create a document directly as a draft when using the client's create() mutation. The Sanity client's create() method will always create a published document.

Important: While you might see references to prefixing document IDs with drafts., Sanity's official documentation explicitly advises against this: "We advise against using our APIs to create document IDs prefixed with drafts. or versions. as these are used internally. Such documents may react with platform functionality in unexpected ways."

Here are the recommended approaches instead:

Option 1: Create then immediately convert to draft (via Studio) Create the document as published, then have editors open it in Sanity Studio where it will automatically create a draft version when they make edits. This is the safest approach that works with Sanity's draft system as intended.

Option 2: Use a status field Add a status or published field to your schema to track whether content should be considered "live":

{
  name: 'post',
  type: 'document',
  fields: [
    {
      name: 'title',
      type: 'string'
    },
    {
      name: 'status',
      type: 'string',
      options: {
        list: ['draft', 'published']
      },
      initialValue: 'draft'
    }
  ]
}

Then filter your queries to only show published content:

*[_type == 'post' && status == 'published']

Option 3: Use a separate dataset For bulk imports or programmatic content creation that needs review, consider importing to a staging dataset first, then using Sanity's dataset copy functionality to move approved content to production.

The draft system in Sanity is really designed to work through the Studio interface, where it automatically manages the relationship between published documents and their draft counterparts. Trying to manually create drafts via the API goes against how the platform is designed to work and could cause unexpected issues.

Show original thread
7 replies
The goal is that the created document can be reviewed by someone before its published
Read through this, maybe you'll find some clues: https://www.sanity.io/docs/custom-workflows
Cool so what I did was set the
_id
property on the document in the
client.create
object to
drafts.
which prefixed the generated id with drafts. and that gave me the desired result. Is there any downside to this approach?
Hi Andre, as far as I know it’s a valid way of getting prefixed, unique IDs — draft IDs in this case. You’d use a similar approach with the mutations API. I guess you’ve confirmed that
drafts.
gets removed when you publish the relevant document? 🙂
P.S. Thanks Chris for suggesting this approach!
👍
user M
Yes and thanks for the follow up
I ran into a little snag with this, might be a bug with sanity not sure.. https://github.com/sanity-io/sanity/issues/1865
Hi Zane! You’re doing a great job reporting these, although I do wish the Studio would treat you a bit better 😉 Thanks for flagging this one. When you say ‘click from the dashboard’, are you referring to an intent link in a dashboard widget?

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.

Was this answer helpful?