Best way to manage API-fetched data and dynamic category hierarchy in Sanity?
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:
- No external hosting needed - runs on Sanity's infrastructure
- Event-driven triggers (when categories change, update your data)
- Can run on a schedule or via webhooks from your e-commerce platform
- Direct access to Sanity's client API for creating/updating documents
- Node.js v22 runtime with TypeScript support
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:
- Create category documents for each category from your API
- Use a singleton to maintain the canonical order (array of references)
- Set up a Sanity Function that:
- Fetches categories from your e-commerce API
- Creates/updates category documents in Sanity
- Updates the singleton with the correct order
- Query efficiently - Frontend can fetch everything in one GROQ query by starting with the singleton and dereferencing
*[_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 thread42 replies
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.