How to auto-publish documents created via Sanity client in Node.js?
I understand your frustration! The behavior you're experiencing is actually expected - when you use client.create() without specifying an _id, Sanity auto-generates one as a UUID, and this creates what Sanity considers a published document (no drafts. prefix in the ID).
Understanding Draft vs Published
The key thing to understand is that in Sanity, whether a document is "draft" or "published" is determined entirely by its _id:
- Published document: ID does NOT start with
drafts.(e.g.,abc-123) - Draft document: ID DOES start with
drafts.(e.g.,drafts.abc-123)
According to the Perspectives documentation and Published Documents documentation, when you create a document without the drafts. prefix, it's already published.
Why You Might Think They're Not Published
The confusion likely comes from how you're viewing the documents. Here's what might be happening:
- Studio Display: If you're checking in Sanity Studio, it might show indicators that make it look like documents need publishing, but if the
_iddoesn't havedrafts.prefix, they're already live - Query Perspective: Depending on how you're querying, you might be looking at the wrong perspective
- CDN Caching: With
useCdn: true, you might not see fresh data immediately
Verify Your Documents Are Actually Published
Run this check to see what's really in your dataset:
const sanityClient = require('@sanity/client')
const client = sanityClient({
projectId: 'your-project-id',
dataset: 'bikeshop',
apiVersion: '2021-06-24', // Use a current date
token: 'sanity-auth-token',
useCdn: false // Important for accurate results
})
// Check for published documents (no drafts. prefix)
const published = await client.fetch('*[_type == "yourType" && !(_id in path("drafts.**"))]')
console.log('Published documents:', published.map(d => d._id))
// Check for draft documents (with drafts. prefix)
const drafts = await client.fetch('*[_type == "yourType" && _id in path("drafts.**")]')
console.log('Draft documents:', drafts.map(d => d._id))Your Code Should Already Work
Your current approach should create published documents automatically:
const sanityClient = require('@sanity/client')
const client = sanityClient({
projectId: 'your-project-id',
dataset: 'bikeshop',
apiVersion: '2021-06-24', // Update to a current date
token: 'sanity-auth-token',
useCdn: false // Use false for write operations
})
// This creates a PUBLISHED document (auto-generated ID without drafts. prefix)
await client.create({
_type: 'yourDocumentType',
title: 'My Document',
// your other fields
})According to the Document ID documentation, when you don't specify an _id, Sanity auto-generates a UUID, and these are created as published documents by default.
Important Configuration Changes
Make these adjustments to your client setup:
- Set
useCdn: false- The CDN is only for reads, and using it for writes can cause confusion - Update your
apiVersion- Use a more recent date like'2021-06-24'or later - Verify token permissions - Ensure your token has write access to the dataset
If Documents Really Aren't Showing
If you run the verification query above and find your documents ARE published (no drafts. prefix) but still aren't appearing where you expect:
- Check your frontend queries are using the
publishedperspective - Make sure you're querying the correct dataset
- Verify your API version settings match between write and read operations
The documents created by client.create() without an explicit _id are already published and ready to use - no manual publishing step needed!
Show original thread10 replies
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.