Importing data with array references - missing _key error

2 replies
Last updated: Jun 14, 2024
Hi people, I am using this article (https://www.sanity.io/guides/guide-importing-data-from-external-sources ) as a reference when importing data to Sanity. But I get an error that _key is missing. Is there an updated article somewhere on how to import data with array references via the API?
AI Update

The _key error you're encountering is a common issue when importing data with arrays to Sanity. The good news is there's a straightforward solution!

When you're importing data that contains arrays of objects, Sanity requires each object in those arrays to have a unique _key property. This is essential for real-time collaborative editing and proper tracking of array items. The _key property documentation explains that these keys must be unique within their containing array.

The Solution: autoGenerateArrayKeys

When using the Sanity client to create or patch documents with arrays, you can enable the autoGenerateArrayKeys option. According to the mutation API documentation, this parameter automatically adds _key attributes to array items that are missing them:

import {createClient} from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset',
  token: 'your-token',
  apiVersion: '2024-01-01',
  useCdn: false
})

// When creating or patching documents
client.createOrReplace({
  _id: 'my-document',
  _type: 'myType',
  items: [
    {title: 'Item 1'}, // No _key needed!
    {title: 'Item 2'}
  ]
}, {autoGenerateArrayKeys: true})

Manual _key Generation

If you prefer to generate keys yourself (which gives you more control), you can use a UUID library:

import {v4 as uuidv4} from 'uuid'

const document = {
  _id: 'my-document',
  _type: 'myType',
  items: [
    {_key: uuidv4(), title: 'Item 1'},
    {_key: uuidv4(), title: 'Item 2'}
  ]
}

For CLI-based Imports

If you're using the Sanity CLI migration tool, the good news is that it automatically runs mutations with autoGenerateArrayKeys enabled, so you shouldn't need to worry about manually adding keys.

NDJSON Imports

When importing NDJSON files, make sure each object in your arrays includes a _key property before importing. You can preprocess your NDJSON file to add these keys using a script before running sanity dataset import.

The article you're referencing might be slightly outdated in terms of this specific requirement becoming more strictly enforced, but the core import concepts remain valid. The autoGenerateArrayKeys option is the modern, recommended approach for handling this requirement when importing data programmatically via the API.

Show original thread
2 replies
After adding
_key
to the object, I was presented with a new issue. So an updated article on how to import data via the API would be much appreciated :)
Figured it out, trying to add array of objects as reference is not 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.

Was this answer helpful?