Issue with API requests being called repeatedly in Next.js project.

38 replies
Last updated: Jan 26, 2022
when looking at my API requests its saying 7000 requests per day?
AI Update

It sounds like you're seeing a limit of 7,000 requests per day when checking your API usage. This is likely related to Sanity's usage quotas, which vary by plan. Let me break down what this might mean:

Understanding Your API Request Limits

The 7,000 requests/day figure isn't explicitly mentioned in Sanity's current technical limits documentation, but here's what you should know:

Rate Limits vs. Quota Limits

Sanity has two types of limits:

  1. Rate limits (per second) - These are enforced per IP address:

    • Maximum global API call rate: 500 req/s
    • Maximum mutation rate: 25 req/s
    • Maximum concurrent queries: 500
  2. Monthly quotas - These vary by plan and are what you're likely seeing

Plan-Based Quotas

Your monthly API request quota depends on your plan:

  • Free Plan: Limited monthly API requests (the exact limit varies)
  • Growth Plan: Higher included limits with pay-as-you-go for overages
  • Enterprise Plan: Custom quotas

The Growth Plan pricing page mentions that you can add an "Increased Quota" add-on for $299/month that extends limits to 1M API requests per month (which would be roughly 33,000 per day).

What You Should Do

  1. Check your project dashboard at sanity.io/manage to see your current plan and usage details
  2. Review your API usage patterns - 7,000 requests/day might be sufficient for many use cases, but if you're hitting limits:
    • Implement caching on your frontend
    • Use the API CDN which has unlimited cached requests
    • Consider upgrading to Growth plan if you need more capacity
  3. Optimize your queries - Make sure you're not making unnecessary API calls

If you're on the Free plan and need more requests, you can add a credit card to pay for overages, or upgrade to the Growth plan for higher included limits.

Hey User! If you don't have a ton of traffic to your site, it's likely that something is misconfigured on your frontend and you're calling the API repeatedly unintentionally. Can you share you project ID so I can look it up?
cjcfec7t
Its my first time using sanity
I am calling it from getServerSideProps in Next.js
export async function getServerSideProps() {

    const featuredPost = await client.fetch(`
        *[_type == "post" && featured == true && "Chronicles" in categories[]->title ] | order(dateTime(publishedAt) desc) [0]{
            title,
            "name": author->name,
            "categorySlug": categories[]->slug,
            "categories": categories[]->title,
            publishedAt,
            excerpt,
            slug,
            mainImage,
            featured,
            externalLink
        }
    `);

    const podcastPosts = await client.fetch(`
        *[_type == "post" && "Bomb Talk" in categories[]->title ] | order(dateTime(publishedAt) desc) [0..6]{
            title,
            "name": author->name,
            "categorySlug": categories[]->slug,
            "categories": categories[]->title,
            publishedAt,
            excerpt,
            slug,
            mainImage,
            featured,
            externalLink
        }`
    ); 

    const blogPosts = await client.fetch(`
        *[_type == "post" && "Updates" in categories[]->title ] | order(dateTime(publishedAt) desc) [0..6]{
            title,
            "name": author->name,
            "categorySlug": categories[]->slug,
            "categories": categories[]->title,
            publishedAt,
            excerpt,
            slug,
            mainImage,
            featured,
            externalLink
        }`
    ); 

    const newsPosts = await client.fetch(`
        *[_type == "post" && "Chronicles" in categories[]->title ] | order(dateTime(publishedAt) desc) [0..6]{
            title,
            "name": author->name,
            "categorySlug": categories[]->slug,
            "categories": categories[]->title,
            publishedAt,
            excerpt,
            slug,
            mainImage,
            featured,
            externalLink
        }`
    ); 

    return {
      props: {
        featuredPost,
        podcastPosts,
        blogPosts,
        newsPosts
      }
    }
}

export default Media
here are my calls
Got it, it does look like your
getServerSideProps
is the culprit. How often are you triggering rebuilds? Do you have preview enabled in the Studio?
i can try moving it
but the strange this is the site has been in staging and no one has been building it (its hosted in aws)
could you thinnk of anything else?
It looks like this query is running a lot:

*[_type == "post"]{
  title,
  "name": author->name,
  "categorySlug": categories[]->slug,
  "categories": categories[]->title,
  publishedAt,
  excerpt,
  slug,
  mainImage,
  featured,
  externalLink
}
Do you recognize it, configured to a client on
v2021-11-30
?
It looks like this query is running a lot:

*[_type == "post"]{
  title,
  "name": author->name,
  "categorySlug": categories[]->slug,
  "categories": categories[]->title,
  publishedAt,
  excerpt,
  slug,
  mainImage,
  featured,
  externalLink
}
thats strange
i cant find the query it must be an older version
is there anyway of finding out where it came from?
I think i found it
its on the home page
how many times is it getting called?
Essentially all of what you see in your stats is that.
lol well thats not good
okay ill move it into the component
i think the re rendering is eating it up
thanks for all the help
We’ll check tomorrow to see if the calls have levelled off.
great thanks
If you move the query to
getStaticProps
instead it won't run on every render. Should save you a ton of requests. keep in mind that it will still run more in development
I moved it inside the module for now lets see if that works
instead of being rendered at the page level and passed down
that should help if not I can use staticprops
If it's in the component it's likely to keep running every time. A child will rerender when the parent does. If you moved it from
getServerSideProps
to a
useEffect
hook or similar it's only going to get worse
getServerSideProps
runs when the page loads. Any side effects in a component will run when the component rerenders. Depending on your structure that can be more often than the server render, but never less
Yes I only used it for the posts we pulled so there are no other items we need to run server side
so taking it out completely at the home page level should be okay
lets see tomorrow 🙂
Looks like things have levelled off significantly.
hey everyone thanks for all the help
its about 43 requests today which means its all good

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?