# Sanity

<div align="center" data-full-width="true"><figure><img src="/files/Q6hPmqRgAfDimKAo3uRF" alt="" width="375"><figcaption><p>Example workflow listing resources</p></figcaption></figure></div>

## Authentication

1. **Generate an API Key**
   1. Go to your project Dashboard on Sanity.io
   2. Go to the API tab
   3. Click on the "Add API Token" button
   4. Set a name for your token (i.e. "AirOps")
   5. Set the permissions as "Editor"
   6. Click on "Save"
   7. Copy the generated token
2. **Add the API Key to AirOps**
   1. Go to AirOps > Settings > Integrations page
   2. Click on the "Add New" and select Sanity
   3. Paste the generated token in the API Key field
   4. Click on "Save"

**Security Note:** Treat your API Key like a password. It provides REST API access to your Sanity instance, and it should not be shared. Store it securely and only use it in server-side or secure/authenticated environments.

## Functions and Their Parameters <a href="#user-content-functions-and-their-parameters" id="user-content-functions-and-their-parameters"></a>

#### Common Configuration Parameters

1. **Project ID**: The ID of your Sanity project. (e.g. "w22x3y4z")
2. **Dataset**: The dataset you want to use. (e.g. "production")

### **List Resources**

Fetches a list of resources from your Sanity project. This is useful for retrieving existing posts, images, or other content types. Returns 100 records per page.

1. **Resource Type**: The type of resource you want to list. (e.g. "post")
2. **Last ID**: To use pagination, you can set the last ID of the previous request. (Optional)
3. **Include drafts?**: Whether to include draft resources in the results. (Optional)

### **Fetch Resource**

Fetches a single resource from your Sanity project. This is useful for retrieving detailed information about a specific post or image.

Array of blocks will be returned as is but a companion data structure will be returned with the blocks attributes converted to plain HTML within the `__airops.html` JSON path.

1. **Resource ID**: The ID of the resource you want to fetch. (e.g. "4ddb0459-2734-4611-b1af-4ee13f3d5630")

### **Create Resource**

Creates a new resource in your Sanity project. This is useful for adding new posts, images, or other content types.

1. **Resource JSON**: The JSON object representing the resource you want to create.
   * For instance: `{ "title": "My New Post", "category": "blog" }`
   * When used in conjunction with the Resource HTML input, HTML attributes will be ignored from this input.
   * See <https://www.sanity.io/docs/js-client#creating-documents>.
2. **Resource HTML**: A JSON object containing the resource attribute name as the key and the HTML document as value. (Optional)
   * For instance: `{ "body": "<h1>My New Post</h1><p>This is the content of my new post.</p>" }`
   * This is useful for creating posts with rich text content.
   * The HTML will be converted to Sanity's Portable Text structure and used in the payload to create the resource. Send as many HTML attributes as needed.

### **Update Resource**

Updates an existing resource in your Sanity project.

1. **Resource ID**: The ID of the resource you want to update. (e.g. "4ddb0459-2734-4611-b1af-4ee13f3d5630")
2. **Resource JSON**: The JSON object representing the resource you want to update.
   * For instance: `{ "title": "My Updated Post", "category": "blog" }`
   * When used in conjunction with the Resource HTML input, HTML attributes will be ignored from this input.
   * See <https://www.sanity.io/docs/js-client#patchupdate-a-document>.
3. **Resource HTML**: A JSON object containing the resource attribute name as the key and the HTML document as value. (Optional)
   * For instance: `{ "body": "<h1>My New Post</h1><p>This is the content of my new post.</p>" }`
   * This is useful for creating posts with rich text content.
   * The HTML will be converted to Sanity's Portable Text structure and used in the payload to create the resource. Send as many HTML attributes as needed.

### **Upload Image**

Uploads an image to your Sanity project. This is useful for adding images to your content or media library.

1. **Image URL**: The URL of the image you want to upload to Sanity. (e.g. "<https://example.com/image.jpg>")

### **Publish Resource**

Publishes a draft resource in your Sanity project, making it live and visible to your audience.

1. **Resource ID**: The ID of the draft resource you want to publish. (e.g. "drafts.4ddb0459-2734-4611-b1af-4ee13f3d5630")

### **Execute Query**

Executes an arbitrary [GROQ](https://www.sanity.io/docs/groq) query against your Sanity project. This is useful for fetching data with custom filtering, sorting, or projections that go beyond the built-in List Resources action.

1. **Query**: The GROQ query to execute against your dataset. (e.g. `*[_type == "post" && status == "published"] | order(publishedAt desc) { _id, title, slug, publishedAt }`)
2. **Query Params**: A JSON object of named parameters to pass into the query. (Optional)
   * For instance: `{ "status": "published" }` can be used with a query like `*[_type == "post" && status == $status]`
   * See <https://www.sanity.io/docs/query-cheat-sheet> for GROQ syntax reference.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.airops.com/context/overview/integrations/sanity.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
