Site Backups / Recovery

I have been building a site for a client and now need to migrate it to their account. How can I back up all of the content in a space and import it into another space? I have seen how you can use the content api to download json files of individual content items but that doesn’t really work for a bulk operation like this.

Please fill out as many of the following questions as possible so we can help you promptly! If you don’t know how to answer or it does not apply, feel free to remove it or leave it blank.

Builder content link
e.g. Builder.io: Visual Development Platform

Builder public api key
go to Builder.io: Visual Development Platform and copy your PUBLIC api key

What are you trying to accomplish
e.g. I want to integrate builder on a landing page

Screenshots or video link
Screenshots of your issue, or even better a link to a video of what is happening or what you are trying to accomplish. We love using loom for that sort of thing

Code stack you are integrating Builder with
e.g. NextJS, react, Shopify

Reproducible code example
If you are having integration errors, please link to codesandbox or copy and paste your problematic code here. The more detail the better!

Hello @jhs129,

You can either use cli to clone your space and then migrate the cloned space to the client organization

Or if you prefer to use the Content API for a more automated bulk operation, you’ll need to write a script to download all content items and then upload them to the target space. Below are the general steps:

Export Content via API

  1. Get All Content Items: Use the GET /api/v1/content endpoint to retrieve a list of all content items.
  2. Download Content in JSON: Loop through each content item ID and download the content using the GET /api/v1/content/{model}?query={} endpoint.

Import Content via API

  1. Create Content Items: Use the POST /api/v1/content endpoint to create new content in the target space with the downloaded JSON data.

Here is a pseudo-code example to illustrate the process:

const axios = require('axios');
const fs = require('fs');

// Define API Keys
const sourceSpaceAPIKey = 'SOURCE_SPACE_API_KEY';
const targetSpaceAPIKey = 'TARGET_SPACE_API_KEY';

// Get All Content
async function getAllContent(apiKey) {
  const response = await axios.get('https://cdn.builder.io/api/v1/content/model-name', {
    headers: {
      Authorization: `Bearer ${apiKey}`
    }
  });
  return response.data.results;
}

// Export Content
async function exportContent(apiKey) {
  const contentList = await getAllContent(apiKey);
  fs.writeFileSync('backup.json', JSON.stringify(contentList, null, 2));
}

// Import Content
async function importContent(apiKey) {
  const contentList = JSON.parse(fs.readFileSync('backup.json'));
  for (const item of contentList) {
    await axios.post('https://cdn.builder.io/api/v1/content', item, {
      headers: {
        Authorization: `Bearer ${apiKey}`
      }
    });
  }
}

// Execute Export and Import
(async () => {
  await exportContent(sourceSpaceAPIKey);
  await importContent(targetSpaceAPIKey);
})();

Make sure to test the import/export process with a small amount of content first to verify that everything works as expected.

Hope this helps!

Thanks,

Thanks Manish! A couple of quick additional questions:

1.) Is it possible to do this import / export with the JavaScript SDK (i.e. using the builder object)
2.) What is the recommended approach when you are talking about backing up and restoring the same space (say you had to do a roll back or something)?

Finally, I think it would be a great thing for you to add this as a formal feature for admins. Should I add it as a feature request?

Regards,
John

Hi Manish-

I the instructions you provided to import / export worked but I am realizing that this did not export the assets in the asset library. I do however still have references to assets the old organization / space that still render. How can I do this export / import but also migrate the assets and the references to them?

Here is an example of a structured data object that had a reference to an asset that migrated but is still pointing to assets in the old instance:

Referencing: https://cdn.builder.io/o/assets%2Fa351bba0694846ec8a75c05e9fefbc69%2F8c53207b08c14796b1834bed72c088df?alt=media&token=9390ffb6-fd8c-4975-8e9d-66af72f89e06&apiKey=a351bba0694846ec8a75c05e9fefbc69

Org: 6217d9c964384f8c82dd3988f59555ef

Regards,
John

Hello @jhs129,

To export assets from the assets library, you will need to use the upload API and then utilize the response object to update the asset links in your export body.