Write Api a reference fields

Hi, I’m using the write api to migrate an entire blog from datoCMS to Builder.
Currently, I have already migrated the authors and categories (there are separated models).
When migrating the entire blog post, how could I write the ‘authors’ field (it’s a list of references to the author model) and the category (it’s a reference to the category model)?
I would have to filter to catch the respective category and authors, but then how can I write them in the API? I know how to write strings… but a reference type?

thanks

Hi @emiliano great question. So a reference would need to include the type, id and model. For for example, I have a demo space that has blog-article models that include a reference to an author:
https://cdn.builder.io/api/v2/content/blog-article?apiKey=e37b966ec695434bb21e97442a4a9f46&fields=id,name,data.author

The output here would be:

{
  "results": [
    {
      "id": "dccc48821a1648b0848861eada0c72ee",
      "name": "blog article testing",
      "data": {
        "author": {
          "@type": "@builder.io/core:Reference",
          "id": "fb859711e12a4d25ac3e3f8472da2e2e",
          "model": "author"
        }
      }
    },
    {
      "id": "8bd83690d3264673906ccd8713cc9cb7",
      "name": "testing header",
      "data": {
        "author": {
          "@type": "@builder.io/core:Reference",
          "id": "b9388e4b8e6a4a3db8340c601a1d550b",
          "model": "author"
        }
      }
    }
  ]
}

So yes, as you said you would probably need to make a call to the content API to pull the authors, and then filter to find the correct author name and pass their content ID and model name into the write API call for creating the blog posts. Does that answer your question? I would recommend trying it out with one or two posts to get it working correctly and then rolling it out across your entire content log.

Let me know how it goes or if you have any further questions!

Thanks for answering. So I would just pass an object like:

'@type': '@builder.io/core:Reference',
    id: id,
    model: 'blog-category-model'

Because this isn’t working for me…
It has to be a json? or an object?

I’ve also tried like this but it isn’t working…

{
    '@type': '@builder.io/core:Reference',
    'id': id,
    'model': 'blog-category-model'
}

My entire write API code consist in:

const toBuilder = await fetch('https://builder.io/api/v1/write/blog-model', {
    method: 'POST',
    headers: {
      Authorization: `Bearer my-private-key`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      name: element.title,
      data: {
        title: element.title,
        slug: element.slug,
        extract: element.extract,
        category: {
          '@type': '@builder.io/core:Reference',
          'id': id,
          'model': 'blog-category-model'
         } ,
        date: element.date,
        metaTitle: element.metaTitle,
        metaDescription: element.metaDescription,
        coverImage: cloudinaryImg,
        content: element.content.replace(/\n/g, '<br />')
      }
    })
  });

the id is okay, I already checked it. (I run a function to get all the builder categories and get the Id by filtering them)

Hi @TimG , could you help me with this? Thanks! Really appreciate your answers and you are a big help for me

Hi @emiliano when you say it doesn’t work for you what does that mean? Is it writing the content to builder but without the correct reference? Is it giving an error?

Do you have any examples in your space? What is the API key of the space you are working in , I didnt see any spaces with the relevant data when I tried to search for your store.

Hey, it writes all the content correct, except for the reference… the category field stays empty (avoid the authors field in the image)

thanks!

Hmm… I tried running with hard coded values, and it worked for me: Builder.io: Drag and drop page builder and CMS

Hope you don’t mind, we can delete once testing is done. What value are you passing as the ID? It should be the content id, so for the General category, the ID should be 1352e4da86234d4b9086eb77a1bfbedb, the same as the ID in the builder content URL

My example call:


fetch('https://builder.io/api/v1/write/blog-model', {
    method: 'POST',
    headers: {
      Authorization: `Bearer YOUR_PRIVATE_KEY`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      name: 'test',
      data: {
        title: 'test title',
        slug: 'test slug',
        extract: 'test extract',
        category: {
          '@type': '@builder.io/core:Reference',
          'id': '1352e4da86234d4b9086eb77a1bfbedb',
          'model': 'blog-category-model'
         } ,
      }
    })
  });

Thanks for all your help! I finally could do it!

1 Like

I also opened another question here about the same but with lists type… I couldn’t write them with the API, is there any similar secret for those? thanks