Difference between API Requests and CDN Requests in Sanity

2 replies
Last updated: Apr 19, 2024
What is the difference between API Requests and CDN requests?
AI Update

The key difference between API requests and CDN requests in Sanity relates to whether your content is being fetched from the origin API or served from cached responses:

API Requests are direct queries to Sanity's Content Lake that bypass the cache. These requests:

  • Go directly to the origin servers at api.sanity.io
  • Always fetch the latest, most up-to-date content
  • Count against your plan's API request rate limits
  • Have slightly higher latency since they hit the origin servers
  • Are used when you need real-time data or when the CDN doesn't have a cached version

CDN Requests are served from Sanity's API CDN at apicdn.sanity.io. These requests:

  • Serve cached responses from edge locations closest to your users
  • Have unlimited rate - they don't count against your API rate limits
  • Deliver content faster due to geographic distribution
  • May serve slightly stale content (until cache invalidates)
  • Are the default for most content fetching scenarios

In Practice:

When you configure Sanity clients, the useCdn option controls this behavior. Setting useCdn: true (the default for production) routes requests through the API CDN, while useCdn: false makes direct API requests.

For most use cases, you'll want to use CDN requests for public-facing content to benefit from faster delivery and unlimited rate. You'd use direct API requests when you need guaranteed fresh data (like in the Studio or for authenticated content) or when working with draft content.

The API CDN also works with cache invalidation - when you publish content changes, Sanity automatically invalidates the relevant cached entries so users get updated content quickly.

Show original thread
2 replies
You can find information about that by searching the docs .
API requests are handled on demand and are the most current version of content. CDN requests get a stored version of the data from the content delivery network.

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?