Monday, July 13, 2015

Develop a Mailchimp library with Haskell

Mailchimp is a free email marketing service provider. It helps you to design email newsletters, share them on social networks, integrate with services you already use and track your results. 

Haskell is an advanced purely-functional programming language, which has improved performs than other imperative languages. 

I have developed a library in Haskell interacting with the JSON API in Mailchimp, which enables one to do the following tasks with respect to a mailing list in Mailchimp;
  • Add subscribers
  • List subscribers in a particular mailing list
  • List mailing lists for a particular user account
  • Past activities performed on a given mailing list
Before starting off with the implementation, you should understand the functionalities we can perform on a mailing list in Mailchimp. For that, you should create a mailing list in Mailchimp. So, let's first go through the steps of creating a mailing list in Mailchimp (Here I'm referring to version 2.0 API)

Create a mailing list in Mailchimp
  1. After creating an account in Mailchimp, first thing you have to do is the creation of an 'apikey', which provides the authentication purpose in the JSON API in Haskell
    • Navigate to your 'Account'
    • Then go to the 'API keys' from the drop down menu in 'Extras'

    • Then create an API key from the menu
  2. Then create a mailing list according to your/client's preferences
  3. Then try adding couple of subscribers from the web interface and explore the functionalities available

Implementation of library

After that I recommend you to read the Mailchimp API documentation. This one is the Mailchimp version 2.0. Pay a closer attention towards the sample JSON requests and responses. 

Then select the functionality you want in your library, and start coding. In my library I have selected the above mentioned functionalities.

The first step of the implementation is the construction of the EndPoint URL, since all of the method descriptions in the API documentation are relative to it. The format of the end-point URL is as follows; `https://<dc>`, where `<dc>` refers to the data-center prefix. 

The data-center prefix is the last part of your apikey separated by the "-" character. In order to retrieve the end-point URL you need to extract it. Here is a snapshot of my implementation of it;

One of the main sections, that you must pay attention is the exception handling in HTTP requests and responses. 

In my implementation I have used the `catch` method imported from `Control.Exception` package.

As explained in the Hackage documentation the statement which throws the exception is encapsulated within the catch statement, and then in the handler function the thrown `StatusCodeException` is handled by calling the `getResponse` function.

Here the `StatusCodeException` has 3 parameters, they are; Status, ResponseHeaders, and CookieJar as explained in the Hackage documentation.  

These are then passed into `getResponse` function. Using those 3 components it builds a Response of the same format as `withManager $ httpLbs req`.

Here is the link to the GitHub repository of my code, which is a work in progress. I hope to further develop it to support Mailchimp version 3.0 in the future. 


  1. Hello, I would like share with you that whether you're new to email marketing campaigns or already a pro, MailChimp is one of the best options. It offers basic and advanced features that can help you create the best newsletters and emails for your subscribers. The one staple of the best solutions that it doesn't offer is professional design services, but its templates are easy to customize to your liking. Another Email marketing platform . Just try it.

    1. Thanks Alex for the suggestion. Actually this started as part of learning Haskell (the version 2.0), and it came to a pretty good level. So I published it. Yes, MailChimp is a really good email marketing service provider.

  2. This comment has been removed by the author.

  3. I think this article will fully complement you article. PLease continue publishing helpful topics like this. Regards, from Always Open Commerce