Discussing syncing external data with Sanity and adding new fields to the schema.

7 replies
Last updated: Mar 2, 2022
Hello! I have a high-level, architectural approach question for folks who have tied Sanity pretty closely to existing / external systems.
Imagine a platform that has several existing pieces of data in a database. For example, let's say that they're `tags`: they have a
label
and a
slug
.
These tags are in use, and already managed through a few admin forms. The intent is to keep them managed through these forms.

I'd love to keep these tags in sync, one-way, in Sanity, with the addition of an
image
to their schema.
A few questions:

1. Is this possible?
◦ It seems straight-forward enough to add or update a tag in Sanity from the existing system. The main unknown for me is the slight mutation of schema with the additional
image
. Are there traps there?2. Is this advisable?
◦ I know it introduces complexity to the system, but allows editorial additions to a schema that the external system doesn't care about. Different roles are managing tags and content, and editorial shouldn't be in control of these particular tags.
Any additional info that'd help? Are there variables I'm not considering? Thanks for any insight you can provide!
AI Update

This is absolutely possible and a pretty common pattern! You're essentially looking to create a one-way sync from your existing system to Sanity, where Sanity acts as an "enrichment layer" for your tags. Let me break down your questions:

1. Is this possible?

Yes, very straightforward. The schema mutation you're describing (adding an image field in Sanity that doesn't exist in your source system) is actually one of the main benefits of this pattern. Here's how it works:

Your Sanity schema would look something like:

{
  name: 'tag',
  type: 'document',
  fields: [
    {name: 'label', type: 'string', readOnly: true},
    {name: 'slug', type: 'slug', readOnly: true},
    {name: 'image', type: 'image'} // Editorial addition
  ]
}

The sync process from your external system would only touch label and slug, leaving image completely under editorial control. You can use the Sanity client to create or update documents.

Key trap to avoid: Use createOrReplace() carefully or use patch() instead. With createOrReplace(), you'll completely replace the document, which would wipe out the image if editors added one. Better approach:

await client
  .patch(externalTag.id)
  .set({label: externalTag.label, slug: {current: externalTag.slug}})
  .commit()

This way, only the fields you explicitly set are updated, preserving the editorial image field.

2. Is this advisable?

Yes, this is a well-established pattern at Sanity. In fact, Sanity Connect for Shopify works exactly this way - syncing product data from Shopify while allowing editors to enrich it with additional fields in Sanity.

Benefits of this approach:

  • Separation of concerns: Your admin system remains the source of truth for tag management
  • Editorial enrichment: Content teams can add context (images, descriptions, etc.) without touching the operational system
  • Single source for frontend: Your frontend can query everything from Sanity in one GROQ query instead of joining data from multiple sources

Implementation recommendations:

  1. Mark synced fields as read-only in your schema so editors don't accidentally change them
  2. Use your external ID as the Sanity _id to make syncing deterministic (e.g., _id: externalTag.id)
  3. Consider using Sanity Functions (if you're on a Growth plan or higher) to handle the sync. They're serverless functions within Sanity that can respond to events in your external system, eliminating the need for separate infrastructure
  4. Set up a webhook from your external system to trigger syncs when tags change, or use a scheduled cron job if webhooks aren't available

Additional variables to consider:

  • Deletion handling: What happens when a tag is deleted in your external system? You'll need to decide whether to delete it from Sanity or just mark it as inactive
  • Reference integrity: If other Sanity documents reference these tags, deleting them could break things. Consider using weak references or keeping soft-deleted tags around
  • Initial sync: You'll need a one-time bulk import script to populate existing tags
  • Conflict resolution: What happens if someone manually changes a synced field in Sanity? Your sync should overwrite it, but you might want logging to catch these cases

This pattern is battle-tested and gives you the best of both worlds - operational control where it matters and editorial flexibility where you need it. The guide on integrating external data sources provides more detailed examples of sync implementations if you need them!

This is pretty similar to how a lot of people handle ecommerce integrations. The ecommerce platform handles the base product but sanity adds extra fields to the product's schema. I'd say just make sure the data from upstream (eg. label and slug) is read-only in the Sanity Studio.That's to say: it's pretty common and you shouldn't have any big issues.
That's great news! I thought as much, but it's nice to confirm before dedicating some time to it. Thank you!
user E
It does not currently, though in our case I think this is okay: we want the source of truth for part of the schema to be outbound from our system to Sanity, instead of a two-way sync or the reverse.
It does not currently, though in our case I think this is okay: we want the source of truth to be outbound from our system to Sanity, instead of a two-way sync.
(I might be misunderstanding your point!)
I took a second to digest it and I think I understand, a webhook implementation would be a little more resilient than having to identify all the places we need to set up a sanity client in code.
You can create Sanity documents to augment external data with or without syncing. But if you don't have syncing from the external service, you'll need to manually set an identifier for the external data source on your Sanity documents.
Syncing would essentially allow you to have content from the external source "pushed" into Sanity automatically. From there, it's probably a little easier to maintain documents that augment your external data
🙂. It also has the advantage of pushing the content itself directly into Sanity, allowing you to query it with GROQ and stitch it together with other content in interesting ways!

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?