Create a Post with Multiple Particles¶
This tutorial explains how to create an article with multiple particles.
Procedure¶
1. Initialize variables¶
Set the global variables.
import requests
API_DOMAIN = '<your-secure-domain>'
API_KEY = '<your-api-key>'
2. Initialize particles¶
The following code snippet shows a list of dictionaries using the schema expected for the Drafts API and Posts API.
particles = [
{
'headline': 'This is the first particle',
'body': "Some content here!",
},
{
'headline': 'This is the second particle',
'body': 'More content here',
},
{
'headline': 'This is the third particle',
'body': 'Content for this particle',
},
]
3. 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 3 particles',
}
)
response.raise_for_status()
draft_api_response = response.json()
print(draft_api_response['id'])
4. Publish the draft¶
Once the particles schema is done, send it to the Drafts API using the field
listicle
.
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 image</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,
}
}
)
response.raise_for_status()
draft_api_response = response.json()
print(draft_api_response['post_url'])