Search code examples
pythoncreatesend

Getting Subscribers using an API call to Campaign Monitor with Create Send & Python


Aloha Friends,

Apologies for the noobish question, I'm still in my infancy with Python & Programming and I'm trying to find a better way to do a very very manual task.

I would like to get all subscribers from Campaign Monitor using it's API

I've read and re-read the API documentation and arrived at the conclusion that I need to use this snippet of code:

 from __future__ import absolute_import

import json

from createsend.createsend import CreateSendBase, BadRequest
from createsend.utils import json_to_py, validate_consent_to_track


class Subscriber(CreateSendBase):
    """Represents a subscriber and associated functionality."""

    def __init__(self, auth=None, list_id=None, email_address=None):
        self.list_id = list_id
        self.email_address = email_address
        super(Subscriber, self).__init__(auth)

    def get(self, list_id=None, email_address=None, include_tracking_preference=False):
        """Gets a subscriber by list ID and email address."""
        params = {
            "email": email_address or self.email_address,
            "includetrackingpreference": include_tracking_preference,
        }
        response = self._get("/subscribers/%s.json" %
                             (list_id or self.list_id), params=params)
        return json_to_py(response)

I'm a little lost with the code, I'm unsure if I need to apend the create send class above this with the API key, and would the above just give me a full Json list of my subscribers..?

I'm taking a basic API course on Udemy at the moment so I know how to use postman and run basic api calls using Flask, but I've not seen or used Createsend.

Github for the Code here

Documentation here

For anyone reading this I really appreciate your time, (whether you reply or not)!

Yours Sincerely, Datanovice.


Solution

  • greetings from another novice. Came across your post yesterday as I was trying to add a subscriber to a list using their API. I struggled a bit and could only solve it with the help of a senior dev. so don't beat yourself up. I know it might be a tad late but hope it still helps. if you did solve it meanwhile, do let me know :-)

    first of, if you want to get all subscribers to a list, you need to be looking at this part https://www.campaignmonitor.com/api/lists/ The above will probably get you one subscriber at a time. I recommend using something like Postman to do some test GET calls to the API and see which one is getting you the results you need. for example, this is what I get when I made a GET call to all active subscribers to a list:

    {
        "Results": [
            {
                "EmailAddress": "marco@polo.bro",
                "Name": "Marco",
                "Date": "2018-11-13 10:36:00",
                "State": "Active",
                "CustomFields": [],
                "ReadsEmailWith": ""
            },
            {
                "EmailAddress": "marco@polo.broke",
                "Name": "Marco",
                "Date": "2018-11-13 10:38:00",
                "State": "Active",
                "CustomFields": [],
                "ReadsEmailWith": ""
            },
            {
                "EmailAddress": "marco@polo.mkd",
                "Name": "Marco",
                "Date": "2018-11-13 17:22:00",
                "State": "Active",
                "CustomFields": [],
                "ReadsEmailWith": ""
            },
            {
                "EmailAddress": "marco@polo.ro",
                "Name": "Marco",
                "Date": "2018-11-13 09:52:00",
                "State": "Active",
                "CustomFields": [],
                "ReadsEmailWith": ""
            },
            {
                "EmailAddress": "subscriber1@example.com",
                "Name": "New Subscriber",
                "Date": "2018-11-13 16:55:00",
                "State": "Active",
                "CustomFields": [],
                "ReadsEmailWith": ""
            },
            {
                "EmailAddress": "subscriber2@example.com",
                "Name": "New Subscriber 2",
                "Date": "2018-11-13 16:59:00",
                "State": "Active",
                "CustomFields": [],
                "ReadsEmailWith": ""
            }
        ],
        "ResultsOrderedBy": "email",
        "OrderDirection": "asc",
        "PageNumber": 1,
        "PageSize": 1000,
        "RecordsOnThisPage": 6,
        "TotalNumberOfRecords": 6,
        "NumberOfPages": 1
    }
    

    Otherwise, try this (you should create an instance of the Subscriber class):

    from createsend import * 
    
    list_id = 'your-list-id'
    subscriber = Subscriber(
        {'api_key': 'your-api-key'},
        list_id,
        email_address=None
    )
    
    user_email = 'user@example.com'
    subscriber.get(list_id, user_email, include_tracking_preference=True|False)
    

    I think you can also just do subscriber.get() to get the details without any arguments.

    Hope it helps.

    Happy coding :-)