Creating Product w/NodeJs

This might be a long shot. But I am attempting to create a product from my API. I am able to create a product but at a very basic since with one variation. I am hoping that someone might be able to show me a json payload creating/adding just about every property you can when creating items. I have the following code in place now

const catalogObject = {
            idempotencyKey: `${newProduct.sku}-${Date.now()}`,
            object: {
                id: `#${newProduct.sku}`,
                type: 'ITEM',
                itemData: {
                    name: newProduct.name,
                    shortDescription: newProduct.shortDescription, 
                    description: newProduct.description,
                    abbr: slugify(newProduct.name),
                    category: newProduct.category,
                    productType: 'REGULAR',
                    variations: [
                        {
                            id: `#${newProduct.sku}-variation`,
                            type: 'ITEM_VARIATION',
                            itemVariationData: {
                                itemId: `#${newProduct.sku}`,
                                name: `${newProduct.name} Variation`,
                                priceMoney: {
                                    amount: Math.round(newProduct.price.value * 100),
                                    currency: 'USD'
                                },
                                sku: newProduct.sku,
                                inventoryAlertType: 'LOW_QUANTITY',
                                inventoryAlertThreshold: 5,
                                pricingType: 'FIXED_PRICING'
                            }
                        }
                    ]
                }
            },
            customAttributeValues: {
                'brand': newProduct.brand,
                'price_unit': newProduct.price.unit,
                'stock': newProduct.stock,
                'weight': newProduct.weight ? `${newProduct.weight.value} ${newProduct.weight.unit}` : '',
                'volume': newProduct.volume ? `${newProduct.volume.value} ${newProduct.volume.unit}` : '',
                'dimensions': newProduct.dimensions ? `${newProduct.dimensions.length} x ${newProduct.dimensions.width} x ${newProduct.dimensions.height} ${newProduct.dimensions.unit}` : '',
                'specifications': newProduct.specifications ? JSON.stringify(Object.fromEntries(Object.entries(newProduct.specifications))) : ''
            }
        };

While this code does create a product, it doesn’t seem to fully register all the properties and custom attributes I’m sending.

Could someone provide an example JSON payload/function that demonstrates how to include as many properties and custom attributes as possible when creating items through the Square API? I’d greatly appreciate a comprehensive example or any tips on what might be missing in my current setup.

Thank you!

:wave: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Add Custom Attributes
Build a Simple Catalog
Build and Manage a Simple Inventory

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

To create a product with as many properties and custom attributes as possible, you would need to include all the available fields in the CatalogObject instance. Here’s an example of how you might structure your payload:

const catalogObject = {
    idempotencyKey: `${newProduct.sku}-${Date.now()}`,
    object: {
        id: `#${newProduct.sku}`, // Square ID should start with # for custom objects
        type: 'ITEM',
        itemData: {
            name: newProduct.name,
            description: newProduct.description,
            abbreviation: slugify(newProduct.name),
            categoryId: newProduct.categoryId,
            taxIds: newProduct.taxIds,
            modifierListIds: newProduct.modifierListIds,
            variations: [
                {
                    id: `#${newProduct.sku}-variation`,
                    type: 'ITEM_VARIATION',
                    itemVariationData: {
                        itemId: `#${newProduct.sku}`,
                        name: `${newProduct.name} Variation`,
                        pricingType: 'FIXED_PRICING',
                        priceMoney: {
                            amount: Math.round(newProduct.price.value * 100),
                            currency: 'USD'
                        },
                        sku: newProduct.sku,
                        trackInventory: true,
                        inventoryAlertType: 'LOW_QUANTITY',
                        inventoryAlertThreshold: 5,
                        userData: newProduct.userData,
                        serviceDuration: newProduct.serviceDuration,
                        itemOptionValues: newProduct.itemOptionValues
                    }
                }
            ],
            productType: 'REGULAR',
            skipModifierScreen: false,
            itemOptions: newProduct.itemOptions,
            sortName: newProduct.sortName
        }
    }
};

In this example, I’ve added more fields to the itemData and itemVariationData objects. These include categoryId, taxIds, modifierListIds, trackInventory, userData, serviceDuration, itemOptionValues, productType, skipModifierScreen, itemOptions, and sortName.

Please note that some of these fields like categoryId, taxIds, modifierListIds, and itemOptionValues require IDs of existing catalog objects. You would need to create these objects separately using the Catalog API before you can reference them in your product creation payload.

Also, remember that the customAttributeValues field in your payload should be an object where the keys are the IDs of the custom attribute definitions and the values are CatalogCustomAttributeValue objects.

Please refer to the Square Catalog API documentation for more details on each of these fields and how to use them.

This answer was reviewed by @Bryan-Square.

1 Like