> For AI agents: the complete Sanity documentation index is available at [https://www.sanity.io/docs/llms.txt](https://www.sanity.io/docs/llms.txt).

# Create and query an embeddings index (deprecated)

To get started with the Embeddings Index API, you first need to create an index.

> [!TIP]
> Deprecation notice
> The **Embeddings Index API** is deprecated and will be sunset in a future release. We recommend migrating to the new **Embeddings** feature, now natively available within Sanity datasets.
> The new Embeddings feature offers a more integrated experience with improved performance and full support going forward. No new features or fixes will be made to this package.
> **Migrate today:** [Dataset Embeddings documentation](https://www.sanity.io/docs/content-lake/dataset-embeddings)
> If you have questions or need migration support, please open a discussion or reach out in the [Sanity Community](https://snty.link/community).

You can create an embeddings index in one of the following ways:

- With the [Embeddings Index CLI](https://www.npmjs.com/package/@sanity/embeddings-index-cli).
- With the [Embeddings Index UI](https://www.npmjs.com/package/@sanity/embeddings-index-ui) for Sanity Studio.
- With the [Embeddings Index HTTP API](https://www.sanity.io/docs/http-reference/embeddings-index).

This guide walks you through configuring an embeddings index for a Sanity project using the Embeddings Index CLI.

## Prerequisites

- The Sanity CLI. The CLI ships with the [main Sanity package](https://www.npmjs.com/package/sanity). The Embeddings Index CLI runs its commands through the Sanity CLI and authenticates with your Sanity login. Run `sanity login` before you start, or set a `SANITY_EMBEDDINGS_TOKEN` environment variable.
- Run the commands from the root directory of a Sanity project that contains a `sanity.cli.ts` or `sanity.cli.js` file.
- A user or token with permission to create webhooks, create tokens, and read datasets. The `create` command uses these permissions to set up a robot token and a webhook that keeps the index in sync with the dataset.

> [!WARNING]
> Gotcha
> The Embeddings Index API doesn't support dataset aliases. This means that you have to use the **real dataset name** in all requests.

## Create an embeddings index

To create an embeddings index, open a terminal session, and then run the command that matches how you plan to create an index:

**npm**

```shell
# Create an embeddings index by passing arguments
npx @sanity/embeddings-index-cli create --indexName "INDEX_NAME" --dataset "DATASET_NAME" --filter "GROQ_FILTER" --projection "GROQ_PROJECTION"

# Alternatively, create an embeddings index by passing a JSON manifest
npx @sanity/embeddings-index-cli create --manifest MANIFEST_FILE_NAME.json
```

**pnpm**

```shell
# Create an embeddings index by passing arguments
pnpm dlx @sanity/embeddings-index-cli create --indexName "INDEX_NAME" --dataset "DATASET_NAME" --filter "GROQ_FILTER" --projection "GROQ_PROJECTION"

# Alternatively, create an embeddings index by passing a JSON manifest
pnpm dlx @sanity/embeddings-index-cli create --manifest MANIFEST_FILE_NAME.json
```

**yarn**

```shell
# Create an embeddings index by passing arguments
yarn dlx @sanity/embeddings-index-cli create --indexName "INDEX_NAME" --dataset "DATASET_NAME" --filter "GROQ_FILTER" --projection "GROQ_PROJECTION"

# Alternatively, create an embeddings index by passing a JSON manifest
yarn dlx @sanity/embeddings-index-cli create --manifest MANIFEST_FILE_NAME.json
```

**bun**

```shell
# Create an embeddings index by passing arguments
bunx @sanity/embeddings-index-cli create --indexName "INDEX_NAME" --dataset "DATASET_NAME" --filter "GROQ_FILTER" --projection "GROQ_PROJECTION"

# Alternatively, create an embeddings index by passing a JSON manifest
bunx @sanity/embeddings-index-cli create --manifest MANIFEST_FILE_NAME.json
```

Creating an index can take time, depending on the number of existing documents and the indexer load.

> [!TIP]
> The commands in this guide use `npx` to run the CLI, but you can also install it in your project as a development dependency and use the `embeddings-index` command as shown in the [CLI readme](https://www.npmjs.com/package/@sanity/embeddings-index-cli).

You can define the configuration of an embeddings index in one of the following ways:

- By passing configuration arguments when you create the index in the CLI.
- By storing the configuration details in a JSON manifest file.

### Define the index in the CLI

To define a new embeddings index in the root directory of a Sanity project, pass the following arguments with the `embeddings-index create` command:

- `--indexName`: Assign a descriptive name to the index.
- `--dataset`: Specify the name of an existing dataset. This is the target dataset to index. If you omit it, the CLI uses the dataset in your `sanity.cli.ts` or `sanity.cli.js` file.
- `--filter`: Specify the filtering criteria to include in the index only the selected subset of documents from the dataset.
The filter must be a valid [GROQ filter](https://www.sanity.io/docs/content-lake/how-queries-work) *without the square brackets* that wrap the filter in a full query such as `*[_type=='tutorial']`.
Example: `_type=='tutorial'`
- `--projection`: Specify the projection criteria to include in the index only the selected subset of properties from the filtered documents.
The projection must be a valid [GROQ projection](https://www.sanity.io/docs/content-lake/query-cheat-sheet), including curly brackets.
Example: `{title, author}`

Alternatively, you can create an embeddings index by passing a JSON manifest file with the `--manifest` argument:

- `--manifest MANIFEST_FILE_NAME.json`

**Example**

**npm**

```shell
# Create embeddings index with arguments
# 'filter' has no '[]' square brackets
# 'projection' keeps '{}' curly brackets
npx @sanity/embeddings-index-cli create --indexName "my-embeddings-index" --dataset "production" --filter "_type=='myDocumentType'" --projection "{...}"

# Create embeddings index with JSON manifest
# The JSON manifest is in the project root directory
npx @sanity/embeddings-index-cli create --manifest embeddings-index-manifest.json
```

**pnpm**

```shell
# Create embeddings index with arguments
# 'filter' has no '[]' square brackets
# 'projection' keeps '{}' curly brackets
pnpm dlx @sanity/embeddings-index-cli create --indexName "my-embeddings-index" --dataset "production" --filter "_type=='myDocumentType'" --projection "{...}"

# Create embeddings index with JSON manifest
# The JSON manifest is in the project root directory
pnpm dlx @sanity/embeddings-index-cli create --manifest embeddings-index-manifest.json
```

**yarn**

```shell
# Create embeddings index with arguments
# 'filter' has no '[]' square brackets
# 'projection' keeps '{}' curly brackets
yarn dlx @sanity/embeddings-index-cli create --indexName "my-embeddings-index" --dataset "production" --filter "_type=='myDocumentType'" --projection "{...}"

# Create embeddings index with JSON manifest
# The JSON manifest is in the project root directory
yarn dlx @sanity/embeddings-index-cli create --manifest embeddings-index-manifest.json
```

**bun**

```shell
# Create embeddings index with arguments
# 'filter' has no '[]' square brackets
# 'projection' keeps '{}' curly brackets
bunx @sanity/embeddings-index-cli create --indexName "my-embeddings-index" --dataset "production" --filter "_type=='myDocumentType'" --projection "{...}"

# Create embeddings index with JSON manifest
# The JSON manifest is in the project root directory
bunx @sanity/embeddings-index-cli create --manifest embeddings-index-manifest.json
```

### Define the index in a JSON manifest

To store, reuse, and manage embeddings indexes with source code control and versioning, define their configuration in a JSON manifest file. Save the manifest file, for example `manifest.json`, in the root directory of your Sanity project.

A JSON manifest file defining an embeddings index must contain the following required fields:

```typescript
{
  indexName: string,
  dataset: string,
  filter: string,
  projection: string
}
```

**Example**

```json
{
  "indexName": "my-embeddings-index",
  "dataset": "production",
  "filter": "_type=='myType'",
  "projection": "{...}"
}
```

The manifest must be valid JSON, so it can't contain comments. The `filter` value has no surrounding square brackets, and the `projection` value keeps its curly brackets.

To create a JSON manifest file, invoke the [manifest command](https://www.sanity.io/docs/libraries/embeddings-index-cli-reference):

**npm**

```shell
npx @sanity/embeddings-index-cli manifest --out manifest.json --indexName "INDEX_NAME" --dataset "DATASET_NAME" --filter "GROQ_FILTER" --projection "GROQ_PROJECTION"
```

**pnpm**

```shell
pnpm dlx @sanity/embeddings-index-cli manifest --out manifest.json --indexName "INDEX_NAME" --dataset "DATASET_NAME" --filter "GROQ_FILTER" --projection "GROQ_PROJECTION"
```

**yarn**

```shell
yarn dlx @sanity/embeddings-index-cli manifest --out manifest.json --indexName "INDEX_NAME" --dataset "DATASET_NAME" --filter "GROQ_FILTER" --projection "GROQ_PROJECTION"
```

**bun**

```shell
bunx @sanity/embeddings-index-cli manifest --out manifest.json --indexName "INDEX_NAME" --dataset "DATASET_NAME" --filter "GROQ_FILTER" --projection "GROQ_PROJECTION"
```

To change the configuration of an existing index, run the `delete` command, and then run the `create` command again.

### Check the status of an embeddings index

You can check the status of your embeddings indexes to monitor the creation progress or the completeness of the indexes.

To check the status of all embeddings indexes in a dataset, run the following command. Add `--dataset` to target a dataset other than the one in your Sanity CLI configuration:

**npm**

```shell
npx @sanity/embeddings-index-cli list
```

**pnpm**

```shell
pnpm dlx @sanity/embeddings-index-cli list
```

**yarn**

```shell
yarn dlx @sanity/embeddings-index-cli list
```

**bun**

```shell
bunx @sanity/embeddings-index-cli list
```

To check the status of a specific embeddings index in a Sanity project, run:

**npm**

```shell
npx @sanity/embeddings-index-cli get --indexName "INDEX_NAME"
```

**pnpm**

```shell
pnpm dlx @sanity/embeddings-index-cli get --indexName "INDEX_NAME"
```

**yarn**

```shell
yarn dlx @sanity/embeddings-index-cli get --indexName "INDEX_NAME"
```

**bun**

```shell
bunx @sanity/embeddings-index-cli get --indexName "INDEX_NAME"
```

## Query an index

To query an index, make a request with the [Embeddings Index HTTP API](https://www.sanity.io/docs/http-reference/embeddings-index). 

**JS client**

```javascript
import { createClient } from '@sanity/client'
const client = createClient({
  projectId: 'YOUR_PROJECT_ID',
  dataset: 'DATASET_NAME',
  apiVersion: 'vX', // vX is required for embeddings API calls
  token: process.env.SANITY_API_TOKEN,
  useCdn: false,
});
const dataset = 'DATASET_NAME'
const indexName = 'INDEX_NAME'

const response = await client.request({
  url: `/embeddings-index/query/${dataset}/${indexName}`,
  method: 'POST',
  body: {
    query: 'sci-fi adventure with cowboys and aliens',
    maxResults: 10,
    filter: {
      type: ['summary', 'synopsis', 'userReview'],
    },
  },
})
```

**CURL**

```sh
curl --request POST 'https://YOUR_PROJECT_ID.api.sanity.io/vX/embeddings-index/query/DATASET_NAME/INDEX_NAME' \
     --header 'Authorization: Bearer API_TOKEN' \
     --header 'Content-Type: application/json' \
     --header 'Accept: application/json' \
     --data '{
                "query": "sci-fi adventure with cowboys and aliens",
                "maxResults": 10,
                "filter": {
                  "type": ["summary", "synopsis", "userReview"]
                }
             }'
```

This example uses the query endpoint to search against an index and filter by document type. [Learn more about querying the API](https://www.sanity.io/docs/http-reference/embeddings-index).

## Next steps

- To move off the deprecated Embeddings Index API, see [Dataset Embeddings](https://www.sanity.io/docs/content-lake/dataset-embeddings).
- For every endpoint and command option, see the [Embeddings Index API reference](https://www.sanity.io/docs/http-reference/embeddings-index) and the [Embeddings Index CLI reference](https://www.sanity.io/docs/libraries/embeddings-index-cli-reference).

