👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

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?
Jun 25, 2021, 3:34 PM
Existing
Jun 25, 2021, 4:48 PM
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.
Jun 25, 2021, 5:10 PM
Any sample? How to get document id and that position?
Jun 25, 2021, 5:20 PM
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!
Jun 25, 2021, 6:08 PM
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.
Jun 25, 2021, 6:12 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?