Understanding number of API calls

Hello everyone! :wave:

Probably a beginner question, but I would like to better understand when and how many API calls my website does. I'm using NextJS with the Page Router and getStaticProps

For example:

export const getStaticProps: GetStaticProps = async ({
  locale,
}) => {
  const client = createClient();

  const [navigation, document, footer] =
    await Promise.all([
      client.getByUID('navigation', 'header', { lang: locale }),
      client.getSingle('homepage', { lang: locale }),
      client.getByUID('navigation', 'footer', { lang: locale }),
    ]);

  return {
    props: { navigation, document, footer },
  };
};

I assume that this will do 3 content call plus X amount of assets calls, for each locale.

How many calls will this function actually do? Does the number of content API calls depends on how many slices my homepage has? Does createClient() counts as an API call?
Also, the calls will be done only once at build time? By using graphQL instead, could I reduce the number of calls?

Thanks a lot in advance! :pray:

1 Like

Hey Andrea,

This is a great question. I want to make sure I get you an accurate answer, so I'm going to wait until some of my colleagues have returned from vacation next week to double-check my information.

For now, I will say:

I assume that this will do 3 content call plus X amount of assets calls, for each locale.

I need to check on this.

Image calls do count as API calls.

In principle, you can query multiple locales with one query by adding the lang: "*" option to your query, but I personally wouldn't do this as it will make your data fetching quite messy. That being the case, yes, you should do separate queries for each locale.

Does the number of content API calls depends on how many slices my homepage has?

No.

Does createClient() counts as an API call?

I need to check on this.

The calls will be done only once at build time?

I need to check on how Next.js handles data in CSR mode.

By using graphQL instead, could I reduce the number of calls?

No, I wouldn't recomend switching to GraphQL. I don't think it would reduce your API calls.

If you're thinking it would reduce your calls by allowing you to request multiple types in one query, you might be able to do this with filters on the REST API:

prismic.filter.any("document.type", ["navigation", "homepage"])

I'll get back to you with follow-ups next week.

Sam

Thank a lot, Sam!

I need to check on how Next.js handles data in CSR mode.

I just want to point out that I'm using SSG with getStaticProps without revalidation.

Looking forward to your follow-ups! :pray:

Hey @stano.94,

I got a little more information about this.

I assume that this will do 3 content call plus X amount of assets calls, for each locale.

This is not so straightforward.

Each function (e.g. client.getByUID()) makes two API calls: one to the Repository API to check your content version, and then one to the Document API. This is essential behavior, there is no safe way to circumvent it, and so we took the two-step API call process into account when calculating the API limits. Users often ask if there is a way to avoid the two-step API call process, and I strongly recommend against it.

The calls will be done only once at build time?

Your code example will make six API calls at build time. However, if I understand correctly, Next.js will rerun those API calls in CSR mode when a user is browsing your website. So in principle you could have an unknown number of additional API calls. On top of that, each query for an asset will also constitute an API call, which is dependent on traffic and user behavior.

Having said that, we don't expect many users to hit these limits. We calculated them to be well above the necessary usage for most customers. (The Prismic documentation, for example, which has hundreds of pages, had 400,000 API calls in November — 10% of the limit for the free plan.)

Does createClient() counts as an API call?

No.

I hope that helps. Let me know if you have any other questions.

Sam

Hi @samlittlefair,

I've been working on addressing our API usage, which recently exceeded the 4 million limit on our repository. To remedy this, I've refactored our API call strategy.

Previously, we fetched documents in multiple API calls in all languages during each build, but I've changed that. Now, I pre-fetch all these documents before the build, storing them locally. This way, they're accessed as needed during the build, without extra API calls. Consequently, our API calls have dropped significantly to just 14 - these are solely for fetching the custom types with getAllByType. Outside the build process, API calls are limited to Preview mode for getting the latest changes.

However, there's an issue I can't quite figure out. Despite these changes, our Usage Dashboard still shows an average of 16,000 API calls per day although we are only having about 10 builds per day. This doesn't seem to align with the reduced call strategy I've implemented. Any thoughts on why this discrepancy might be occurring?

Hey @antonio,

In order to discuss the specifics for your repository, could you submit a ticket in our support portal?

https://prismic.atlassian.net/servicedesk/customer/portals

Sam