# Resolve reference fields from API data

https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/resolve-reference-fields

Create the shared helper module that your import scripts use to translate Webflow item IDs into Sanity document references.

---

## Navigation

**Course:** [Migrating from Webflow to Sanity](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity) · [View as markdown](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity.md)

**Previous:** [Run the asset pre-upload pass](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/asset-pre-upload) · **Next:** [Convert rich text to Portable Text](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/convert-rich-text-portable-text)

---

## Course Contents

1. [Introduction](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/introduction-to-webflow-migration)
2. [Audit your Webflow content](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/audit-webflow-content)
3. [Recognize Webflow's content modeling patterns](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/recognize-content-modeling-patterns)
4. [Export from Webflow and connect the Data API](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/export-webflow-connect-api)
5. [Initialize Sanity and install the migration toolkit](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/initialize-sanity-migration-toolkit)
6. [Map collections to Sanity document types](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/map-collections-to-schemas)
7. [Build a page builder with a sections array](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/page-builder-sections-array)
8. [Model custom-template pages as singletons](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/custom-template-singletons)
9. [Model navigation in Sanity](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/model-navigation)
10. [Create a siteSettings singleton](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/site-settings-singleton)
11. [Handle internal links in Portable Text](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/internal-links-portable-text)
12. [Fetch your collection items from the API](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/fetch-collection-items-api)
13. [Run the asset pre-upload pass](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/asset-pre-upload)
14. **Resolve reference fields from API data** *(current)*
15. [Convert rich text to Portable Text](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/convert-rich-text-portable-text)
16. [Extract static page content using the page DOM API](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/extract-static-page-content)
17. [Import content with the two-pass pattern](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/two-pass-import)
18. [Validate your import in Studio](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/validate-import-studio)
19. [Set up forms, redirects, and sitemap](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/forms-redirects-sitemap)
20. [Set up draft mode and visual preview](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/draft-mode-visual-preview)
21. [Validate your import without a frontend](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/validate-migration)
22. [Launch checklist and DNS transfer](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/launch-checklist-dns)
23. [AI prompt library and resources](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity/appendix-prompt-library-resources)

---

`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.



```javascript:scripts/resolve-refs.js
// 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:



```javascript
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.



---

## Related Resources

- [Full course as markdown](https://www.sanity.io/learn/course/migrating-content-from-webflow-to-sanity.md)
- [All courses and lessons](https://www.sanity.io/learn/sitemap.md)
- [Complete content for LLMs](https://www.sanity.io/learn/llms-full.txt)

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback:

POST https://www.sanity.io/learn/resource/feedback

```json
{
  "path": "/learn/course/migrating-content-from-webflow-to-sanity/resolve-reference-fields.md",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.

</AgentInstructions>
