Nodejs BatchUpserts

I am trying to call the BatchUpsert method on the Catalog API from the Square API SDK. All of my operations so far have been successful, but now I am trying to do a batch operation, I am having issues and the SDK docs for node js don’t have many examples. I am calling the following method:

var body = new SquareConnect.BatchUpsertCatalogObjectsRequest({
    idempotency_key: uuidv4(),
    batches: [{
        objects: [{
            id: "#pack_size",
            type: "CUSTOM_ATTRIBUTE_DEFINITION",
            allowed_object_types: ["ITEM_VARIATION"],
            seller_visibility: "SELLER_VISIBILITY_READ_WRITE_VALUES",
            app_visibility: "APP_VISIBILITY_HIDDEN",
            custom_attribute_definition_data: {
                name: "PackSizeKgs",
                key: "pack_size",
                type: "NUMBER"

            }
        }]
    }]
});

apiInstance.upsertCatalogObject(body).then(function(data) {
  console.log('API called successfully. Returned data: ' + JSON.stringify(data));
}, function(error) {
  console.error(error);
});

I am getting the error back -

“INVALID_REQUEST_ERROR”,“code”:“EXPECTED_STRING”,“detail”:“Expected a string value.”,“field”:“idempotency_key”

uuidv4() should be returning a string, I have verified this by logging typeof uuidv4() which returns string and even tried manually typing the fixed string idempotency_key from square’s curl example.

Any ideas what I am doing wrong?

The BatchUpsertCatalogObjectsRequest takes two parameters: idempotency_key and batches. You’re currently passing one single parameter, an entire object (so it’s basically setting idempotency_key as an object, not a string). So, try updating to be something like:

var body = new SquareConnect.BatchUpsertCatalogObjectsRequest();
body.idempotency_key = uuid();
body.batches = [{
    objects: [{
        id: "#pack_size",
        type: "CUSTOM_ATTRIBUTE_DEFINITION",
        allowed_object_types: ["ITEM_VARIATION"],
        seller_visibility: "SELLER_VISIBILITY_READ_WRITE_VALUES",
        app_visibility: "APP_VISIBILITY_HIDDEN",
        custom_attribute_definition_data: {
            name: "PackSizeKgs",
            key: "pack_size",
            type: "NUMBER"

        }
    }]
}];

Finally, you’re calling upsertCatalogObject, so once you fix the above error you’d receive another error. Change this to batchUpsertCatalogObjects(body).then... instead.