
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeFor your array field, you have a few different approaches depending on what you want to do with the data. Here's how to handle mutations for that array type:
If you're working with the Sanity JavaScript client, here's how to mutate your options array:
import { v4 as uuidv4 } from 'uuid';
client
.patch('document-id')
.setIfMissing({options: []}) // Ensure the array exists
.insert('after', 'options[-1]', [
{
_type: 'option',
_key: uuidv4(), // Generate unique key
// ... your option fields
}
])
.commit()client
.patch('document-id')
.set({
options: [
{
_type: 'option',
_key: uuidv4(),
// ... your option fields
}
]
})
.commit()If you're using the Mutation API directly:
{
"mutations": [
{
"patch": {
"id": "document-id",
"insert": {
"after": "options[-1]",
"items": [
{
"_type": "option",
"_key": "unique-key-here",
// ... your option fields
}
]
}
}
}
]
}_key propertyEvery object in a Sanity array must have a unique _key property. This is crucial for:
Use a UUID library to generate these keys programmatically. The Studio generates them automatically, but when using the API directly, you need to provide them yourself.
"after": "options[-1]""before": "options[0]""replace": "options[2:]" to replace from index 2 onwardsCheck out the patches documentation for more advanced operations!
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