DatoCMS API, adding blocks inside a model

Describe the issue:

I’m trying to migrate data from another cms to DatoCMS. The models I want to migrate data to have to contain some blocks like this one:

The AccordianList has ID 2784630

I have read the internet to the end without finding any answer on how to do this :roll_eyes:

My code looks like this:

try {
          const article = {
            item_type: { 
              type: "item_type", 
              id: itemTypeArticle 
            },
            title: child.title,
            body,
            slug: child._slug,
            seo: {
              title: "seo tittel",
              description: "seo beskrivelse",
            }
          };
          ...
} ....

and the body object looks like this:
const body = {
    "schema": "dast",
    "document": {
      "type": "root",
      "children": [
        {
          "item": "2784630",
          "type": "block"
        },
        {
          "level": 2,
          "children": [
            {
              "value": "Hva er spillegrenser?",
              "type": "span"
            }
          ],
          "type": "heading"
        },
        {
          "children": [
            {
              "value": "Spillegrense er en personlig grense for hvor mye penger du kan tape på pengespill hos Norsk Tipping i løpet av en dag eller en måned.",
              "type": "span"
            }
          ],
          "type": "paragraph"
        },
      ]
    },
  }

The error I get is this:

Error: POST https://site-api.datocms.com/items: 422 Unprocessable Content

[
  {
    "id": "ff3436",
    "type": "api_error",
    "attributes": {
      "code": "INVALID_FIELD",
      "details": {
        "field": "body",
        "code": "INVALID_FORMAT",
        "message": "Cannot use IDs instead of block records objects when creating an Item"
      }
    }
  }
]

Can anyone please tell me how to do this the correct way?


(Moderator note: Fixed code formatting)

We ended up answering this over email, so I’m going to close it here :slight_smile: Please feel free to start a new thread or email us (just one or the other, please) if you have any further follow-ups!

If anyone else is wondering, @m.finamor answered the question:

The issue is in the part:

       {
          "type": "block",
          "item": "2784630"
        },

You can’t pass a block using only its ID when creating a new structured text value from an empty field.

You need to create the block and pass it to the field:

{
        "type": "block",
        "item": {
          "type": "item",
          "attributes": {
            // ... put your block fields values here
          },
          "relationships": {
            "item_type": {
              "data": {
                // the block model
                "id": "435822", //the block model ID
                "type": "item_type",
              }
            }
          },
        }
      }
1 Like