Upsert Catalog Object

Hi All,

I’m a bit of a beginner in the grand scheme of things, and I’m using an unsupported language to do my integration, but I feel like something obvious is missing and after re-reading all the API documentation, I still have no idea what is going wrong.

Working off the API documentation, but with my own Item, I generate a JSON request such as:

curl https://connect.squareupsandbox.com/v2/catalog/object \
  -X POST \
  -H 'Square-Version: 2022-06-16' \
  -H 'Authorization: Bearer ACCESS_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
    "idempotency_key": "24bf222c-21a8-4bd2-9097-b7e7ec3d2f36",
    "object": {
      "type": "ITEM",
      "id": "#1298498081",
      "is_deleted": false,
      "present_at_all_locations": true,
      "item_data": {
        "description": "Grey Cat",
        "name": "Grey Cat",
        "available_online": true,
        "available_for_pickup": true,
        "category_id": "P4KZKJNAOWNT7JIU6GRTWNAL",
        "image_ids": [
          "HSVH4KFM2RWCAPUFOB2EWJ3T"
        ],
        "tax_ids": [
          "AUSSALESTAXMLQZ38K6X6A1N"
        ],
        "product_type": "REGULAR",
        "variations": [
          {
            "type": "ITEM_VARIATION",
            "id": "#1298main",
            "is_deleted": false,
            "item_variation_data": {
              "image_ids": [
                "HSVH4KFM2RWCAPUFOB2EWJ3T"
              ],
              "inventory_alert_type": "NONE",
              "item_id": "#1298498081",
              "name": "Grey Cat",
              "pricing_type": "FIXED_PRICING",
              "price_money": {
                "amount": 1989,
                "currency": "AUD"
              },
              "sellable": true,
              "stockable": true,
              "stockable_conversion": {
                "nonstockable_quantity": "0",
                "stockable_quantity": "1",
                "stockable_item_variation_id": "#1298main"
              },
              "inventory_alert_threshold": 0
            }
          }
        ]
      },
      "version": 0
    }
  }'

and I get the following error:

{
  "errors": [
    {
      "category": "INVALID_REQUEST_ERROR",
      "code": "BAD_REQUEST",
      "detail": "Bad request."
    }
  ]
}

BAD_REQUEST doesn’t really give me any guidance as to what the problem is, but I got this far by drilling through the various errors the API threw back at me. Without “item_data” I get a “Missing Item Data” error. Without a “variations”, I get a “Variation missing” error. If I continue down this path, I get various errors about the lack of category, lack of stockable_conversion, lack of inventory_alert_type, etc.

I tried going off the example given at https://developer.squareup.com/reference/square/catalog/upsert-catalog-object:

curl https://connect.squareup.com/v2/catalog/object \
  -X POST \
  -H 'Square-Version: 2022-06-16' \
  -H 'Authorization: Bearer ACCESS_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
    "idempotency_key": "af3d1afc-7212-4300-b463-0bfc5314a5ae",
    "object": {
      "id": "#Cocoa",
      "type": "ITEM",
      "item_data": {
        "abbreviation": "Ch",
        "description": "Hot Chocolate",
        "name": "Cocoa",
        "variations": [
          {
            "id": "#Small",
            "type": "ITEM_VARIATION",
            "item_variation_data": {
              "item_id": "#Cocoa",
              "name": "Small",
              "pricing_type": "VARIABLE_PRICING"
            }
          },
          {
            "id": "#Large",
            "type": "ITEM_VARIATION",
            "item_variation_data": {
              "item_id": "#Cocoa",
              "name": "Large",
              "pricing_type": "FIXED_PRICING",
              "price_money": {
                "amount": 400,
                "currency": "USD"
              }
            }
          }
        ]
      }
    }
  }'

But it appears to be missing a heap of the fields I’ve been told are required by the API.

Sorry if this seems rambling, but I’ve spent about 6 hours trying to troubleshoot a JSON object and the request, and I have no more information on what’s missing. I’ve tried looking for similar queries, but there has been nothing obviously analogous to my situation.

This can be closed. It turns out that I’m just a bad developer. The demo does indeed work, and it was a secret idiosyncrasy to do with the way Golang deals with nil values and “OmitEmpty”.

For anyone who trips over this and is trying to do the dumb things I was doing, make sure you use pointers to your structs you want to be omitted. A struct is always initialized if the structure defines it. But pointers aren’t, and require you to initialize them. They convert to JSON properly, but they’re also omitted if they aren’t initialized. The key one that was killing this was:

Stockable_Conversion      *CatalogStockConversion                   `json:"stockable_conversion,omitempty"`

Without the “*”, the struct is always in place. Adding that single “*” allows the struct to become a pointer, which fixes the omitempty if you don’t initialize it, fixing the universe. And it only took 6 hours and 12 minutes…

Glad to hear you figured it out! :slightly_smiling_face: