Step 1. Plan the product

To create a configurable product programmatically, you'll need to know the following:

Since this tutorial uses the sample data, we can take advantage of the options that the Top attribute set provides. This attribute set contains attributes that describe the fabric, sleeve length, and other characteristics that are specific to clothing. It also includes EAV attributes such as size and color, which are commonly available to all types of physical products.

The size of the t-shirt will be the configurable aspect of this product. Therefore, we'll create a simple product for each size (Small, Medium, and Large).

Define product characteristics

The following table lists the general characteristics of men's t-shirt we're creating. These items are among those listed on the New Product page in Admin when the Top attribute set is selected.

Characteristic
Description
Attribute Set
Top
Product Name
Champ Tee
SKU
MS-Champ
Price
25.00
Tax Class
Taxable Goods
Weight
0.5
Categories
Men, Tops, Tees
Visibility
Catalog, Search
Material
LumaTech
Pattern
Graphic Print
Color
Gray
Size
Configurable in small, medium, or large
Description
The Champ Tee keeps you cool and dry while you do your thing. Let everyone know who you are by adding your name on the back for only $10.

A merchant typically provides the product name, SKU, price, weight, and description. The other characteristics are defined by the system.

Find the system-defined values

We'll make several calls to find the values needed to create the product

Get the attribute set ID

The sample data provides multiple attribute sets, including Default, Top, and Bottom. To assign the Top attribute set to the product, we need to know the corresponding attribute_set_id.

Use the following call to search for the attribute set named Top and entity_type_id equals to 4. By default, the catalog_product entity has the entity_type_id value as 4.

Endpoint:

GET <host>/rest/<store_code>/V1/eav/attribute-sets/list?
searchCriteria[filter_groups][0][filters][0][field]=attribute_set_name&
searchCriteria[filter_groups][0][filters][0][value]=Top&
searchCriteria[filter_groups][0][filters][0][condition_type]=eq&
searchCriteria[filter_groups][1][filters][0][field]=entity_type_id&
searchCriteria[filter_groups][1][filters][0][value]=4&
searchCriteria[filter_groups][1][filters][0][condition_type]=eq

Response:

The attribute_set_id for the Top attribute set is 9.

{
    "items": [
        {
            "attribute_set_id": 9,
            "attribute_set_name": "Top",
            "sort_order": 0,
            "entity_type_id": 4
        }
    ],
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "attribute_set_name",
                        "value": "Top",
                        "condition_type": "eq"
                    }
                ]
            },
            {
                "filters": [
                    {
                        "field": "entity_type_id",
                        "value": "4",
                        "condition_type": "eq"
                    }
                ]
            }
        ]
    },
    "total_count": 1
}

Get the list of attributes defined in an attribute searchCriteria

Use the GET V1/products/attribute-sets/:attributeSetId/attributes call to return information about the attributes defined in the Top attribute set.

Endpoint:

GET <host>/rest/default/V1/products/attribute-sets/9/attributes

Response:

[
    {
        "attribute_id": 141,
        "attribute_code": "size",
        "frontend_input": "select",
        "entity_type_id": "4",
        "is_required": false,
        "options": [
            {
                "label": " ",
                "value": ""
            },
            {
                "label": "Small",
                "value": "168"
            },
            {
                "label": "Medium",
                "value": "169"
            },
            {
                "label": "Large",
                "value": "170"
            }
        ],
        "is_user_defined": true,
        "default_frontend_label": "Size",
        "frontend_labels": [],
        "backend_type": "int",
        "source_model": "Magento\\Eav\\Model\\Entity\\Attribute\\Source\\Table",
        "default_value": "",
        "is_unique": "0",
        "validation_rules": []
    }
]

The response contains almost 3,000 lines and the above is a part of the response from the endpoint. The following table provides a summary of the attributes that are relevant in this tutorial.

Admin label
Selected value
Attribute ID
attribute_code
Attribute value
Tax Class
Taxable Goods
132
tax_class_id
2
Visibility
Catalog, Search
99
visibility
4
Material
LumaTech
136
material
148
Pattern
Graphic Print
152
pattern
196
Color
Gray
93
color
52
Size
Not applicable
141
size
168 (small), 169 (medium), 170 (large)
data-variant=warning
data-slots=text
The attribute ID and value numbers might be different on your installation. Check the values carefully before using them in your calls.

Get the list of category values

You must assign the product to one or more categories to enable customers to find the product by browsing. We'll assign the Champ Tee to the Men, Tops, and Tees categories.

Use the following call to search for all categories (id is greater than or equal to 0).

Endpoint:

GET <host>/rest/default/V1/categories?
searchCriteria[filter_groups][0][filters][0][field]=id&
searchCriteria[filter_groups][0][filters][0][value]=1&
searchCriteria[filter_groups][0][filters][0][condition_type]=gte

Response:

{
    "id": 2,
    "parent_id": 1,
    "name": "Default Category",
    "is_active": true,
    "position": 1,
    "level": 1,
    "product_count": 0,
    "children_data": [
        {
            "id": 11,
            "parent_id": 2,
            "name": "Men",
            "is_active": true,
            "position": 1,
            "level": 2,
            "product_count": 1,
            "children_data": []
        }
    ]
}

Note that women's tops and tees have different IDs than men's tops and tees. The values for men's clothing are:

Verify this step

At this point, we're gathering information, so there is nothing to verify.