Sanity
App · 9 actions
Query, create, update and publish content in Sanity, the headless CMS.
Actions
| Action | What it does |
|---|---|
| Create a document | Create a new document. |
| Create or replace a document | Create a document with this ID, or replace the whole document if it already exists. |
| Delete a document | Delete a document and its draft. |
| Get documents by ID | Get one or more documents by their IDs, always reading the latest write. |
| List datasets | List the project's datasets. |
| List recent documents of a type | List the newest documents of one type, most recently created first. |
| Publish a draft | Publish a document's draft, replacing the published version and removing the draft, as Publish does in Sanity Studio. |
| Run a GROQ query | Read content with a GROQ query, Sanity's query language, such as *[_type == "post"]{title, slug}. |
| Update fields of a document | Set 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
- Dataset
datasettext - The dataset to use. Leave blank for production, the dataset every project starts with.Default:
production. - Document type
document_typetextRequired - The document's type, as named in your Sanity schema (its _type), such as post.
- Fields
fieldsobject - The document's fields and their values, such as title: Hello. _id and _type are set from their own inputs.
- Document ID
document_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
- Dataset
datasettext - The dataset to use. Leave blank for production, the dataset every project starts with.Default:
production. - Document ID
document_idtextRequired - The document's ID. Start it with drafts. to write an unpublished draft.
- Document type
document_typetextRequired - The document's type, as named in your Sanity schema (its _type), such as post.
- Fields
fieldsobject - 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
- Dataset
datasettext - The dataset to use. Leave blank for production, the dataset every project starts with.Default:
production. - Document ID
document_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
- Dataset
datasettext - The dataset to use. Leave blank for production, the dataset every project starts with.Default:
production. - Document IDs
document_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
- Dataset
datasettext - The dataset to use. Leave blank for production, the dataset every project starts with.Default:
production. - Document type
document_typetextRequired - The type of document to list, as named in your Sanity schema (its _type), such as post.
- Limit
limitnumber - How many documents to return, up to 100. Default is 25.From 1 to 100.
- Created after
created_afterdate and time - Only list documents created after this date and time.
- Versions
perspectivetext - 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
- Dataset
datasettext - The dataset to use. Leave blank for production, the dataset every project starts with.Default:
production. - Document ID
document_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
- Dataset
datasettext - The dataset to use. Leave blank for production, the dataset every project starts with.Default:
production. - Query
querytextRequired - The GROQ query, such as *[_type == "post" && slug.current == $slug][0].
- Params
paramsobject - Values for the $names the query uses, by name without the $, such as slug: hello-world.
- Versions
perspectivetext - 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
- Dataset
datasettext - The dataset to use. Leave blank for production, the dataset every project starts with.Default:
production. - Document ID
document_idtextRequired - The ID of the document to update. Use drafts. in front of it to update the draft.
- Fields to set
setobject - 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 remove
unsetlist - Names of fields to remove, such as subtitle.
- Only if revision is
if_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"
}
}