Sanity

App · 9 actions

Query, create, update and publish content in Sanity, the headless CMS.

Actions

ActionWhat it does
Create a documentCreate a new document.
Create or replace a documentCreate a document with this ID, or replace the whole document if it already exists.
Delete a documentDelete a document and its draft.
Get documents by IDGet one or more documents by their IDs, always reading the latest write.
List datasetsList the project's datasets.
List recent documents of a typeList the newest documents of one type, most recently created first.
Publish a draftPublish a document's draft, replacing the published version and removing the draft, as Publish does in Sanity Studio.
Run a GROQ queryRead content with a GROQ query, Sanity's query language, such as *[_type == "post"]{title, slug}.
Update fields of a documentSet or remove fields of an existing document, leaving its other fields as they are.

Create a document

Create a new document. It fails if a document with the same ID already exists. Give an ID that starts with drafts. to create an unpublished draft.

Inputs

Datasetdatasettext
The dataset to use. Leave blank for production, the dataset every project starts with.Default: production.
Document typedocument_typetextRequired
The document's type, as named in your Sanity schema (its _type), such as post.
Fieldsfieldsobject
The document's fields and their values, such as title: Hello. _id and _type are set from their own inputs.
Document IDdocument_idtext
The new document's ID. Leave blank for Sanity to make one. An ID ending in a dot, such as post., is used as the start of a new unique ID. Letters, digits, dots, hyphens and underscores only.

Outputs

dataobject
The response from Sanity.
Example output
{
  "data": {
    "document": {
      "_createdAt": "example",
      "_id": "example",
      "_rev": "example",
      "_type": "example",
      "_updatedAt": "example"
    },
    "documentId": "example",
    "documentIds": [
      "example"
    ],
    "operation": "example",
    "results": [
      {
        "document": {
          "_createdAt": "example",
          "_id": "example",
          "_rev": "example",
          "_type": "example",
          "_updatedAt": "example"
        },
        "id": "example",
        "operation": "example"
      }
    ],
    "transactionId": "example"
  }
}

Create or replace a document

Create a document with this ID, or replace the whole document if it already exists. Fields left out of a replaced document are removed.

Inputs

Datasetdatasettext
The dataset to use. Leave blank for production, the dataset every project starts with.Default: production.
Document IDdocument_idtextRequired
The document's ID. Start it with drafts. to write an unpublished draft.
Document typedocument_typetextRequired
The document's type, as named in your Sanity schema (its _type), such as post.
Fieldsfieldsobject
The document's fields and their values, such as title: Hello. _id and _type are set from their own inputs.

Outputs

dataobject
The response from Sanity.
Example output
{
  "data": {
    "document": {
      "_createdAt": "example",
      "_id": "example",
      "_rev": "example",
      "_type": "example",
      "_updatedAt": "example"
    },
    "documentId": "example",
    "documentIds": [
      "example"
    ],
    "operation": "example",
    "results": [
      {
        "document": {
          "_createdAt": "example",
          "_id": "example",
          "_rev": "example",
          "_type": "example",
          "_updatedAt": "example"
        },
        "id": "example",
        "operation": "example"
      }
    ],
    "transactionId": "example"
  }
}

Delete a document

Delete a document and its draft. It succeeds even if there was nothing to delete.

Inputs

Datasetdatasettext
The dataset to use. Leave blank for production, the dataset every project starts with.Default: production.
Document IDdocument_idtextRequired
The ID of the document to delete, with or without drafts. in front: both versions are deleted.

Outputs

dataobject
The response from Sanity.
Example output
{
  "data": {
    "document": {
      "_createdAt": "example",
      "_id": "example",
      "_rev": "example",
      "_type": "example",
      "_updatedAt": "example"
    },
    "documentId": "example",
    "documentIds": [
      "example"
    ],
    "operation": "example",
    "results": [
      {
        "document": {
          "_createdAt": "example",
          "_id": "example",
          "_rev": "example",
          "_type": "example",
          "_updatedAt": "example"
        },
        "id": "example",
        "operation": "example"
      }
    ],
    "transactionId": "example"
  }
}

Get documents by ID

Get one or more documents by their IDs, always reading the latest write. To read a draft, use its ID with drafts. in front. IDs that don't exist, or that the token can't read, are listed in omitted.

Inputs

Datasetdatasettext
The dataset to use. Leave blank for production, the dataset every project starts with.Default: production.
Document IDsdocument_idslistRequired
The IDs of the documents to get.

Outputs

dataobject
The response from Sanity.
Example output
{
  "data": {
    "documents": [
      {
        "_createdAt": "example",
        "_id": "example",
        "_rev": "example",
        "_type": "example",
        "_updatedAt": "example"
      }
    ],
    "omitted": [
      {
        "id": "example",
        "reason": "existence"
      }
    ]
  }
}

List datasets

List the project's datasets.

Inputs

None.

Outputs

datalist
The response from Sanity.
Example output
{
  "data": [
    {
      "aclMode": "public",
      "addonFor": "example",
      "createdAt": "example",
      "datasetProfile": "example",
      "name": "example",
      "tags": [
        {}
      ]
    }
  ]
}

List recent documents of a type

List the newest documents of one type, most recently created first.

Inputs

Datasetdatasettext
The dataset to use. Leave blank for production, the dataset every project starts with.Default: production.
Document typedocument_typetextRequired
The type of document to list, as named in your Sanity schema (its _type), such as post.
Limitlimitnumber
How many documents to return, up to 100. Default is 25.From 1 to 100.
Created aftercreated_afterdate and time
Only list documents created after this date and time.
Versionsperspectivetext
Which version of each document to read. Published (the default) reads only published documents. Drafts reads unpublished changes in place of the published version, and includes documents that exist only as drafts. Raw reads every version, with drafts under IDs that start with drafts.One of: published, drafts, raw.

Outputs

dataobject
The response from Sanity.
Example output
{
  "data": {
    "ms": 1,
    "query": "example",
    "result": [
      {
        "_createdAt": "example",
        "_id": "example",
        "_rev": "example",
        "_type": "example",
        "_updatedAt": "example"
      }
    ],
    "syncTags": [
      "example"
    ]
  }
}

Publish a draft

Publish a document's draft, replacing the published version and removing the draft, as Publish does in Sanity Studio.

Inputs

Datasetdatasettext
The dataset to use. Leave blank for production, the dataset every project starts with.Default: production.
Document IDdocument_idtextRequired
The ID of the document to publish, with or without drafts. in front.

Outputs

dataobject
The response from Sanity.
Example output
{
  "data": {
    "transactionId": "example"
  }
}

Run a GROQ query

Read content with a GROQ query, Sanity's query language, such as *[_type == "post"]{title, slug}. The answer is in result.

Inputs

Datasetdatasettext
The dataset to use. Leave blank for production, the dataset every project starts with.Default: production.
QueryquerytextRequired
The GROQ query, such as *[_type == "post" && slug.current == $slug][0].
Paramsparamsobject
Values for the $names the query uses, by name without the $, such as slug: hello-world.
Versionsperspectivetext
Which version of each document to read. Published (the default) reads only published documents. Drafts reads unpublished changes in place of the published version, and includes documents that exist only as drafts. Raw reads every version, with drafts under IDs that start with drafts.One of: published, drafts, raw.

Outputs

dataobject
The response from Sanity.
Example output
{
  "data": {
    "ms": 1,
    "query": "example",
    "result": [
      {}
    ],
    "syncTags": [
      "example"
    ]
  }
}

Update fields of a document

Set or remove fields of an existing document, leaving its other fields as they are. Fields are set first, then removed.

Inputs

Datasetdatasettext
The dataset to use. Leave blank for production, the dataset every project starts with.Default: production.
Document IDdocument_idtextRequired
The ID of the document to update. Use drafts. in front of it to update the draft.
Fields to setsetobject
Fields to set and their new values, such as title: New title. A dotted name, such as author.name, sets a nested field.
Fields to removeunsetlist
Names of fields to remove, such as subtitle.
Only if revision isif_revision_idtext
Only update the document if its _rev is still this value, so a change made since it was read isn't overwritten.

Outputs

dataobject
The response from Sanity.
Example output
{
  "data": {
    "document": {
      "_createdAt": "example",
      "_id": "example",
      "_rev": "example",
      "_type": "example",
      "_updatedAt": "example"
    },
    "documentId": "example",
    "documentIds": [
      "example"
    ],
    "operation": "example",
    "results": [
      {
        "document": {
          "_createdAt": "example",
          "_id": "example",
          "_rev": "example",
          "_type": "example",
          "_updatedAt": "example"
        },
        "id": "example",
        "operation": "example"
      }
    ],
    "transactionId": "example"
  }
}

On this page