Exporting structured data models to use on other spaces

I have a structured data model that I have built on one site and I want to be able to use in in another space. How can I export / import the definition of this structured data model from one space to another?

Hi @jhs129,

You can use content API to import and write API to upload. You can also use the download JSON option Download/uploading content

Hi @garima I see how you can do that for page content in the page editor but what about for structured data?

Hello @jhs129,

Builder.io currently doesn’t provide a direct “export/import” button for structured data models in the UI, but you can handle this by using the Builder.io APIs to programmatically export and import the model definitions.

Here’s a step-by-step guide to achieve this:

  1. Export the Data Model from the Source Space
  2. Import the Data Model into the Destination Space

Here is a single script that performs both actions sequentially:

const axios = require('axios');

const sourceSpaceId = 'YOUR_SOURCE_SPACE_ID';
const destinationSpaceId = 'YOUR_DESTINATION_SPACE_ID';
const apiKey = 'YOUR_BUILDER_IO_API_KEY';

const modelName = 'MODEL_NAME_TO_EXPORT';

const fetchModel = async () => {
  try {
    const response = await axios.get(`https://builder.io/api/v1/write/models/${modelName}`, {
      headers: {
        Authorization: `Bearer ${apiKey}`
      }
    });
    const modelSchema = response.data;
    console.log('Exported Model Schema:', JSON.stringify(modelSchema, null, 2));
    return modelSchema;
  } catch (error) {
    console.error('Error fetching model schema:', error);
  }
};

const importModel = async (modelSchema) => {
  try {
    const response = await axios.post(`https://builder.io/api/v1/write/models`, modelSchema, {
      headers: {
        Authorization: `Bearer ${apiKey}`
      }
    });
    console.log('Model successfully imported:', response.data);
  } catch (error) {
    console.error('Error importing model schema:', error);
  }
};

const migrateModel = async () => {
  const modelSchema = await fetchModel();
  if (modelSchema) {
    await importModel(modelSchema);
  }
};

migrateModel();