Error when trying to add custom attribute "Unknown custom attribute definition"

I am using the below code to add items to a catalog. Each item has the same custom attribute"Brand"with different values obviously. I am getting this error

 {
  "errors": [
    {
      "category": "INVALID_REQUEST_ERROR",
      "code": "BAD_REQUEST",
      "detail": "Unknown custom attribute definition with id `#brand` referred to in object with id `#cat301315-BLK-L`"
    }
  ]
}
$uniqid = uniqid();

			$priceInCents = $product['price'] * 100;

			// Create the Money object for the price
			$price_money = new \Square\Models\Money();
			$price_money->setAmount((int) $priceInCents); // Amount in cents, e.g., $10.00 should be 1000
			$price_money->setCurrency('USD'); // Set the currency, e.g., USD


			//Brand custom attribute
			$allowed_object_types = ['ITEM', 'ITEM_VARIATION'];
			$custom_attribute_definition_data = new \Square\Models\CatalogCustomAttributeDefinition('STRING', 'Brand', $allowed_object_types);
			$custom_attribute_definition_data->setType('STRING');
			$custom_attribute_definition_data->setKey('brand');
			$custom_attribute_definition_data->setName('Brand');
			$custom_attribute_definition_data->setAppVisibility('APP_VISIBILITY_READ_WRITE_VALUES');
			$custom_attribute_definition_data->setSellerVisibility('SELLER_VISIBILITY_READ_WRITE_VALUES');
			$custom_attribute_definition_data->setAllowedObjectTypes($allowed_object_types);

			$catalog_object = new \Square\Models\CatalogObject('CUSTOM_ATTRIBUTE_DEFINITION', '#brand');
			$catalog_object->setType('CUSTOM_ATTRIBUTE_DEFINITION');
			$catalog_object->setCustomAttributeDefinitionData($custom_attribute_definition_data);

			$catalog_custom_attribute_value = new \Square\Models\CatalogCustomAttributeValue();
			$catalog_custom_attribute_value->setName('Brand');
			$catalog_custom_attribute_value->setStringValue($product['brand']);
			$catalog_custom_attribute_value->setCustomAttributeDefinitionId('#brand');
			$catalog_custom_attribute_value->setType('STRING');
			$catalog_custom_attribute_value->setKey('brand');

			$custom_attribute_values = ['brand' => $catalog_custom_attribute_value];


					
			
			// Create the CatalogItemVariation object
			$item_variation_data = new \Square\Models\CatalogItemVariation();
			$item_variation_data->setItemId('#cat' . $product['sku']); // Temporary ID for new items
			$item_variation_data->setSku($product['sku']);
			$item_variation_data->setName($product['name']);
			$item_variation_data->setPricingType('FIXED_PRICING');
			$item_variation_data->setPriceMoney($price_money);
			$item_variation_data->setUpc($product['UPC']);
			
			// Create the CatalogObject for the item variation
			$catalog_object_variation = new \Square\Models\CatalogObject('ITEM_VARIATION', '#' . $product['sku']);
			$catalog_object_variation->setItemVariationData($item_variation_data);
			
			// Create the CatalogItem object
			$item_data = new \Square\Models\CatalogItem();
			$item_data->setName($product['name']);
			$item_data->setDescription("description"); // Assuming you have a description field
			$item_data->setAbbreviation($product['sku']);
			$item_data->setVariations([$catalog_object_variation]);
			$item_data->setTaxIds($this->getTaxIdString($product));	
			
			
			
			
			// Create the CatalogObject for the item
			$catalog_object_item = new \Square\Models\CatalogObject('ITEM', '#cat' . $product['sku'],);
			$catalog_object_item->setCustomAttributeValues($custom_attribute_values);
			$catalog_object_item->setItemData($item_data);
			


			$objects[] = $catalog_object_item;

This is the request taken from the dashboard

"batches": [
    {
      "objects": [
        {
          "type": "ITEM",
          "id": "#cat301315-BLK-L",
          "custom_attribute_values": {
            "brand": {
              "name": "Brand",
              "string_value": "Salty Crew",
              "custom_attribute_definition_id": "#brand",
              "type": "STRING",
              "key": "brand"
            }
          },
          "item_data": {
            "name": "75035012W Seafarer 1-Piece Blk-L",
            "description": "description",
            "abbreviation": "301315-BLK-L",
            "tax_ids": [],
            "variations": [
              {
                "type": "ITEM_VARIATION",
                "id": "#301315-BLK-L",
                "item_variation_data": {
                  "item_id": "#cat301315-BLK-L",
                  "name": "75035012W Seafarer 1-Piece Blk-L",
                  "sku": "301315-BLK-L",
                  "upc": "804046559023",
                  "pricing_type": "FIXED_PRICING",
                  "price_money": {
                    "amount": 8995,
                    "currency": "USD"
                  }
                }
              }
            ]
          }
        },

I have been trying to sort this out for hours. Please help!

THanks
Don

: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

Update Catalog Objects
Call the SearchCatalogItems Endpoint
Use Custom Attributes for Merchants

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.

The error message you’re seeing is indicating that the custom attribute definition with the id #brand is not found. When you’re creating a custom attribute for a catalog object, you need to ensure that the custom attribute definition already exists in the catalog.

In your code, you’re creating a CatalogCustomAttributeDefinition object and a CatalogObject instance for the definition, but you’re not actually making a request to the UpsertCatalogObject endpoint to add the definition to the catalog before you try to use it in your item.

You need to make a separate call to the UpsertCatalogObject endpoint to create the custom attribute definition in your catalog before you can use it in your item. Once the definition is successfully created, you can then use its id (which will be a Square-generated id, not the temporary #brand id) in your custom_attribute_values for your items.

Remember to replace the #brand in setCustomAttributeDefinitionId('#brand') with the actual id of the custom attribute definition returned by the UpsertCatalogObject endpoint.

This answer was reviewed by @Bryan-Square.