# Product Image

This service has upsert logic. If a given `product_sku` exists in the system, the product image will be updated with the given parameters. If there is no image for the product SKU, it will be created. Data that is not included in the request will be deleted even if it was added before.

**Note**: Image URLs must be accessible for "Omnitron" in order for the images to be uploaded to the CDN.

| **Parameter** | **Data Type** | **In** | **Required** | **Description**                                                                                                               |
| ------------- | ------------- | ------ | ------------ | ----------------------------------------------------------------------------------------------------------------------------- |
| api\_token    | string        | header | YES          | [The API key of the customer’s account](https://apidocs.akinon.com/omnitron/getting-started)                                  |
| product\_sku  | string        | body   | YES          | Product SKU                                                                                                                   |
| images        | dict          | body   | YES          | Images dictionary. It has **url** and **order** keys                                                                          |
| url           | string        | body   | YES          | Image URL                                                                                                                     |
| unit\_type    | string        | body   |              | <p>Stocked quantity type.<br>qty: Quantity</p>                                                                                |
| order         | integer       | body   |              | Image order. Default\_value = 0                                                                                               |
| image\_type   | string        | body   |              | Default or swatch. Swatch is a thumbnail image which is used to show product variants on the website. Defaul\_value = default |

## <mark style="color:red;">Insert and Update Images</mark>

#### <mark style="color:red;">**Request POST**</mark>

It is used to replace existing `downloadable_image` objects with new images or to add images to products that do not have images. Images of the product must be sent in a single request. When more than one image is wanted to be added, if any of them is wrong, none of the images will be added.

Downloadable image(s) the product has, but not in the request body, will be deleted.

Downloadable image(s) the product has also in the request body will be updated with `order` and `image_type` values.

Downloadable images just in the request body will be created.

```python

import requests
import json

url = "https://{customer_api_url}/api/i1/downloadable_image/"
api_token = "f532e28df6e4d5fa4648a812018876da1bb9332d"

headers = {
    'content-type': 'application/json',
    'Authorization': 'Token {}'.format(api_token)
}


data = {
    'product_sku': '1182701025',
    'images': [
        {'url': 'https://url/image1.png',
         'order': 0
         'image_type': 'swatch'
        },
        {'url': 'https://url/image2.png',
         'order': 1
        }
    ]
}

response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.text)

```

**Response**

Returns the data of the successfully added images.

```json

{
    "product_sku": "1182701025",
    "images": [
        {
            "id": 676599,
            "url": "https://url/image1.png",
            "order": 0,
            "image_type": "swatch"
        },
        {
            "id": 676600,
            "url": "https://url/image2.png",
            "order": 1,
            "image_type": "default"
        }
    ]
}
```

## <mark style="color:red;">Retrieve Images</mark>

#### <mark style="color:red;">**Request GET**</mark>

In order to access the image data of all products, a request can be made without adding any parameters.

‘content\_type’ header represents the response type.

‘Authorization’ header is a required header for authentication. You can retrieve api\_token with login.

Path: `api/i1/downloadable_image/`

```python

import requests
import json

url = "https://{customer_api_url}/api/i1/downloadable_image/"
api_token = "f532e28df6e4d5fa4648a812018876da1bb9332d"

headers = {
    'content-type': 'application/json',
    'Authorization': 'Token {}'.format(api_token)
}

response = requests.get(url, headers=headers)
print(response.text)

```

**Response**

When the request is sent without adding any parameters, the image data of all products will be listed on the basis of SKU.

“count” shows how many products exist in the system.

“next” shows the next page url to retrieve the desired image data.

“previous” shows the previous page url to retrieve the desired image data.

“results” shows every image property with detailed field descriptions.

Images are listed in the “images” area of each product.

```json

{
  "count": 2,
  "next": null,
  "previous": null,
  "results": [
    {
      "product_sku": "1182701025",
      "images": [
        {
          "id": 676599,
          "url": "https://url/image1.png",
          "order": 0,
          "image_type": "swatch"
        },
        {
          "id": 676600,
          "url": "https://url/image2.png",
          "order": 1,
          "image_type": "default"
        }
      ]
    },
    {
      "product_sku": "7832783278",
      "images": [
        {
          "id": 676601,
          "url": "https://url/image3.png",
          "order": 0,
          "image_type": "swatch"
        },
        {
          "id": 676602,
          "url": "https://url/image4.png",
          "order": 1,
          "image_type": "default"
        }
      ]
    }
  ]
}
```

## <mark style="color:red;">Retrieve All Images for Given SKU</mark>

#### <mark style="color:red;">**Request GET**</mark>

When you want to list images of a particular product, information can be accessed by adding `product_sku`information as a query parameter.

‘content\_type’ header represents the response type.

‘Authorization’ header is a required header for authentication. You can retrieve api\_token with login.

Path: `api/i1/downloadable_image/`

```python

import requests
import json

url = "https://{customer_api_url}/api/i1/downloadable_image/"
api_token = "f532e28df6e4d5fa4648a812018876da1bb9332d"

headers = {
    'content-type': 'application/json',
    'Authorization': 'Token {}'.format(api_token)
}

response = requests.get(url, headers=headers, params={"product_sku": "1182701025"})
print(response.text)

```

**Response**

The ID, URL, order and `image_type` information of the images of the specified product will be listed as a response.

```json

{
  "count": 1,
  "next": null,
  "previous": null,
  "results": [
    {
      "product_sku": "1182701025",
      "images": [
        {
          "id": 676599,
          "url": "https://url/image1.png",
          "order": 0,
          "image_type": "swatch"
        },
        {
          "id": 676600,
          "url": "https://url/image2.png",
          "order": 1,
          "image_type": "default"
        }
      ]
    }
  ]
}
```

## <mark style="color:red;">Delete All Images</mark>

#### <mark style="color:red;">**Request POST**</mark>

Every image for a product with the given SKU can be removed with the following request:

Path: `api/i1/downloadable_image/`

```python

import requests
import json

url = "https://{customer_api_url}/api/i1/downloadable_image/"
api_token = "f532e28df6e4d5fa4648a812018876da1bb9332d"

headers = {
    'content-type': 'application/json',
    'Authorization': 'Token {}'.format(api_token)
}


data = {
    'product_sku': '1182701025',
    'images': [
    ]
}

response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.text)

```

**Response**

If the deletion is successful, the transmitted data will be displayed in the response.

```json

{
  "product_sku": "1182701025",
  "images": [
  ]
}

```

## <mark style="color:red;">Delete a Single Image</mark>

#### <mark style="color:red;">**Request POST**</mark>

To delete a single image for a product, an image list should be sent without the image to be deleted.

Path: `api/i1/downloadable_image/`

```python

import requests
import json

url = "https://{customer_api_url}/api/i1/downloadable_image/"
api_token = "f532e28df6e4d5fa4648a812018876da1bb9332d"

headers = {
    'content-type': 'application/json',
    'Authorization': 'Token {}'.format(api_token)
}

response = requests.get(url, headers=headers, params={"product_sku": "1182701025"})

data = response.json()
data = data["results"][0]
data["images"].pop()

response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.text)

```

**Response**

After the request is sent, all images belonging to the product will be sent as a response.

```json

{
  "product_sku": "1182701025",
  "images": [
    {
      "id": 676599,
      "url": "https://url/image1.png",
      "order": 0,
      "image_type": "default"
    }
  ]
}
```

## <mark style="color:red;">Update Order and Image Type</mark>

#### <mark style="color:red;">**Request POST**</mark>

To update any field, the POST request should be sent with the original data and the updated field.

Default order value is 0 if not sent with request.

Default image type value is default if not sent with request.

The data that already exists but is not included in the request for the update will be deleted.

Path: `api/i1/downloadable_image/`

```python

import requests
import json

url = "https://{customer_api_url}/api/i1/downloadable_image/"
api_token = "f532e28df6e4d5fa4648a812018876da1bb9332d"

headers = {
    'content-type': 'application/json',
    'Authorization': 'Token {}'.format(api_token)
}

response = requests.get(url, headers=headers, params={"product_sku": "1182701025"})

data = response.json()
data = data["results"][0]

data["images"][0]["order"] = 5
data["images"][1]["order"] = 6

response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.text)

```

**Response**

Edited or newly added data will be listed in the response.

```json

{
  "product_sku": "1182701025",
  "images": [
    {
      "id": 676599,
      "url": "https://url/image1.png",
      "order": 5,
      "image_type": "default"
    },
    {
      "id": 676600,
      "url": "https://url/image2.png",
      "order": 6,
      "image_type": "default"
    }
  ]
}
```
