
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeThe _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.
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})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'}
]
}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.
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.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store