Skip to main content

Sanity

Read and write a Sanity dataset - query documents with GROQ, create and patch them as drafts, then publish, unpublish or discard.

Connect a Sanity project with an API token to work its content lake end to end: query anything with GROQ, fetch a document in either its draft or published form, create and patch documents, and control what is live with publish, unpublish and discard. Writes land on the draft by default, so nothing the site serves changes until an explicit publish.

Connect

Credential fieldRequiredWhere it comes from
API token (API_TOKEN)YesA project token from sanity.io/manage → API → Tokens. Viewer to read, Editor to write; its role is fixed at creation and it reaches only its own project.
SettingRequiredWhat it is
Project ID (PROJECT_ID)YesThe project's id, shown on its page at sanity.io/manage. The token is scoped to this project.
Dataset (DATASET)YesThe dataset every tool acts on, usually 'production'. Fixed per connection on purpose - a write landing in the wrong dataset is not something anything surfaces.
API version (API_VERSION)NoSanity pins behaviour to a dated version. Defaults to 2024-10-01 when left empty; an unpinned client is a silent breaking-change surface.

Tools

ToolAccessWhat it does
Query documents (GROQ) (yekar.sanity.query)ReadRun a GROQ query against the connection's dataset and return the result. This is the general read: filter by type, follow references with ->, project fields, order and slice. Reads published documents AND drafts (drafts.<id>) - add a filter if you want only one.
Get document (yekar.sanity.get-document)ReadFetch one document by id. A published document and its draft are two separate documents in Sanity ('drafts.<id>'), so this says which one it returned rather than leaving it to be assumed.
List datasets (yekar.sanity.list-datasets)ReadList the project's datasets, marking the one this connection uses. Informational: every other tool acts on the connected dataset and cannot be pointed at another - connect a second time to reach a different one.
Create document (yekar.sanity.create-document)WriteCreate a document in the connection's dataset. Writes land on the DRAFT (drafts.<id>) by default - nothing the site serves changes until Publish document promotes it. Creating ACCUMULATES: without an explicit id, a retry makes a second document - pass id + ifNotExists when a repeat must be harmless.
Patch document (yekar.sanity.patch-document)WriteSet or unset fields on an existing document. Writes land on the DRAFT (drafts.<id>) by default - nothing the site serves changes until Publish document promotes it. Patching the DRAFT of a document that has never had one fails - read it first and create the draft (Create document with the published id) if there is none.
Delete document (yekar.sanity.delete-document)WriteDelete a document. Deleting the published document REMOVES IT FROM THE SITE at the next build, and there is no undo through this tool - Sanity keeps history for the retention your plan allows, recoverable only from the Studio. A reference from another document to a deleted one is left dangling.
Publish document (yekar.sanity.publish-document)WritePromote a document's draft to the published document. This is what makes an edit LIVE: the draft is consumed, and the site shows the new content at its next build. Publishing a document with no draft is an error, not a no-op.
Unpublish document (yekar.sanity.unpublish-document)WriteTake a document off the site while keeping its content: the published document is removed and its content becomes a draft. This is the reversible opposite of publishing - unlike Delete document, nothing is lost.
Discard draft (yekar.sanity.discard-draft)WriteThrow away a document's unpublished draft, leaving the published document as it is. The draft's edits are gone - this is the 'abandon these changes' verb, not a way to remove the live document (that is Unpublish document).

Notes

  • In Sanity open sanity.io/manage → your project → API → Tokens, create a token, and paste it here.
  • Choose the role at creation: Viewer can only read, Editor can read and write. A token's role CANNOT be widened afterwards - a broader one means creating a new token.
  • Set the project id and dataset on this connection. The dataset is fixed per connection (usually 'production'); connect a second time to reach another one, so a write cannot land in the wrong dataset by mistake.
  • A draft and its published document are SEPARATE documents ('drafts.<id>' and '<id>'). Writes go to the draft unless told otherwise, and Publish document is what makes an edit live.
  • If the site is a static build, publishing changes the dataset immediately but the site only shows it at its next build.