How Do I Publish Content via the HTTP-API?

7 replies
Last updated: Nov 30, 2021
I have custom project with HTTP-api, that i am
writing
to Sanity Content Lake. My question how do i "publish" the content via HTTP-api?
AI Update

Publishing content via the HTTP API in Sanity is actually quite straightforward once you understand how the draft/published document system works!

In Sanity, draft and published documents are stored as two separate documents in your Content Lake. The key difference is their _id:

  • Draft documents have IDs prefixed with drafts. (e.g., drafts.post-123)
  • Published documents have no prefix (e.g., post-123)

How to "Publish" via HTTP API

To publish a document using the Mutation API, you essentially need to:

  1. Create/update the published version (without the drafts. prefix)
  2. Delete the draft version (if it exists)

Here's a practical example using createOrReplace:

POST https://YOUR-PROJECT-ID.api.sanity.io/v2025-02-19/data/mutate/YOUR-DATASET

{
  "mutations": [
    {
      "createOrReplace": {
        "_id": "post-123",
        "_type": "post",
        "title": "My Published Post",
        "content": "This is now live"
      }
    },
    {
      "delete": {
        "id": "drafts.post-123"
      }
    }
  ]
}

Creating Drafts First

If you want to create content as a draft initially (not published), simply use the drafts. prefix:

{
  "mutations": [
    {
      "create": {
        "_id": "drafts.post-123",
        "_type": "post",
        "title": "Draft Post"
      }
    }
  ]
}

Then when you're ready to publish, use the createOrReplace mutation shown above to copy the content to the published ID and delete the draft.

Important Notes

  • Validation: When using the HTTP API directly, server-side validation doesn't enforce Studio-defined schema rules, so you'll need to implement your own validation logic
  • Transactions: You can wrap multiple mutations in a transaction to ensure atomic operations
  • Querying: By default, queries only return published documents (without the drafts. prefix), which is what you want for production

The Mutation API supports several mutation types: create, createIfNotExists, createOrReplace, patch, and delete - all documented in the Mutation API docs.

What does your current script for writing to your Content Lake look like?
A simple post
request


        body: JSON.stringify({
          mutations: [
            {
              create: {
                _type: 'programs',
                title: title,
                description: description,
              },
            },
          ],
        })
 
If that what you mean with Script?
Yep! It looks like you're missing an
_id
field. You can use a package like
uuid
to generate a random id here. As long as you don't prefix the id with
drafts.
it'll show up as a published document in your dataset.
what is
drafts
?
Yep! It looks like you're missing an
_id
field. You can use a package like
uuid
to generate a random id here. As long as you don't prefix the id with
drafts.
it'll show up as a published document in your dataset.
Any
_id
that is prefixed with
drafts.
in your Studio will be a draft, not a published document. More on that here .
I see, thank you!

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?