# Exporting structured data models to use on other spaces

**URL:** <https://forum.builder.io/t/exporting-structured-data-models-to-use-on-other-spaces/5453>\
**Category:** General\
**Created:** [April 29, 2024, 2:55am UTC](https://forum.builder.io/t/exporting-structured-data-models-to-use-on-other-spaces/5453 "2024-04-29T02:55:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhs129](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@jhs129](https://forum.builder.io/u/jhs129)\
**Post date:** [April 29, 2024, 2:55am UTC](https://forum.builder.io/t/exporting-structured-data-models-to-use-on-other-spaces/5453/1 "2024-04-29T02:55:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![anon43891756](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@anon43891756](https://forum.builder.io/u/anon43891756)\
**Post date:** [April 30, 2024, 5:13pm UTC](https://forum.builder.io/t/exporting-structured-data-models-to-use-on-other-spaces/5453/2 "2024-04-30T17:13:25Z")

</div>

Hi @jhs129,

You can use [content API](https://www.builder.io/c/docs/content-api) to import and [write API](https://www.builder.io/c/docs/write-api) to upload. You can also use the download JSON option [Download/uploading content](https://forum.builder.io/t/download-uploading-content/280)

---

<div class="post-metadata">

**Author:** ![jhs129](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@jhs129](https://forum.builder.io/u/jhs129)\
**Post date:** [May 14, 2024, 6:38pm UTC](https://forum.builder.io/t/exporting-structured-data-models-to-use-on-other-spaces/5453/3 "2024-05-14T18:38:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)\
**Post date:** [July 17, 2024, 12:45pm UTC](https://forum.builder.io/t/exporting-structured-data-models-to-use-on-other-spaces/5453/5 "2024-07-17T12:45:26Z")

</div>

Hello @jhs129,

I apologize for the earlier provided script example, which appears to be non-functional. You can utilize the builder Admin API to retrieve all the structured data models from one space and subsequently import those models into another space.

Here’s a verified script example that demonstrates the import and export of data models from one organization to another:

```auto
const { createAdminApiClient } = require("@builder.io/admin-sdk");

// Admin SDK for the source space
const adminSDKSource = createAdminApiClient("source-space-private-api-key");

// Admin SDK for the target space
const adminSDKTarget = createAdminApiClient("target-space-private-api-key");

// Function to get data models from the source space
async function getDataModels() {
  const models = await adminSDKSource.query({
    models: {
      name: true,
      id: true,
      fields: true,
      kind: true,
    },
  }).then((res) => res.data.models);
  
  return models.filter(model => model.kind === 'data');
}

// Function to recursively map fields, including subfields if present
function mapFields(fields) {
  return fields.map(field => {
    const mappedField = {
      type: field.type,
      "@type": "@builder.io/core:Field",
      required: field.required,
      hideFromFieldsEditor: field.hideFromFieldsEditor,
      name: field.name,
      helperText: field.helperText,
    };

    if (field.subFields && Array.isArray(field.subFields)) {
      mappedField.subFields = mapFields(field.subFields);
    }

    return mappedField;
  });
}

// Function to add data models to the target space
async function addDataModelToTarget(targetModel) {
  try {
    const response = await adminSDKTarget.chain.mutation.addModel({
      body: {
        designerVersion: 1,
        name: targetModel.name,
        kind: "data",
        ownerId: "Target Space public API Key", // You might want to replace this with the correct ownerId for the target space
        examplePageUrl: "http://localhost:3000/",
        helperText: targetModel.helperText || "Data model added from source space",
        pathPrefix: "/",
        fields: mapFields(targetModel.fields), // Use the recursive mapping function here
        // Add other relevant fields if needed
      },
    }).execute({
      id: true,
      name: true,
      kind: true,
    });

    console.log("Create model", response);
  } catch (error) {
    console.error("Error creating model:", error);
  }
}

(async () => {
  const dataModels = await getDataModels();

  for (const model of dataModels) {

    console.log("Processing model:", model.name);
    await addDataModelToTarget(model);
  }

  console.log("Finished processing all data models.");
})();

```

To know more, you can visit the below links

> **[Builder Admin API - Builder.io](https://www.builder.io/c/docs/admin-graphql-api)**
>
> Explore Builder.io's Admin API, a GraphQL-based tool for backend servers to perform administrative tasks and manage content programmatically.

> **[Admin GraphQL Schema - Builder.io](https://www.builder.io/c/docs/admin-graphql-schema)**
>
> Admin GraphQL schema documentation, including queries, mutations, object types, input types, and scalars, along with examples of how to use them.

> **[builder/packages/admin-sdk at main · BuilderIO/builder](https://github.com/BuilderIO/builder/tree/main/packages/admin-sdk)**
>
> Drag and drop headless CMS for React, Vue, Svelte, Qwik, and more - BuilderIO/builder
