Tracking down excessive API requests and potential webhook integration issue.

16 replies
Last updated: Apr 14, 2021
Our API requests have skyrocketed. We're using the js client with CDN enabled, so not sure where these requests could come from. Is there a way to track down where the requests are coming from?
AI Update

Yes, there is! Sanity has a Request Logs feature that can help you track down where those API requests are coming from.

Accessing Request Logs

If you're on a self-serve plan, you can access request logs directly from your project settings:

  1. Go to the 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 that contain detailed information about every request.

What You'll Find in the Logs

The request logs include really useful fields like:

  • Request Type: Whether it's API, APICDN, or CDN requests
  • Remote IP: The IP address making the request
  • User Agent: What client/browser is making the request
  • Referrer: Where the request is coming from
  • Full URL: The complete request including query parameters
  • GROQ Query Identifier: A hashed version of your queries (great for finding duplicate/inefficient queries)
  • Timestamp: When each request happened

Analyzing the Data

You can convert the compressed NDJSON to CSV for easier analysis:

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

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

Pro Tip: Filter Out Studio Requests

Requests from Sanity Studio are marked as studioRequest and don't count toward your API usage or cost. Make sure to filter these out when analyzing what's driving your actual API consumption.

Common Culprits to Look For

Based on your situation with CDN enabled, check for:

  • Requests with requestType: "API" instead of "APICDN" (might indicate CDN isn't being used where you think it is)
  • Repeated identical queries that could be cached better
  • Bot traffic (look at User Agent and IP patterns)
  • Development environments accidentally hitting production
  • Missing dependencies in React hooks causing request loops

You might also want to implement request tags in your code going forward to make it easier to track which parts of your application are making which requests.

CDN requests sitting at 7k
You developing at the same time on the project?
and we haven't updated content often enough to warrant the CDN to have cleared
not that dataset, no
we have separate production and dev datasets
I don't think we're doing enough dev work to warrant 100k extra requests
Does the
sanity dataset export
affect this API usage ?
Interesting that your spike of activity is on the 7th of April, which coincides with the spike I am seeing on that same day of (unexpected) bandwidth usage… https://sanity-io-land.slack.com/archives/C9Z7RC3V1/p1617952768000500
Hey User, the following could be causing your spike:• previews
• active local development
• increased content activity (editors working in the studio)
• low incremental static regeneration settings (e.g. setting to 1s).
If you share your project id we can take a look and give you more specifics.
Thanks
user M
Doesn't look like development, it's going up the same amount of requests per day and over the weekend where there was no development. Project ID is
r80nmzas
I am doing a sanity export daily, but can't see how that'd be 100k requests/day.
Thanks, User. We’ll start looking into it.
Thanks
user M
- it's an insane number of API requests, I really don't think we can be responsible for it unless something is seriously wrong in our code. If there's any info you can share on who the requester is and what the queries are, that'd help us track it down in case it is us.
It's going mental.
Looks like we're hot on the heels of the problem, it's potentially an issue with our webhook integration.Suggestion: If there were some slightly more info/stats in the sanity dashboard we probably could have narrowed this down sooner. i.e. api calls per dataset instead of just project level. Top 5 or 10 queries. Top 5 requesting IPs.
I'll review the API usage tomorrow, but at the moment it looks like we found the cause 👍
user M
As mentioned above, more info on the API requests (even if it's limited to top 5s for developer & advanced plans) would have helped debug this a lot sooner.
user J
glad you’ve got it sorted. I agree that more insight into API requests would be super helpful in situations like this (and maybe help to catch problems before they blow through a plan’s limits). I definitely recommend dropping your thoughts into the ideas and feedback channel or opening a feature request on gh.

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?