Python API: Cannot send a draft campaign

Using createsend API, I'm trying to create a campaign and send it immediately to my list, however it's not working.
Here is how I do it:

    CreateSend.api_key = 'MY API KEY'
    campaign_id = datetime.now().strftime("%m-%d-%Y")
    try:
        campaign = Campaign(campaign_id)
        campaign.create(
            settings.'MY CLIENT ID',
            'Here's todays deals',
            'Daily deal: %s' % campaign_id,
            'Sender name',       
            'myemail@mydomain.com',
            'myemail@mydomain.com',
            'http://www.mysite.com/newsletter.html',
            None,
            ['my_list_id',],
            [],
        )
        campaign.send("myemail@mydomain.com")

I get the following error:

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 378, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded


My template is saved as a draft however it doesn't dispatch emails nor visible under my panel.

Phil Phil, 1 year ago

Hi ulucaydin,

There's at least two problems here.

1. The exception you're seeing is because the API, when a campaign is successfully created, returns a raw string containing the new campaign as the response, ie "{campaignID}". The json parser on your machine is failing to parse that correctly. I'm unsure why at this stage. It works on my machine, running Python 2.7.3 on Windows. It looks like you're running OSX, so i'll have to try that later today and see if can shed any light on it.

2. The second problem is one you haven't seen yet: when you create a campaign, the API returns the ID of the created campaign, which you need to associate with your instance of Campaign before calling Campaign.send. ie:

new_campaign_id = campaign.create(
    settings.'MY CLIENT ID',
    'Here's todays deals',
    'Daily deal: %s' % campaign_id,
    'Sender name',        
    'myemail@mydomain.com',
    'myemail@mydomain.com',
    'http://www.mysite.com/newsletter.html',
    None,
    ['my_list_id',],
    [],
)
campaign.campaign_id = new_campaign_id  # THIS LINE NEEDS TO BE ADDED
campaign.send("myemail@mydomain.com")

Your code is excepting before it even gets that far, but it's a change you'll need to make.

jamesd jamesd, 1 year ago

Here are some details of my Python environment on OSX, which results in createsend-python working as expected:

$ python
Python 2.7 (r27:82500, Sep  4 2010, 09:26:17) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> json.__version__
'2.0.9'

Maybe you can do the same and check your results.

The CI build for this module builds and tests using Python 2.5, 2.6, and 2.7, and actually uses simplejson. The CI build runs on Ubuntu Linux (Travis CI infrastructure).

pip install simplejson

Using simplejson could be an approach for you, however I'd like to know exactly what's causing this issue, because this library should work using either json (where supported) or simplejson (when json can't be imported).

jamesd jamesd, 1 year ago

It is also worth mentioning that the latest version of createsend-python introduced a change so that you no longer need to manually set the ID on newly created objects.

So if you use the latest version of the library, Phil's code above can be slightly simplified to:

campaign.create(
    settings.'MY CLIENT ID',
    'Here's todays deals',
    'Daily deal: %s' % campaign_id,
    'Sender name',        
    'myemail@mydomain.com',
    'myemail@mydomain.com',
    'http://www.mysite.com/newsletter.html',
    None,
    ['my_list_id',],
    [],
)
campaign.send("myemail@mydomain.com")

Hope that helps.

Sign up for free.
Then send campaigns for as little as $9p/m

Create an account