Clarification on using the mutate API to draft posts in Sanity.io

6 replies
Last updated: May 28, 2020
hey could someone clarify whether the mutate api allows drafting (but not publishing) posts? https://www.sanity.io/docs/http-mutations#patch-2f480b2baca5
i've been getting 500 errors when testing out this api and im not entirely sure why.
AI Update

Yes, the Mutate API absolutely allows you to create drafts without publishing! You do this by prefixing the document _id with drafts.

Creating Drafts via the Mutate API

When you want to save a document as a draft (not published), simply add the drafts. prefix to the _id:

{
  "create": {
    "_id": "drafts.post-123",
    "_type": "post",
    "title": "Example Post"
  }
}

This creates a draft document that exists separately from any published version. When you later publish it from Sanity Studio, the draft merges with the published document.

About those 500 errors...

500 errors from the Mutate API can have several causes. Here are the most common culprits:

1. Document size limits

  • Maximum document size is 32 MB
  • Maximum mutation request body size is 4 MB

2. Rate limits

  • Maximum mutation rate: 25 requests/second per IP
  • Maximum concurrent mutations: 100 per dataset
  • If you exceed these, you'll get 429 errors, but sustained rate limit violations could cause other issues

3. Attribute limits

  • Free/Growth plans: max 1000 attributes per document
  • Enterprise: max 8000 attributes per document
  • Maximum nesting depth: 20 levels

4. Authentication issues

  • Make sure your token has write permissions
  • Verify your Authorization header is properly formatted

5. Validation or malformed data

  • Invalid field types
  • Missing required fields (like _type)
  • Circular references

Debugging tips

To troubleshoot your 500 errors:

  1. Check the response body - it should contain error details
  2. Verify your document structure - make sure it matches your schema
  3. Test with a minimal document - try creating the simplest possible draft first
  4. Check your token permissions - ensure it has write access to your dataset
  5. Review the HTTP Mutations documentation for the full API reference

The draft functionality itself is rock-solid and widely used, so the 500 errors are likely related to one of the limits or data issues above rather than the draft mechanism itself.

the only reason i can think of is maybe i am not sending all required fields in the document?
oh was able to get the post request working, but weird that
create
still goes through when all the required fields are not provided.
it'd be great if someone could clarify if i could "draft" only with this api! 🙂
for now i am just going to prefix the
_id
field with
drafts.
which seems to work pretty well! feel free to lmk if this is an anti-pattern or if there is a better way.
Hi Yihwan, you’re correct! Prefixing with
drafts.
is indeed the way to go and not considered an anti-pattern 🙂
Regarding the required fields - validation currently happens on the client side only, so any rules you define in your Studio will not be used for server side validation when using the HTTP API. You would have to set up additional validation for these yourself, unfortunately.

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?