# Add Product images through Admin API

**URL:** <https://forum.shopware.com/t/add-product-images-through-admin-api/64359>\
**Category:** Programming (EN)\
**Created:** [30. Januar 2020 um 11:29 UTC](https://forum.shopware.com/t/add-product-images-through-admin-api/64359 "2020-01-30T11:29:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![WBKNL](https://avatars.discourse-cdn.com/v4/letter/w/e36b37/32.png) [@WBKNL](https://forum.shopware.com/u/WBKNL)\
**Post date:** [30. Januar 2020 um 11:29 UTC](https://forum.shopware.com/t/add-product-images-through-admin-api/64359/1 "2020-01-30T11:29:16Z")

</div>

I’m adding products through the Admin API but I can’t find anything on how to add product images. Can anybody explain to me how I can add product images through the api?

---

<div class="post-metadata">

**Author:** ![oliverriske](https://avatars.discourse-cdn.com/v4/letter/o/ebca7d/32.png) [@oliverriske](https://forum.shopware.com/u/oliverriske)\
**Post date:** [30. Januar 2020 um 11:49 UTC](https://forum.shopware.com/t/add-product-images-through-admin-api/64359/2 "2020-01-30T11:49:15Z")

</div>

Folgende Reihenfolge:

1. Post um eine MediaID zu erhalten

2. Post um Bild hochzuladen

3. Bei Product Post MediaID und ggf. CoverID in den payload aufnehmen

&nbsp;public function uploadProductImage($imageUrl, $imageName)  
&nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; try {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-\>log-\>info(„Connector/Shopware-\>uploadProductImage: " . $imageName);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mediaId = null;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($this-\>cacheMediaFolder[$this-\>productMediaFolder][„id“]) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data = [„mediaFolderId“ =\> $this-\>cacheMediaFolder[$this-\>productMediaFolder][„id“]];  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $url = $this-\>baseUrl . $this-\>apiEndpoints[‚media‘];  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $response = $this-\>postData($url, $data);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mediaId = $response-\>id;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($mediaId) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data = [  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; „url“ =\> $imageUrl,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $image = explode(“.", $imageName);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $url = $this-\>baseUrl . ‚/api/v1/\_action/media/‘ . $mediaId . ‚/upload?extension=‘ . $image[1] . ‚&fileName=‘ . $image[0];  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $response = $this-\>postData($url, $data);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!$response) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-\>log-\>error(  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new \Exception(„Upload Error try to delete MediaID: " . $mediaId . " for: " . $imageUrl)  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $url = $this-\>baseUrl . $this-\>apiEndpoints[‚media‘];  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-\>deleteData($url, $mediaId);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; &nbsp; } catch (\Exception $exception) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-\>log-\>error($exception);  
&nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; }  
&nbsp; &nbsp; public function postData($url, $data, $returnResponse = true)  
&nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; try {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($returnResponse) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (strpos($url, „?“) === false) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $url = $url . „?\_response=true“;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $url = $url . „&\_response=true“;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $token = $this-\>getToken();  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $client = $this-\>getClient();  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $response = $client-\>request(  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ‚POST‘,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $url,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ‚headers‘ =\> [  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ‚Content-Type‘ =\> ‚application/json‘,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ‚charset‘ =\> ‚utf-8‘,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ‚Accept‘ =\> ‚application/json‘,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ‚Authorization‘ =\> 'Bearer ’ . $token,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ‚body‘ =\> json\_encode($data),  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($returnResponse) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data = json\_decode($response-\>getBody()-\>getContents());  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!$data) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $location = $response-\>getHeader(„Location“)[0];  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $id = explode(“/", $location);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $id = $id[sizeof($id) - 1];

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (object)[‚id‘ =\> $id];  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $data-\>data;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $location = $response-\>getHeader(„Location“)[0];  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $id = explode("/", $location);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $id = $id[sizeof($id) - 1];

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $id;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; &nbsp; } catch (\Exception $exception) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this-\>log-\>error($exception);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;  
&nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; }  
&nbsp;

Product Payload:

$data = [  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; „active“ =\> true,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; „isCloseout“ =\> true,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; „taxId“ =\> $tax[„id“],  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; „productNumber“ =\> $product[‚identifier‘],  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; „name“ =\> $values[‚name‘][0][‚data‘],  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; „description“ =\> $values[‚beschreibung‘][0][‚data‘],  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];

$data[‚media‘] = ;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($productImages as $index =\> $productImage) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $dataImage = [  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; „mediaId“ =\> $productImage[‚id‘],  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; „position“ =\> $index,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($index == 0) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data[‚coverId‘] = md5($product[„identifier“] . $productImage[‚id‘], false);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $dataImage[„id“] = $data[‚coverId‘];  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data[‚media‘] = $dataImage;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp;

---

<div class="post-metadata">

**Author:** ![WBKNL](https://avatars.discourse-cdn.com/v4/letter/w/e36b37/32.png) [@WBKNL](https://forum.shopware.com/u/WBKNL)\
**Post date:** [30. Januar 2020 um 12:21 UTC](https://forum.shopware.com/t/add-product-images-through-admin-api/64359/3 "2020-01-30T12:21:17Z")

</div>

Where is this from, a package? I’m not working from Shopware or any other existing software but I’m building my own middleware.

---

<div class="post-metadata">

**Author:** ![oliverriske](https://avatars.discourse-cdn.com/v4/letter/o/ebca7d/32.png) [@oliverriske](https://forum.shopware.com/u/oliverriske)\
**Post date:** [30. Januar 2020 um 12:44 UTC](https://forum.shopware.com/t/add-product-images-through-admin-api/64359/4 "2020-01-30T12:44:49Z")

</div>

My code&nbsp;

php with guzzle http&nbsp;

rest is on you

---

<div class="post-metadata">

**Author:** ![WBKNL](https://avatars.discourse-cdn.com/v4/letter/w/e36b37/32.png) [@WBKNL](https://forum.shopware.com/u/WBKNL)\
**Post date:** [30. Januar 2020 um 13:43 UTC](https://forum.shopware.com/t/add-product-images-through-admin-api/64359/5 "2020-01-30T13:43:57Z")

</div>

Thanks for the code but I’m trying to understand how the api works. Can I upload an image or do I have to give it a url? Is there any documentation on this?

Apart from the uploading, if I understand it correctly:

1. I make a post to /media (createMedia) to create a new Media Resource
2. _Somehow get the image to Shopware_
3. Update the Product and give it the media ID

---

<div class="post-metadata">

**Author:** ![oliverriske](https://avatars.discourse-cdn.com/v4/letter/o/ebca7d/32.png) [@oliverriske](https://forum.shopware.com/u/oliverriske)\
**Post date:** [30. Januar 2020 um 13:48 UTC](https://forum.shopware.com/t/add-product-images-through-admin-api/64359/6 "2020-01-30T13:48:22Z")

</div>

Look at the code everything is there

Step 2:

$url = $this-\>baseUrl . ‘/api/v1/\_action/media/’ . $mediaId . ‘/upload?extension=’ . $image[1] . ‘&fileName=’ . $image[0];  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $response = $this-\>postData($url, $data);

&nbsp;

---

<div class="post-metadata">

**Author:** ![na15fabiankoenig](https://avatars.discourse-cdn.com/v4/letter/n/cab0a1/32.png) [@na15fabiankoenig](https://forum.shopware.com/u/na15fabiankoenig)\
**Post date:** [16. März 2020 um 08:57 UTC](https://forum.shopware.com/t/add-product-images-through-admin-api/64359/7 "2020-03-16T08:57:08Z")

</div>

@oliverriske‍ Could you write me your variables, which you are using in your code?

```
 $this-\>cacheMediaFolder $this-\>productMediaFolder $this-\>apiEndpoints $this-\>baseUrl $this-\>log
```

---

<div class="post-metadata">

**Author:** ![FSA](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@FSA](https://forum.shopware.com/u/FSA)\
**Post date:** [2. September 2021 um 16:57 UTC](https://forum.shopware.com/t/add-product-images-through-admin-api/64359/8 "2021-09-02T16:57:37Z")

</div>

sry for the resurrection, but i’m stuck on a problem related to this thread…

i’m currently creating a json for an http request towards the admin api to create a product, i already created a media file and uploaded data to it, i can see the image when i open the product, but i really have no clue how to set one of the added media ids as cover… this is my json

UPDATE: i got it to work, i had to define an id for the media product relationship and use this id to set one of the associations as cover

```auto
{
    "productNumber" : "SCG1",
    "stock" : 0, 
    "price" : [
        {
            "currencyId" : "b7d2554b0ce847cd82f3ac9bd1c0dfca", 
            "gross": 15, 
            "net": 10, 
            "linked" : false,
            "listPrice": {
                "currencyId": "b7d2554b0ce847cd82f3ac9bd1c0dfca",
                "net": 37.81512605042,
                "gross": 45.0,
                "linked": true,
                "listPrice": null,
                "extensions": []
            }
        }
    ],
    "media": [{
        "id": "b6d2554b0ce847cd82f3bd8bd1c0dfca", // random UUID selfgenerated will be used as "coverId"
        "media": { 
            "id": "7cdd383f3fcb4418b5be2a5d11ba9afc",
            "position": 0
        }
    }, {
        "media": { 
            "id": "4aaf1eff97e848d08aae9590cd8b7af1",
            "position": 1
        }
    }],
    "coverId": "b6d2554b0ce847cd82f3bd8bd1c0dfca",
    "manufacturerId": "fc9b37e6bdfe4efb8cedf62725dfddec",
    "deliveryTimeId": "910be68cd9f44e378c1ba8965a4a8896",
    "taxId": "05ee7e68ead64bec8157c24014cfbbfc",
    "restockTime": 14,
    "isCloseout": true,
    "purchaseSteps": 16,
    "maxPurchase": 160,
    "minPurchase": 16,
    "weight": 0.26,
    "width": 78.0,
    "height": 178.0,
    "length": 78.0,
    "unitId": "e2845da8c4534b8fadc28267c0b7fb10",
    "referenceUnit": 1,
    "purchaseUnit": 1,
    "properties": [
        {"id": "3b3401cc9c6a4362baa92a373617e712"},
        {"id": "a2b0cd0eff3748d890453e493e743755"}
    ]
}

```
