CoursesMigrating from Webflow to SanityResolve reference fields from API data
Migrating from Webflow to Sanity

Resolve reference fields from API data

Create the shared helper module that your import scripts use to translate Webflow item IDs into Sanity document references.
Log in to mark your progress for each Lesson and Task

scripts/resolve-refs.js is a utility module you don't run directly. It exports two functions your import scripts use to convert Webflow item IDs to Sanity references. Create it now so it's ready for Chapter 5.

The Webflow API returns reference fields as Webflow item ID strings, single-ref fields are one string, multi-ref fields are an array. Both convert to Sanity { _type: 'reference', _ref: sanityId } objects using id-map.json, which pass 1 of the import builds.

// scripts/resolve-refs.js
// Single reference field — item.fieldData.author = "64e7f4a8b0e1234567890def"
export function resolveRef(webflowId, idMap) {
if (!webflowId) return null
const sanityId = idMap[webflowId]
if (!sanityId) console.warn(`No Sanity ID for Webflow ID: "${webflowId}"`)
return sanityId ? { _type: 'reference', _ref: sanityId } : null
}
// Multi-reference field — item.fieldData.tags = ["64e7f4a8b0e111", "64e7f4a8b0e222"]
export function resolveMultiRef(ids, idMap) {
if (!Array.isArray(ids) || ids.length === 0) return []
return ids
.map(webflowId => {
const sanityId = idMap[webflowId]
if (!sanityId) console.warn(`No Sanity ID for Webflow ID: "${webflowId}"`)
return sanityId ? { _type: 'reference', _ref: sanityId } : null
})
.filter(Boolean)
}

In Chapter 5, your import script's buildReferenceFields() function will import and use these like this:

import { resolveRef, resolveMultiRef } from './resolve-refs.js'
function buildReferenceFields(item, idMap) {
return {
author: resolveRef(item.fieldData.author, idMap),
tags: resolveMultiRef(item.fieldData.tags, idMap),
}
}

AI will generate the collection-specific buildReferenceFields() function for each of your collections in the import lesson. You won't write it by hand.

Next, you’ll build the rich text converter before running the page extraction script.

Mark lesson as complete
You have 1 uncompleted task in this lesson
0 of 1