Using Mautic API, how do I send the parameter "lists" when creating an email?

Viewed 1836

The documentation for creating emails using Mautic API is: https://developer.mautic.org/#create-email

I can not create an email without specify the parameter lists. The lists parameter is specified like this:

lists array Array of segment IDs which should be added to the segment email

How can I send the parameter lists via HTTP post using Python so that Mautic API can undestand it?

This creates a email of type "template" (default) in Mautic...

emailData = {    
    'name': 'Email-teste',
    'subject': 'Assunto teste',
    'isPublished': '1',
    'language': 'pt_BR',`enter code here`
    'customHtml' : '<strong>html do email<strong>'
}       

But what I need is to create an email of type "list".

For that, it is mandatory to specify each list ids. Lists are the segments in Mautic.... I have a segment with ID 7!

How can I send the segments IDs to Mautic API using POST (Python requests)?

emailData = {    
    'name': 'Email-teste',
    'subject': 'Assunto teste',
    'emailType': 'list',
    'lists': '7',    
    'isPublished': '1',
    'language': 'pt_BR',
    'customHtml' : '<strong>html do email<strong>'
}       

I tried many ways... and I always get the errror:

u'errors': [{u'code': 400,
              u'details': {u'lists': [u'This value is not valid.']},
              u'message': u'lists: This value is not valid.'}]}

I am sure I have a segment with ID 7, as I can see in Mautic interface.

I am using a modified version of https://github.com/divio/python-mautic

3 Answers

you need to send the data as raw json , here's the example of the request:

def create_contact_mautic(email, firstname, lastname):
    params = {"email": email}
    params.update({"firstname": firstname})
    params.update({"lastname": lastname})
    url = '<your mautic url>/api/contacts/new'
    response = requests.request('POST', url, data=json.dumps(params), headers=headers, auth=('<your login>','<your password>'))
    return response.text

the secret is at data=json.dumps(params), that transforms your parameters into raw json

Related