canonical: https://jentic.com/apis/sanity.io/sanity-functions-api

# Sanity Functions API

The Sanity Functions API runs and inspects the serverless functions deployed to a Sanity project or organization. It lists functions with filters for region (us-east-1 or eu-central-1) and status, reads one function's definition, which can be a document, media library asset, cron, queue, pub/sub, durable, or sync-tag-invalidate function, and invokes it with an event payload, synchronously or not. It also reads and streams a function's logs, deletes them, and sets or removes environment variables by key. Every call names its project or organization in the x-sanity-scope-id and x-sanity-scope-type headers.

## For AI agents

List and invoke Sanity Functions with a test event, read or stream their logs, and set or remove their environment variables.

## Scope

Does not create, deploy, or delete functions, and does not edit content. Use for invoking existing Sanity Functions and managing their logs and environment variables only.

## Capabilities

- List the functions in a Sanity project or organization, filtered by region such as us-east-1 or eu-central-1 and by status such as assigned or failed-update
- Read one function's definition, including its type such as sanity.function.document or sanity.function.cron
- Invoke a function with an event whose data object is the test payload, choosing synchronous execution with the sync query parameter
- Page through a function's logs with limit and offset, narrowed to one executionId
- Stream new log entries as server-sent events from the logs stream endpoint, resuming after a given point with the after parameter
- List a function's environment variable keys, set one by envVarKey with a value, or delete it

## Use cases

### AI agent debugging a Sanity Function

An agent asked why a document-triggered function misbehaves searches Jentic for 'invoke a Sanity function', loads the invoke operation, and posts a test event with sync set to true so it gets the result back. It then reads the function's logs filtered by executionId to see what happened in that run, and does it all without the Sanity token entering its context.

Example prompt: POST /{apiV}/functions/{functionId}/invoke with sync=true, headers x-sanity-scope-id 24s6f8sr and x-sanity-scope-type project, and body {event: {data: {_id: 'post-1', title: 'Test'}}}, then GET /{apiV}/functions/{functionId}/logs and report the entries for that run

### Managing function configuration

Functions that call outside services need keys and settings. The environment variables endpoints list a function's variable keys without their values, set a value by envVarKey with a PUT whose body holds the value, and delete a key that is no longer needed, so configuration can change without redeploying the function code.

Example prompt: GET /{apiV}/functions/{functionId}/envvars to list keys, then PUT /{apiV}/functions/{functionId}/envvars/{envVarKey} with envVarKey WEBHOOK_URL and body {value: 'https://hooks.example.com/sanity'}

### Monitoring function health across a project

Teams with many functions need to spot the broken ones. The list endpoint filters by status values such as failed-create, failed-update, or disabled and by region, and each function's logs can be paged with limit and offset or tailed live as server-sent events from the stream endpoint.

Example prompt: GET /{apiV}/functions with status failed-update for project 24s6f8sr, then GET /{apiV}/functions/{functionId}/logs with limit 20 for each one returned

## Key endpoints

| Method | Path | Description |
| --- | --- | --- |
| GET | `/{apiV}/functions` | List functions |
| GET | `/{apiV}/functions/{functionId}` | Read a function |
| POST | `/{apiV}/functions/{functionId}/invoke` | Invoke a function with an event |
| GET | `/{apiV}/functions/{functionId}/logs` | Retrieve function logs |
| GET | `/{apiV}/functions/{functionId}/logs/stream` | Stream function logs |
| PUT | `/{apiV}/functions/{functionId}/envvars/{envVarKey}` | Set an environment variable |

## Key resources

- **Functions** — Deployed serverless functions of types such as document, cron, queue, pub/sub, durable, and media library asset
- **Invocations** — Runs of a function triggered with an event payload, sync or async
- **Logs** — Log entries per function, filterable by executionId and streamable as server-sent events
- **Environment variables** — Key-value settings on a function, listed by key only

## AI readiness

This API is usable in Jentic One now. Its AI-readiness score against Jentic's framework shows where it stands today and where improvements would make it even easier for agents to use.

- **Score:** 49 / 100
- **Maturity:** Foundational
- **Dimensions:**
  - Foundational Compliance: 52 / 100
  - Developer Experience & Jentic Compatibility: 76 / 100
  - AI-Readiness & Agent Experience: 27 / 100
  - Agent Usability: 94 / 100
  - Security: 60 / 100
  - AI Discoverability: 56 / 100
- **View full report:** https://jentic.com/apis/sanity.io/sanity-functions-api/scorecard
- **How the score is calculated:** https://docs.jentic.com/reference/api-readiness-framework/overview/
- **More about the dimensions:** https://docs.jentic.com/reference/api-readiness-framework/specification/#dimensional-model-overview

### Score it yourself

Every API in the directory is allowlisted, so you can re-score it with no key required.

- **Score your own API:** https://jentic.com/scorecard.md
- **Scoring CLI agent skill:** https://github.com/jentic/jentic-api-scorecard/blob/main/skills/jentic-api-scorecard/SKILL.md

```sh
npx @jentic/api-scorecard-cli score <openapi-url>
```

## Why Jentic

- **Setup:** Wiring the Sanity Functions API by hand means sending a bearer token plus the x-sanity-scope-id and x-sanity-scope-type headers on every call, shaping the event body for invoke, and consuming server-sent events for the log stream. Through Jentic you install once, import it from the API Directory, store the token once, and your agent calls it.
- **Permission scoping:** Sanity puts the function id in the URL path (/{apiV}/functions/{functionId}/...), so a rule can pin your agent to one function and the operations you pick. The project itself travels in the scope headers, so rules bound which functions and operations the agent may call, not which project, and destructive ones like log deletion or environment variable removal are not included unless you add them.
- **Credential handling:** Your Sanity token is stored once, encrypted, by your own Jentic One instance and injected at execution time. It never enters the agent's prompt, logs, or context.
- **Discovery method:** Agents search Jentic by intent such as 'invoke a Sanity function' or 'stream Sanity function logs', and Jentic returns the matching operation with its input schema, including the required scope headers, so the agent calls it correctly first time.

## Related APIs

- **Sanity Webhooks API** — Send content change events to your own HTTP endpoint instead of running code on Sanity
- **Sanity HTTP API** — Query and mutate the documents that trigger document functions
- **AWS Lambda API** — General-purpose serverless functions on AWS
- **Cloudflare API** — Run Workers and manage Cloudflare services

## FAQ

### What authentication does the Sanity Functions API use?

Per its OpenAPI spec, every operation uses a bearer token, described as a Sanity API session token, in the Authorization header. Each call also needs the x-sanity-scope-id and x-sanity-scope-type headers to name the project or organization the function belongs to. Through Jentic the token is stored by your own instance and attached at call time.

### How do I invoke a Sanity Function for testing?

POST to /{apiV}/functions/{functionId}/invoke with a body of the form {event: {data: {...}}}, where data is the payload the function should receive. Set the sync query parameter to true to run it synchronously; it defaults to false. The optional x-sanity-lineage header carries a lineage token for the request.

### Can I deploy or create functions with the Sanity Functions API?

Not through this spec. It covers listing, reading, invoking, logs, and environment variables for functions that already exist; there is no create, deploy, or delete-function operation in it.

### What are the rate limits for the Sanity Functions API?

The OpenAPI spec does not specify rate limits. Check the Sanity documentation at https://www.sanity.io/docs for current limits on functions for your plan.

### Is there a Sanity Functions MCP server?

You don't need an MCP server to give your agent the Sanity Functions API. Jentic connects it directly from the API Directory: import it, store your Sanity token once, and your agent calls the invoke, logs, and environment variable operations it needs without loading a separate server's tool definitions.

### How do I read a function's logs through Jentic?

Search Jentic for 'get Sanity function logs', load the matching operation, and execute GET /{apiV}/functions/{functionId}/logs with the scope headers, adding executionId to narrow it to one run. It returns the most recent entries by default, and limit and offset page further back. To run it on your own infrastructure, install Jentic One from its GitHub repo.

### Can I limit what my agent is allowed to do with the Sanity Functions API?

Yes. Allow only the invoke and log-read operations under /{apiV}/functions/abcd123456, and the agent can test that one function and read its output while environment variable changes and log deletion stay off its list. Every call it makes is logged.
