Create a Post with an Slideshow

This tutorial explains how to create an article with a slideshow.

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. Upload images

Please see our Upload Multiple Images tutorial for more information on how to upload images.

3. Initialize particles

The following code snippet shows a list of dictionaries containing information for each slide in the slideshow using the schema expected for the Drafts API and Posts API.

particles = [
    {
        'headline': 'This is the first particle',
        'body': "Some content here!",
        'is_image': True,
        'image_id': image_api_response[0]['id'],
        'media': image_api_response[0]['shortcode'],
        'manual_image_crops': image_api_response[0]['manual_image_crops'],
        'caption': 'First element caption',
    },
    {
        'headline': 'This is the second particle',
        'body': 'More content here',
        'is_image': True,
        'image_id': image_api_response[1]['id'],
        'media': image_api_response[1]['shortcode'],
        'manual_image_crops': image_api_response[1]['manual_image_crops'],
        'caption': 'Second element caption',
    },
    {
        'headline': 'This is the third particle',
        'body': 'Content for this particle',
        'is_image': True,
        'image_id': image_api_response[2]['id'],
        'media': image_api_response[2]['shortcode'],
        'manual_image_crops': image_api_response[2]['manual_image_crops'],
        'caption': 'Third element caption',
    },
]

4. Create a draft

Create a new draft using the Drafts API.

response = requests.post(
    'https://{}/api/1.3/drafts'.format(API_DOMAIN),
    params={'api_key': API_KEY},
    json={
        'headline': 'Article with an slideshow',
    }
)
response.raise_for_status()
draft_api_response = response.json()
print(draft_api_response['id'])

5. Publish the draft

Once the particles schema is done, send it to the Drafts API using the listicle field.

Finally, publish the draft and its URL.

body_content = '''
    <p>This is a HTML piece than can contain shortcodes.</p>
    <listicle id="listicle-{}"></listicle>
    <p>This text goes after the embedded slideshow.</p>
'''.format(draft_api_response['id'])
response = requests.put(
    'https://{}/api/1.3/drafts/{}'.format(API_DOMAIN, draft_api_response['id']),
    params={'api_key': API_KEY},
    json={
        'action': 'publish',
        'body': body_content,
        'listicle': {
            'items': particles,
            'groups': [{
                'slides': [0, 1, 2],
                'settings': {
                    'layout': 'slideshow',
                    'columns': 1,
                    'show_thumbnails': True,
                },
            }],
        },
    }
)
response.raise_for_status()
draft_api_response = response.json()
print(draft_api_response['post_url'])