# Sanity

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

App · 9 actions. Web page: https://docs.lodol.com/docs/integrations/sanity

## Actions

### 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 (`dataset`, text): The dataset to use. Leave blank for production, the dataset every project starts with. Default: `production`.
- Document type (`document_type`, text, required): The document's type, as named in your Sanity schema (its _type), such as post.
- Fields (`fields`, object): The document's fields and their values, such as title: Hello. _id and _type are set from their own inputs.
- Document ID (`document_id`, text): 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**

- `data` (object): The response from Sanity.

**Example output**

```json
{
  "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 (`dataset`, text): The dataset to use. Leave blank for production, the dataset every project starts with. Default: `production`.
- Document ID (`document_id`, text, required): The document's ID. Start it with drafts. to write an unpublished draft.
- Document type (`document_type`, text, required): The document's type, as named in your Sanity schema (its _type), such as post.
- Fields (`fields`, object): The document's fields and their values, such as title: Hello. _id and _type are set from their own inputs.

**Outputs**

- `data` (object): The response from Sanity.

**Example output**

```json
{
  "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 (`dataset`, text): The dataset to use. Leave blank for production, the dataset every project starts with. Default: `production`.
- Document ID (`document_id`, text, required): The ID of the document to delete, with or without drafts. in front: both versions are deleted.

**Outputs**

- `data` (object): The response from Sanity.

**Example output**

```json
{
  "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 (`dataset`, text): The dataset to use. Leave blank for production, the dataset every project starts with. Default: `production`.
- Document IDs (`document_ids`, list, required): The IDs of the documents to get.

**Outputs**

- `data` (object): The response from Sanity.

**Example output**

```json
{
  "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**

- `data` (list): The response from Sanity.

**Example output**

```json
{
  "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 (`dataset`, text): The dataset to use. Leave blank for production, the dataset every project starts with. Default: `production`.
- Document type (`document_type`, text, required): The type of document to list, as named in your Sanity schema (its _type), such as post.
- Limit (`limit`, number): How many documents to return, up to 100. Default is 25. From 1 to 100.
- Created after (`created_after`, date and time): Only list documents created after this date and time.
- Versions (`perspective`, text): 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**

- `data` (object): The response from Sanity.

**Example output**

```json
{
  "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 (`dataset`, text): The dataset to use. Leave blank for production, the dataset every project starts with. Default: `production`.
- Document ID (`document_id`, text, required): The ID of the document to publish, with or without drafts. in front.

**Outputs**

- `data` (object): The response from Sanity.

**Example output**

```json
{
  "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 (`dataset`, text): The dataset to use. Leave blank for production, the dataset every project starts with. Default: `production`.
- Query (`query`, text, required): The GROQ query, such as *[_type == "post" && slug.current == $slug][0].
- Params (`params`, object): Values for the $names the query uses, by name without the $, such as slug: hello-world.
- Versions (`perspective`, text): 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**

- `data` (object): The response from Sanity.

**Example output**

```json
{
  "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 (`dataset`, text): The dataset to use. Leave blank for production, the dataset every project starts with. Default: `production`.
- Document ID (`document_id`, text, required): The ID of the document to update. Use drafts. in front of it to update the draft.
- Fields to set (`set`, object): 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 (`unset`, list): Names of fields to remove, such as subtitle.
- Only if revision is (`if_revision_id`, text): Only update the document if its _rev is still this value, so a change made since it was read isn't overwritten.

**Outputs**

- `data` (object): The response from Sanity.

**Example output**

```json
{
  "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"
  }
}
```
