Best way to copy an array from one product to another using Sanity.io API.

5 replies
Last updated: Jun 25, 2021
Hey all, best way to copy an array from a product to another product ?
For example if I want all this copied to another page, what would be the best way?
AI Update

Good news - Sanity Studio has built-in copy/paste functionality for array fields that makes this pretty straightforward!

Using Built-in Copy/Paste (Easiest Method)

Sanity Studio supports native copy/paste operations for array fields. Here's how:

  1. Open the source product in Studio
  2. Click the Field Actions menu (three dots) next to the array field you want to copy
  3. Select "Copy" (or use Ctrl/Cmd+C when focused on the field)
  4. Open the destination product
  5. Click the Field Actions menu on the target array field and select "Paste" (or Ctrl/Cmd+V)

This works for entire arrays or individual array items. You can even copy items between different document types as long as the field structures are compatible.

Programmatic Approach

If you need to copy arrays between many products or automate the process, you can use the Sanity Client:

import {createClient} from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset',
  token: 'your-token-with-write-access',
  apiVersion: '2025-02-19'
})

// Get the source product
const sourceProduct = await client.getDocument('source-product-id')

// Copy the array to the destination product
await client
  .patch('destination-product-id')
  .set({ yourArrayFieldName: sourceProduct.yourArrayFieldName })
  .commit()

Cross-Dataset Copying

If you need to copy between different datasets or projects, check out the Cross Dataset Duplicator plugin which handles migration of documents and assets between Sanity projects directly from Studio.

The built-in copy/paste is definitely your quickest option for one-off copies!

Existing
If you’re comfortable using the HTTP API (in Insomnia or Postman, for example), you could get the JSON of the desired array, paste it into the JSON of the destination document, and use a patch mutation to set the new JSON.
Any sample? How to get document id and that position?
You’ll need to have a write token created. I use Insomnia but you could use Postman, curl, or I think there’s even a VS Code extension. This is how I would do it:
1. In your API client of choice (e.g., Insomnia), set up a POST request that points at the
mutate endpoint . It might look like
https://<projectId>.<http://api.sanity.io/v2021-03-25/data/mutate/<dataset|api.sanity.io/v2021-03-25/data/mutate/<dataset>>
. You’ll need to set up a Bearer token with your read/write token.2. In the document that has the array you want to duplicate, click the three vertical dots in the top-left corner and choose Inspect. Click Raw JSON at the top, then select the entire JSON key and array (being careful to copy the correct number of closing curlies and brackets).
3. Get the
_id
of the document you want to copy the array to (i.e., the destination document). You can find the
_id
using the Inspect method in the previous point.4. You can now
patch in the data using
set
. It has the potential to ruin the document if mistyped, which is why it’s a great idea to practice on a dummy document or a non-production dataset. As long as you paste in the JSON as an object whose top-level key is the array you want to copy into (note that everything below is wrapped in curlies, which weren’t in the JSON that we copied—at least in my case), everything else in the document should remain the same (but please do a test first). Anything already in the array, if it exists, will be overwritten. The JSON of your mutation might look like:
{ 
  "mutations": [
    { 
      "patch": { 
        "id": "a1b482f0-123a-257b-yoyo-f4111da573b3",
		"set": {
		  "modules": [
			{
			  "_key": "04f4f399ea28",
			  "member": {
			    "_ref": "df0f021e-fcd6-42b4-b9fc-1a1bece49e74",
			    "_type": "reference"
			  },
			  "position": "some position"
		    },
			{
			  "_key": "2e79707d887c",
			  "member": {
				"_ref": "dac1cd71-0b6a-4083-b0e4-c3ee11bdb6c1",
				"_type": "reference"
			  },
			  "position": "second position",
			  "startDate": "2021-06-20"
			},
			{
			  "_key": "104d443e1072",
			  "endDate": "2021-06-20",
			  "member": {
			    "_ref": "7266e7e0-9996-4124-9aed-c03234111f47",
				"_type": "reference"
			  },
			  "position": "third",
			  "startDate": "2020-06-30"
			}
		  ]
		}
	  }
    }
  ]
}
If the mutation is successful, you’ll get a 200 OK response with a transactionId and
results.operation
of
update
. You’ll also see the array immediately in your destination document. If everything looks good on the test document, give it a try on the “real” one. A few hangups could be: pasting into a draft vs. published, using a read token instead of read/write token, and not sending a content-type of application/json (this should be detected automatically, I think, but you can double-check in the Header tab). Good luck!
Another approach, if it’s only the array that is laborious to create (i.e., everything else can be plugged in quickly and easily) would be to duplicate the document and change the non-array stuff.

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?