Magento – Magento2 Adding product attribute option via API

apimagento2

I am adding new colors to the colors attribute via API. This works fine, but I want to send all my options in 1 request. Now I have to do multiple POST request to rest/V1/products/attributes/color/options

This is my code

protected $resourcePath = 'rest/V1/products/attributes';

foreach($options as $option) {
$optionData = [];

$optionData = [
    AttributeOptionInterface::LABEL => $option,
    AttributeOptionInterface::SORT_ORDER => 100,
    AttributeOptionInterface::IS_DEFAULT => false,
    AttributeOptionInterface::STORE_LABELS => [
        [
            AttributeOptionLabelInterface::LABEL => $option,
            AttributeOptionLabelInterface::STORE_ID => 1,
        ],
    ],
];

    $this->addColorAttribute($optionData);
}

public function addColorAttribute($optionData)
{
    $attributeCode = 'color';

    $serviceInfo = [
        'rest' => [
            'resourcePath' => $this->resourcePath . '/' . $attributeCode .'/options',
            'httpMethod' => Request::HTTP_METHOD_POST,
        ]
    ];

    return $this->client->request($serviceInfo,
        [
            'attributeCode' => $attributeCode,
            'option' => $optionData,
        ]
    );
}

How could I send all my $optionData to the API in 1 request so all the options are addded to the product attribute color ?

Best Answer

Send them all into either put or Post at /V1/products/attributes/{attributeCode}

and it will create them,

FYI im having problems updating the labels later, which looks like its not possible via the API right now