Upload Multiple Images

This tutorial explains how to upload more than one image within a single request.

Requirements

For this tutorial you will need both the Python and requests libraries installed.

Procedure

1. Initialize variables

Set the global variables.

import requests
API_DOMAIN = '<your-secure-domain>'
API_KEY = '<your-api-key>'

2. Read image files to upload

This code snippet reads the image files and prepares them to be in the type that is expected for the requests library.

image_filenames = [
    'image1.png',
    'image2.png',
    'image3.png',
]
image_files = []
for image_filename in image_filenames:
    image_files.append(
        ('file', (image_filename, open('/tmp/{}'.format(image_filename), 'rb'), 'image/png'))
    )

3. Perform upload request

Finally, perform an upload request and print its ID and URL.

response = requests.post(
    'https://{}/api/1.3/images'.format(API_DOMAIN),
    params={'api_key': API_KEY},
    files=image_files
)
response.raise_for_status()
image_api_response = response.json()
for image_info in image_api_response:
    print(image_info['id'], image_info['thumbnails']['origin'])