Magento – How to add product image when creating via rest api

magento2product-images

This is how I creating product.

{
  "product": {
    "attribute_set_id": 4,
    "type_id": "simple",
    "sku": "B201-SKU",
    "name": "B201",
    "price": 25,
    "status": 1,
    "custom_attributes": {
      "description": "Heavy Duty Brake Cables",
      "meta_description": "Some describing text"
    }
  }
}

Best Answer

You need an additional POST with the route :

http://YOUR_MAGENTO_ADDRESS/index.php/rest/V1/products/B201-SKU/media

The basic template JSON would be something like:

{
    "entry": {
        "media_type": "image",
        "label": "Image",
        "position": 1,
        "disabled": false,
        "types": [
            "image",
            "small_image",
            "thumbnail"
        ],
        "content": {
            "base64_encoded_data": YOU_NEED_YOUR_IMAGE_BASE64_ENCODED,
            "type": "image/png",
            "name": "choose_any_name.png"
        }
    }
}

After that your previously added product B201-SKU, will have the associated image.

Related Topic