Reducing Sanity API calls on keystroke and enabling CDN usage in Next.js

10 replies
Last updated: Dec 8, 2023
Hi all! I'm using Sanity studio in a next js app, specifically the setup provided by this repo provided by the sanity team. My actual implementation is linked here . I'm noticing that my total API call usage is exorbitantly high, and i've narrowed the reason down to two possible issues. When I update documents in the studio, i'm faced with thousands of sanity API requests that fire on each keystroke when i'm editing saved documents, and this is running up my limit of API requests very quickly. Is there any way to minimize these requests when editing in the studio, say, only fire an API request when getting preexisting documents or publishing new documents rather than on every update to the UI? The second issue is that even though I've set 'useCDN' to be true on the client, next is still grabbing data from the sanity api rather than the api CDN. As such, my API CDN usage is actually minuscule when compared to my my API usage, a value that is supposedly meant to be much smaller, which is actually very high for me. Has anyone had any experience with this issue before?
AI Update

I understand you're experiencing high API usage from Sanity Studio during editing and finding that your CDN isn't being utilized as expected. Let me break down what's happening with both issues.

Understanding Studio's API Requests During Editing

When you edit documents in Sanity Studio, you're seeing thousands of API requests fire on each keystroke. This is expected behavior and is how Studio's autosave system works:

  • Studio creates draft documents (prefixed with drafts.) as you type
  • Each change triggers mutation/patch requests to save your work in real-time
  • This ensures you never lose content during editing
  • These mutation requests always use the live API endpoint (api.sanity.io), never the CDN

This behavior cannot be disabled or reduced - it's a core part of how Studio functions to provide a robust authoring experience. The API is designed to handle this load, and these requests count against your API quota (not CDN quota). Mutations cannot be cached by design since they're write operations.

Why Your CDN Isn't Being Used

The second issue is more straightforward than it might seem. Based on the Sanity CDN documentation, when you set useCdn: true in your client configuration, it tells the JavaScript client to fetch from apicdn.sanity.io instead of api.sanity.io.

Critical detail: In your configuration, you have a typo:

useCDN: process.env.NEXT_PUBLIC_SANITY_DATASET_NAME === "production"

Notice it's useCDN (uppercase "CDN") but the correct property name is useCdn (lowercase "dn"). JavaScript is case-sensitive, so this typo means the client isn't recognizing your CDN setting at all and is defaulting to useCdn: false.

Solution

Fix the typo in all your client configurations:

// helpers/sanity.js
const options = {
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET_NAME,
  apiVersion: '2022-01-18',
  useCdn: process.env.NEXT_PUBLIC_SANITY_DATASET_NAME === "production" // lowercase 'dn'
};

Also fix it in helpers/generateRedirects.js:

const options = {
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET_NAME,
  apiVersion: "2022-01-18",
  useCdn: process.env.NEXT_PUBLIC_SANITY_DATASET_NAME === "production" // lowercase 'dn'
};

When CDN is Used vs. Bypassed

Even with useCdn: true correctly configured, certain operations will always bypass the CDN:

  • Mutations (create/edit/delete) - Studio editing uses the live API
  • Listeners/subscriptions - real-time updates require live API
  • Draft content - when using tokens to fetch unpublished drafts
  • Preview/Draft Mode - when previewing unpublished content

According to the Live CDN documentation, the API CDN uses intelligent caching with approximately 10-minute cache times, providing a balance between freshness and performance.

Verifying Your Fix

After correcting the typo:

  1. Check your Sanity project usage dashboard
  2. Compare "API" vs "API CDN" usage
  3. Expected pattern:
    • High API usage from Studio editing (mutations)
    • High API CDN usage from your Next.js app serving published content
    • Lower direct API usage from your Next.js app (reads should shift to CDN)

Additional Notes

Images: Assets always use the asset CDN (cdn.sanity.io) regardless of the useCdn setting - this is separate from the API CDN and explains why you're seeing cdn.sanity.io URLs for images even when your API queries weren't using the CDN.

Warning message: The console warning about useCdn not being specified should disappear once you fix the typo, as the client will properly recognize the setting.

The Studio's mutation-heavy behavior during editing is by design and ensures a robust authoring experience. The key to managing your API costs is ensuring your public-facing content delivery (your Next.js frontend) uses the CDN for read operations, which should now work once you fix the typo.

Show original thread
10 replies
Studio requests do not count toward your usage. Are you actively editing in your Studio? If so, the cache is invalidated when you publish changes.
Thanks for your input! yes, I am actively editing in my studio. I'm really trying to debug where the respective API and API CDN calls are coming from: from this link in the docs it makes it seem like API calls usually come from editing content (which would be sanity studio in my case) whereas API CDN calls come from serving content to end users. Is there a way to debug where these requests are coming from from Sanity, to determine where the excessive API calls are occurring?
I can tell you which API calls are occurring most frequently, but I can’t give you referrer data. What’s your project ID?
thank you! my project id is nwlu3bfm
It looks like this is the culprit. Are you able to find where you’re performing that query in your code?
Hmm yeah that's my loadSettings query in my navbar and footer- thats interesting. Is it taking up a disproportionate amount of the API calls? Also, I'm surprised that its going to the API rather than the API CDN, i have useCDN set to true on my client and I would prefer to be fetching that information from the CDN rather than the API.
Did you have any luck solving this
user P
?
I did actually, you can see the changes made in this commit
specifically in the 'sanity/loader/loadQuery.ts' folder
awesome thank you!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?