Sanity Mutation icon

Sanity Mutation

Create, Read, Update, and Delete documents in Sanity.io

Create if Not Exists

AI-generated

Summary

Create a new document in Sanity.io only if a document with the specified ID does not already exist, using the given JSON data as the document content.

Inputs

  • Document ID — The ID to use for the new document. If left blank, a random ID will be generated. The operation only creates the document if one with this ID does not already exist.
  • Document Data (required) — The full JSON content of the document to create if it does not exist.
  • Options — Optional parameters including whether to return the full created document(s) in the response and which Sanity API version to use.

Output shape

a list of mutation result objects representing the created document(s) if creation occurred, otherwise an empty list with no document created

If the document already exists with the specified ID, no new document is created and the response reflects that. The 'Return Documents' option controls if full document data appears in the response. Errors on invalid JSON or authorization are thrown.

Examples

Example 1: Creating a new document with an explicit ID if it does not exist

Set 'Document ID' to the desired document ID, provide full JSON in 'Document Data', and enable 'Return Documents' to get the created document back.

Example 2: Creating a new document without specifying an ID

Leave 'Document ID' blank, provide the JSON in 'Document Data', and the node will generate a random ID if needed for creation.

Links

Discussion