How to analyze high Sanity CDN API usage and identify request sources?

13 replies
Last updated: Sep 19, 2022
Hey folks! Been playing around with Sanity V3, Remix and the Shopify Connect over the last couple of weeks and I’ve noticed we’re getting a huge amount of API CDN requests (hitting about 50k a day 😰 ) Is there any way to get more info out of the usage data to maybe see where this is coming from? Thanks 🙂
AI Update

Good news - you can definitely get more detailed information about those API CDN requests! Sanity has a Request Logs feature that's perfect for tracking down exactly where your usage is coming from.

For self-serve plans, you can download request logs directly:

  1. Go to your project's Usage section at sanity.io/manage
  2. Scroll to the bottom of the page
  3. Download up to 1GB of log data from the last 7 days
  4. You can request a new export every 24 hours

The logs come as GZIPPED NDJSON files and include super helpful fields like:

  • Request Type (API, APICDN, CDN) - so you can see exactly which requests are hitting the CDN
  • Full URL with query parameters
  • GROQ Query Identifier - lets you group similar queries together
  • Referrer and User Agent - helps identify where requests are coming from
  • Remote IP - can help spot bot traffic or unexpected sources
  • Timestamp - identify patterns or spikes

Pro tip: Use Request Tags to track down sources

This is probably the most useful thing for your situation. You can add the tag query parameter or X-Sanity-Client-Tag header to your requests to label them. This makes it super easy to see which parts of your app are generating the most requests.

If you're using @sanity/client, you can set this up at two levels:

const client = createClient({
  projectId: "<project>",
  dataset: "<dataset>",
  useCdn: false,
  apiVersion: "2024-01-24",
  requestTagPrefix: "remix-app" // Added to every request
});

// Then tag specific requests
const products = await client.fetch('*[_type == "product"]', {
  tag: `product-page`, // Results in tag=remix-app.product-page
});

Analyzing the logs

Once you've downloaded the logs, you can convert them to CSV for easier analysis:

gunzip --stdout [logfile].ndjson.gz | npx json2csv --ndjson --output output.csv

You can also use tools like jq, GROQ CLI, or even upload samples to ChatGPT to help analyze patterns.

Common culprits for high CDN usage:

  • Missing dependencies in useEffect hooks causing request loops
  • Shopify webhook integrations making excessive calls
  • Bot traffic
  • Queries running on every render instead of being properly cached

Since you're using Remix + Shopify Connect, I'd especially check if your Shopify webhooks or product sync logic might be triggering more fetches than expected. The request logs will show you the exact URLs and patterns so you can pinpoint it!

Show original thread
13 replies
Should mention this is a proof of concept piece so it’s only me visiting the site etc.
Yes there is and yes there is! But I think I might know what the issue might be: syncing sanity to the cart (which means, that every-time something in the cart is happening, an api call is fired.If you send me your projectID and details in a DM I can also provide you with a more detailed account of your data usage.
And I will see if I can find the most recent version of a POC..
Amazing thank you! I’ll send it over now 🙂
The Sanity Shopify Connect app is set to sync products and collections manually, and the last sync was on the 9th September
I think the reason you’re seeing this kind of volume is def. some sort of error in your code (useEffect or some other hook)
since we know the culprit queries (DM) you should check where there are called and why so many times per day!What framework are you using?
Ahh cool thanks. Hmm interesting, we’re using Remix and the worst offender is a query in the root.tsx file so I’ll go and have a look at that and see what’s going on there
Okay so I need to double check this tomorrow morning (as it’s time I went home lol) but it looks as though my query is getting picked up in my healthcheck endpoint and fly.io is querying it every 10 seconds 🙃 whoops!
woopsie
Yeah i need to go …. to the other room #remote💜life
Hey! Just to follow up on this with a more detailed answer in case anyone comes across this in the future (or I do it again 😂 )
In the Remix Indie Stack there is a
heathcheck route intended for services like fly.io to call at regular intervals, the default is every 10 seconds.
Inside that healthcheck there is a loader function containing a fetch request which fetches the homepage and returns an “OK” response if it connects okay.

If you have any queries to Sanity in the
loader()
function on that page (either in
root.tsx
or
routes/index.tsx
in this case, then they will get called when the fetch runs.
I’ve resolved this by removing the fetch request to the homepage from the healthcheck file.
Appreciate the extra effort! And wonderful you found it as well 💪If you want, you can publish this in our
Exchange platform and make this also google-able and get your name under the solution 🙂
This made me giggle on monday morning …. so extra thanks 😉

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?