capabilities.work

Upsert Document

A 3-step recipe with Sanity. We can help you get access.

Recipe · 3 steps

Upsert Document

with Sanity

Upsert a document keyed on a GROQ field match within a dataset.

  1. Search the dataset for a document of the given type whose key field equals the supplied value, returning the first match's id.
  2. Patch the matched document with the supplied fields, leaving other fields untouched.
  3. Create a new document of the given type carrying the key value and the supplied fields when no existing document matched.
What you'll need to have ready
  • projectId: The Sanity project id that scopes the API endpoint.
  • apiToken: Sanity project API token used as a Bearer credential.
  • dataset: Dataset name to upsert the document into.
  • documentType: The _type of the document being upserted.
  • keyField: Field name used to detect an existing document (e.g. slug).
  • keyValue: Value of the key field to match on.
  • fields: Map of field name/value pairs to write onto the document.
For your developer

Arazzo workflow sanity:upsert-document, 3 operations: queryDocumentsPost → mutateDocuments → mutateDocuments. Specs on APIs.io.