How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

Best way to manage API-fetched data and dynamic category hierarchy in Sanity?

42 repliesLast updated: Nov 29, 2025

Hey there, in you experience with sanity what’s the best way to manage something that can be fetched from an api? Eg. Category order from an e-commerce? I’m trying to get category dynamically from another api and if it’s possibile set documents as child of a singleton document. Any tips?
AI Update

Great question! Managing external API data in Sanity is a common scenario, especially for e-commerce integrations. Let me share some approaches based on what works well in practice.

The Modern Approach: Sanity Functions

For dynamically syncing data from external APIs (like e-commerce category orders), I'd recommend using Sanity Functions as your first choice. Functions are serverless compute that run directly on Sanity's infrastructure and are perfect for this use case:

Benefits:

Example use case: Set up a Function that fetches categories from your e-commerce API and creates/updates corresponding documents in Sanity. You could trigger this on a schedule or when your e-commerce platform sends a webhook.

The Enterprise Option: Sanity Connect

If you're working with Shopify specifically, Sanity Connect for Shopify provides turnkey bi-directional sync. It automatically syncs products, collections, and categories from Shopify into Sanity documents. This is the most robust option for Shopify integrations, handling all the sync logic for you.

Modeling Parent-Child Relationships

For your specific question about setting documents as children of a singleton document, here's the pattern:

Option 1: Singleton with Reference Array (Recommended for your use case)

Create a singleton document that contains an array of references to your category documents:

{
  name: 'categoryOrder',
  type: 'document',
  fields: [
    {
      name: 'categories',
      type: 'array',
      of: [{ type: 'reference', to: [{ type: 'category' }] }]
    }
  ]
}

This gives you a single place to manage category order. When your Function syncs categories from the external API, it can update both the individual category documents AND this singleton's reference array to maintain order.

Option 2: Use the Orderable Document List Plugin

For manual reordering in Studio, install the @sanity/orderable-document-list plugin. It adds drag-and-drop ordering that persists via an orderRank field. However, since you're fetching order from an external API, you'd probably want to programmatically set this field in your sync function.

Practical Implementation Pattern

Here's how I'd approach this:

*[_type == "categoryOrder"][0] {
  categories[]-> {
    _id,
    title,
    // other fields
  }
}

Alternative: Traditional Webhooks

If Functions don't fit your needs, you can still use traditional webhooks with your own serverless functions (Vercel, Netlify, etc.). However, Functions are generally easier since they're integrated directly into your Sanity project as described in this guide on integrating external data.

The key insight is that Sanity excels at being the "source of truth" for your content operations, even when that content originates elsewhere. The sync pattern (whether via Functions or Connect) creates a unified API where your frontend can query everything from Sanity in one request.

Show original thread
42 replies

Was this answer helpful?

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.

Related contributions