Sanity API integration for AI agents

<h2>Sanity API & Chase Agents</h2><h3>Manage Your Structured Content Seamlessly: Chase Agents & Sanity API Integration</h3><p>Chase Agents enhances your content operations by integrating directly with the Sanity API. Build powerful automated agents and applications that can create, read, update, and query your structured content within Sanity—all without writing complex code, managed through Chase Agents's intuitive no-code platform. Streamline content workflows from creation to publication effortlessly.</p>

Connect Sanity API (also known as sanity) to Chase Agents as an API connection, then use it in plain-English automations — on the web, in Slack, or inside ChatGPT and Claude via MCP. Chase Agents builds the workflow and deterministic code runs it.

How to connect Sanity API

Add Sanity API from the Chase Agents connections marketplace as API connection, provide the required credentials, and reference it in your automation steps. See plans or read the connection guide.

About Sanity API

Fetching Data from Sanity with curl: Common Pitfalls and Solutions

When using curl to fetch data from the Sanity API, especially with GROQ queries, a couple of common issues can arise. Understanding these can save you time and prevent frustrating errors.

1. Mistake: Unencoded Special Characters in GROQ Queries

  • The Problem: GROQ queries often contain special characters such as * (wildcard), [ and ] (array/filter syntax), = (equals), " (quotes for strings), and spaces. When these are included directly in a curl URL without proper encoding, curl (or the receiving server) can misinterpret them.
  • The Symptom: You might encounter curl errors like (3) bad range specification in URL or other parsing errors. The Sanity API might also return an error indicating an invalid query, even if the GROQ query itself is logically correct. In our case, the initial error was: curl: (3) bad range specification in URL position 76.
  • The Fix: URL-Encode Your GROQ Query.

Before embedding your GROQ query into the curl command's URL, you must URL-encode it. This process converts special characters into a format that can be safely transmitted over the internet (e.g., a space becomes %20, [ becomes %5B, * becomes %2A).

  • Original (Problematic) Query Part in URL: ...query=*[_type == "agent"][0]
  • Corrected (URL-Encoded) Query Part in URL: ...query=%2A%5B_type%20%3D%3D%20%22agent%22%5D%5B0%5D

Many online tools or programming language functions can perform URL encoding for you.

2. Mistake: curl URL Globbing Interference

  • The Problem: By default, curl can attempt to interpret certain characters in the URL, like square brackets ([ and ]), as "globbing" patterns (similar to filename wildcards in a shell). This can interfere with the structure of your Sanity query URL, even if parts of it are URL-encoded, because the brackets themselves might still be processed by curl before the request is sent.
  • The Symptom: This can also lead to errors similar to the unencoded character issue, or the query might not be interpreted correctly by the Sanity API, leading to unexpected results or errors from Sanity.
  • The Fix: Disable Globbing with -g or --globoff.

To prevent curl from trying to interpret these characters as globbing patterns, use the -g (or its longer form --globoff) command-line option.

Putting It All Together: The Corrected Command

Based on our interaction, a successful curl command to fetch a Sanity document, incorporating these fixes, looks like this:

curl -g -X GET \
  -H "Authorization: Bearer ${SANITY_API_TOKEN}" \
  "https://${SANITY_PROJECT_ID}.api.sanity.io/v2025-02-19/data/query/${SANITY_DATASET}?query=%2A%5B_type%20%3D%3D%20%22agent%22%5D%5B0%5D"

Key takeaways from the corrected command:

  1. -g: Disables URL globbing.
  2. %2A%5B_type%20%3D%3D%20%22agent%22%5D%5B0%5D: The URL-encoded version of the GROQ query *[_type == "agent"][0].

By remembering to URL-encode your GROQ queries and use the -g flag with curl, you can ensure more reliable interactions with the Sanity API from the command line.

Official Sanity API documentation