Help with creating referenced fields using API

Hi

Iā€™m trying to create a new content record (a country name) and link it to another content record (a continent) using the API.

Iā€™m getting the following error message:
Failed to create country ā€˜Nigeriaā€™: {ā€œdataā€:[{ā€œidā€:ā€œ7634b9ā€,ā€œtypeā€:ā€œapi_errorā€,ā€œattributesā€:{ā€œcodeā€:ā€œINVALID_FORMATā€,ā€œdetailsā€:{ā€œmessagesā€:[ā€œ#/data/relationships: failed schema #/definitions/item/links/3/schema/properties/data/properties/relationships: "continent" is not a permitted key.ā€]}}}]}

Iā€™m using an HTTP POST request with the json structure shown below. Is this json the correct format for what Iā€™m trying to do?

Many thanks

Tom

data = {
        "data": {
            "type": "item",
            "attributes": {
                "country": country_name  # Matches 'country' field in your Country model
            },
            "relationships": {
                "item_type": {
                    "data": {
                        "type": "item_type",
                        "id": COUNTRY_MODEL_ID,  # ID of the Country model
                    }
                },
                "continent": {  # 'continent' field in the country content model
                    "data": {
                        "type": "item",  # The 'item' type for the linked continent
                        "id": continent_id,  # ID of the continent being linked
                    }
                },
            },
        }
    }

[Moderator note: Applied proper JSON formatting]

Hi @TD_Travel,

Welcome to the forum!

Your JSON is almost right :slight_smile: Could you please try this instead:

{
  "data": {
    "type": "item",
    "attributes": {  // These are the record fields
      "country": "New Zealand", // Single-line string
      "continent": "KOa9I8zMS4qB3Sgb8luQAg" // ID of the referenced continent, in this case "Zealandia"
    },
    "relationships": { // This tells our API what you're creating
      "item_type": {
        "data": {
          "type": "item_type",
          "id": "fI3EdreaTJ6Xp9UKmZIqTQ" // ID of "country" model, NOT "continent"
        }
      }
    }
  }
}

Generally speaking:

  • Relationships require the record ID of the reference
  • They are a property of the relationship field (i.e. attributes.continent), not the relationships property (I know itā€™s confusing, sorry!)
  • The relationships are basically just metadata, and not used to actually link records to each other. They more accurately describe schema relationships (i.e., it tells our API that this request is meant for the country model). To link records to each other, you specify them in fields instead.

PS Have you already tried our JS CMA client? https://www.datocms.com/docs/content-management-api/using-the-nodejs-clients

Compared to making raw HTTP calls, the JS client handles a lot of the ambiguities for you. The docs for the client are also a lot better for now (weā€™re slowly working on the HTTP ones, but generally the client is a lot easier to use so we try to focus on that).

Hope that helps! Please let me know if anything was unclear.

Also, protip: The Dato UI itself uses the CMA. So if you ever are unclear about how something is done, just open the network tab in your browser inspector and record the XHR calls right before you hit ā€œsaveā€.

Like if you make a new country record in the admin area UI, link it to continent, and then open the network tab before you hit Save, youā€™ll see the exact CMA request needed to create that record. Then you can export it as curl (for Postman/Curl/etc) or fetch (for your backend) and modify accordingly.

Or just ask us! Happy to explain anything and/or update docs :slight_smile:

Hi Roger - Thanks so much for the support and my import script using the API now works. I will definitely check out the JS CMA client!

1 Like