> 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).

# Testing functions locally

Simulate Functions locally with real data and an interactive playground.

Developing functions requires a different mindset than developing your studio or front-end. They react to changes in your data. To avoid making a change, deploying, changing a document, and checking the logs of a function, you can take advantage of the included local testing tools to simulate how your functions will interact with your data.

Prerequisites:

- Complete the [Functions quick start](https://www.sanity.io/docs/functions/function-quickstart), or be comfortable writing and deploying a Sanity Function.
- The latest version of the `sanity` CLI (`sanity@latest`) is recommended to interact with Blueprints and Functions as shown in this guide. You can always run the latest CLI commands with `npx sanity@latest`. The commands in this guide require `sanity` v4.9.0 or later.
- Run the commands in this guide from the directory containing your blueprints configuration. If you're not already logged in, the CLI prompts you to do so.

> [!NOTE]
> Local testing applies your function's filter and projection in the same way as a deployed function. If the data you supply doesn't match the filter, the function isn't invoked.

There are two approaches to running Sanity Functions locally. The `dev` command launches the development playground—a visual interface where you can run functions, edit the test data, and view the output. The `test` command offers a more traditional CLI experience where you can pass settings and test data directly as you invoke functions.

## Development playground

The Functions CLI includes an interactive playground where you can test your functions and see results prior to deploying them to Sanity's infrastructure.

Run the following command from your blueprint directory:

**npm**

```shell
npx sanity@latest functions dev
```

**pnpm**

```shell
pnpm dlx sanity@latest functions dev
```

**yarn**

```shell
yarn dlx sanity@latest functions dev
```

**bun**

```shell
bunx sanity@latest functions dev
```

This starts a local server running at `http://localhost:8080`. You can also specify a port with the `--port` flag. 

### Parts of the playground interface

![Screenshot of the Functions development playground with seven numbered callouts: (1) the Project, Dataset, Event, and API Version selectors under Client Options; (2) the Functions list; (3) the With Token toggle; (4) the Document ID field and Fetch Document button; (5) the document editor panel, here showing Before Document for an update event; (6) the Console with the Preserve Log toggle; (7) the Run button.](https://cdn.sanity.io/images/3do82whm/next/aa3a190578f91da76db32d2faca062fe705d7939-2784x1758.png)

1. Select which project, dataset, event type, and API version you want to use to test the function.
2. Select the function you want to test.
3. Toggle **With Token** to supply a `token` to the function handler's `context.clientOptions` object. A token is omitted from local testing unless this is toggled.
4. Optionally, pass a valid document ID and select the **Fetch Document** button to fetch the document and populate the sample document field with real data.
5. You can edit the **Document** panel manually, or use the **Fetch Document** button (4). The input expects a document-like shape. When you select the *update* event, the **Document** panel is replaced by **Before Document** and **After Document** panels.
6. Output from the function is displayed in the console. You can toggle the **Preserve Log** option to keep logs between runs.
7. Run the function locally with the **Run** button.

You can stop the playground at any time by returning to your terminal and pressing Control+C.

## Test functions from the CLI

If you prefer a CLI-based testing interface, you can run the following command:

**npm**

```shell
npx sanity@latest functions test FUNCTION_NAME
```

**pnpm**

```shell
pnpm dlx sanity@latest functions test FUNCTION_NAME
```

**yarn**

```shell
yarn dlx sanity@latest functions test FUNCTION_NAME
```

**bun**

```shell
bunx sanity@latest functions test FUNCTION_NAME
```

Replace `FUNCTION_NAME` with the `name` of a function defined in your blueprint. Output from your functions logs to the console.

### Common CLI flags

The CLI includes flags to help you supply data and configuration settings to your functions. The following are some common flags in use. To see a full list, see the [functions CLI reference documentation](https://www.sanity.io/docs/cli-reference/functions) or run `npx sanity functions test --help`.

#### Supply data from a document in your dataset

Use the `--dataset`, `--project-id`, and `--document-id` flags to fetch real documents to use as source data for your function.

**npm**

```shell
npx sanity@latest functions test log-event --document-id 52df8926-1afe-413b-bd23-e9efbc32cea3 --project-id YOUR_PROJECT_ID --dataset production
```

**pnpm**

```shell
pnpm dlx sanity@latest functions test log-event --document-id 52df8926-1afe-413b-bd23-e9efbc32cea3 --project-id YOUR_PROJECT_ID --dataset production
```

**yarn**

```shell
yarn dlx sanity@latest functions test log-event --document-id 52df8926-1afe-413b-bd23-e9efbc32cea3 --project-id YOUR_PROJECT_ID --dataset production
```

**bun**

```shell
bunx sanity@latest functions test log-event --document-id 52df8926-1afe-413b-bd23-e9efbc32cea3 --project-id YOUR_PROJECT_ID --dataset production
```

Replace `YOUR_PROJECT_ID` with your project ID. If you omit `--project-id`, the CLI uses the project ID stored in your blueprint configuration.

#### Simulate event triggers

Sometimes you have logic in your function that reacts to different events, like `delta::operation`. Use the `--event` flag to simulate different event types. The default is `create`.

**npm**

```shell
npx sanity@latest functions test log-event --event delete
```

**pnpm**

```shell
pnpm dlx sanity@latest functions test log-event --event delete
```

**yarn**

```shell
yarn dlx sanity@latest functions test log-event --event delete
```

**bun**

```shell
bunx sanity@latest functions test log-event --event delete
```

#### Supply data from a file or string

Use `--file` to read a JSON file, or `--data` to pass a JSON string. The two flags can't be used together.

**JSON file**

```sh
npx sanity@latest functions test log-event --file sample-document.json
```

**Data string**

```sh
npx sanity@latest functions test log-event --data '{ "_type": "post", "_id": "123456", "content": "test content" }'
```

#### Supply before and after documents for update events

When you use `--event update`, you must supply a before and an after document with one of these flag pairs: `--document-id-before` and `--document-id-after`, `--data-before` and `--data-after`, or `--file-before` and `--file-after`. These flags supply the changes between two documents for delta GROQ functions. Without a pair, the command exits with `When using --event=update, you must provide one of the following flag pairs:`.

**npm**

```shell
npx sanity@latest functions test log-event --event update --document-id-before BEFORE_DOCUMENT_ID --document-id-after AFTER_DOCUMENT_ID --dataset production
```

**pnpm**

```shell
pnpm dlx sanity@latest functions test log-event --event update --document-id-before BEFORE_DOCUMENT_ID --document-id-after AFTER_DOCUMENT_ID --dataset production
```

**yarn**

```shell
yarn dlx sanity@latest functions test log-event --event update --document-id-before BEFORE_DOCUMENT_ID --document-id-after AFTER_DOCUMENT_ID --dataset production
```

**bun**

```shell
bunx sanity@latest functions test log-event --event update --document-id-before BEFORE_DOCUMENT_ID --document-id-after AFTER_DOCUMENT_ID --dataset production
```

Replace `BEFORE_DOCUMENT_ID` and `AFTER_DOCUMENT_ID` with the IDs of the documents to use as the before and after states. The CLI fetches each document by its exact ID, and passes the after document to your function as the payload.

#### Include your personal token for API calls

Local invocations don't include a `token` by default. By setting the flag, the function handler will have access to it at `context.clientOptions.token`.

**npm**

```shell
npx sanity@latest functions test log-event --with-user-token
```

**pnpm**

```shell
pnpm dlx sanity@latest functions test log-event --with-user-token
```

**yarn**

```shell
yarn dlx sanity@latest functions test log-event --with-user-token
```

**bun**

```shell
bunx sanity@latest functions test log-event --with-user-token
```

## Tips and best practices

### Use real data that matches your event

Your blueprint defines a function's event, including the filter and projection. The testing tools respect these settings and won't invoke a function if the incoming document shape wouldn't trigger it in a production setting.

### Detect local invocation

Even when you test your functions locally, your code still executes. If you're interacting with a Sanity dataset or external API from within a function, be careful not to trigger writes accidentally.

You can avoid accidental writes by checking if a function is invoked locally. Both the `test` and `dev` commands set the `context.local` value to `true`. It remains undefined in deployed functions.

```typescript
if (context.local) {
  console.log('This code only runs when testing locally.')
}

if (!context.local) {
  console.log('This code only runs in the deployed function.')
}
```

You can also pair this with the `dryRun` functionality in many Sanity libraries and APIs. For example:

**Patch**

```typescript
// will set dryRun to true in test / dev
client
  .patch('bike-123')
  .set({inStock: false})
  .inc({numSold: 1})
  .commit({ dryRun: context.local }) 
```

**Actions**

```typescript
// will set dryRun to true in test / dev
client.action({
    actionType: 'sanity.action.document.create',
    publishedId: 'bike-123',
    attributes: {_id: 'drafts.bike-123', name: 'Sanity Tandem Extraordinaire', _type: 'bike', seats: 1},
    ifExists: 'fail',
  },
  {dryRun: context.local}
)
```

**Agent Actions**

```typescript
client.agent.action.generate({
  schemaId: 'your-schema-id',
  documentId: 'your-document-id',
  instruction: 'Write a summary for the following topic: $topic',
  instructionParams: {
    topic: 'Grapefruit',
  },
  target: {path: ['body']},
  noWrite: context.local
})
```



## Related changelog entries

Entries are listed newest first; each link points to the full entry as markdown. Follow one when you need to know what changed, when, or why — for example, to summarize recent updates, explain behavior that differs from older documentation, or check whether a fix has shipped.

- [Functions v2026-01-28: Function system logs for runtime events](https://www.sanity.io/docs/changelog/1d670c68-a45b-4e29-b69a-69a5828756c7.md) — January 29, 2026