.NET Wrapper for the MailChimp v2.0 API, built with MailChimp love ❤️
Note: This is for the 2.0 API (which is now deprecated). If you'd like a client for the 3.0 API, check out this one by Brandon Seydel
Install the NuGet package from the package manager console:
Install-Package MailChimp.NETNext, you will need to provide MailChimp.NET with your API key in code. Need help finding your API key? Check here: http://kb.mailchimp.com/article/where-can-i-find-my-api-key
In your application, call:
// Pass the API key on the constructor:MailChimpManagermc=newMailChimpManager("YourApiKeyHere-us2");// Next, make any API call you'd like:ListResultlists=mc.GetLists();For help and support, first check out the examples below.
If you can't figure out what you need from the examples (or if you're running into a tough problem) you might want to check out the MailChimp support site, or ping the MailChimp API support twitter account.
If you've got a question/bug/feature request for the API wrapper itself, please use Github issues and consider contributing to the project yourself. See the "Making contributions" section for more information on how to contribute.
usingMailChimp;usingMailChimp.Lists;usingMailChimp.Helper;MailChimpManagermc=newMailChimpManager("YourApiKeyHere-us2");ListResultlists=mc.GetLists();// For each listforeach(varlistinlists.Data){// Write out the list name:Debug.WriteLine("Users for the list "+list.Name);// Get the first 100 members of each list:MembersResultresults=mc.GetAllMembersForList(list.Id,"subscribed",0,100);// Write out each member's email address:foreach(varmemberinresults.Data){Debug.WriteLine(member.Email);}}MailChimpManagermc=newMailChimpManager("YourApiKeyHere-us2");// Create the email parameterEmailParameteremail=newEmailParameter(){Email="customeremail@righthere.com"};EmailParameterresults=mc.Subscribe("YourListID",email);// optionally create a class that inherits MergeVar and add any additional merge variable fields:[System.Runtime.Serialization.DataContract]publicclassMyMergeVar:MergeVar{[System.Runtime.Serialization.DataMember(Name="FNAME")]publicstringFirstName{get;set;}[System.Runtime.Serialization.DataMember(Name="LNAME")]publicstringLastName{get;set;}}MyMergeVarmyMergeVars=newMyMergeVar();myMergeVars.Groupings=newList<Grouping>();myMergeVars.Groupings.Add(newGrouping());myMergeVars.Groupings[0].Id=1234;// replace with your grouping idmyMergeVars.Groupings[0].GroupNames=newList<string>();myMergeVars.Groupings[0].GroupNames.Add("Your Group Name");myMergeVars.FirstName="Testy";myMergeVars.LastName="Testerson";MailChimpManagermc=newMailChimpManager("YourApiKeyHere-us2");// Create the email parameterEmailParameteremail=newEmailParameter(){Email="customeremail@righthere.com"};EmailParameterresults=mc.Subscribe("YourListID",email,myMergeVars);// or use the Dictionary to specify the fields and values. // GetMemberInfo will always return the fields and values using the dictionary and not the custom class.MergeVarmyMergeVars=newMergeVar();myMergeVars.Groupings=newList<Grouping>();myMergeVars.Groupings.Add(newGrouping());myMergeVars.Groupings[0].Id=1234;// replace with your grouping idmyMergeVars.Groupings[0].GroupNames=newList<string>();myMergeVars.Groupings[0].GroupNames.Add("Your Group Name");myMergeVars.Add("FNAME","Testy");myMergeVars.Add("LNAME","Testerson");MailChimpManagermc=newMailChimpManager("YourApiKeyHere-us2");// Create the email parameterEmailParameteremail=newEmailParameter(){Email="customeremail@righthere.com"};EmailParameterresults=mc.Subscribe("YourListID",email,myMergeVars);MailChimpManagermc=newMailChimpManager("YourApiKeyHere-us2");ListResultlists=mc.GetLists();// For each listforeach(varlistinlists.Data){Debug.WriteLine("Information for "+list.Name);// Get the location data for each list:List<SubscriberLocation>locations=mc.GetLocationsForList(list.Id);// Write out each of the locations:foreach(varlocationinlocations){Debug.WriteLine("Country: {0} - {2} users, accounts for {1}% of list subscribers",location.Country,location.Percent,location.Total);}}The IMailChimpManager and IMailChimpExportManager interfaces have been included to allow you to easily mock this API for your own testing.
To set up in your dependency injector, bind the interface with a constructor argument passing your API key. This example uses Ninject loading the value from an app setting in the Web.config named 'MailChimpApiKey':
kernel.Bind<IMailChimpManager>().To<MailChimpManager>().WithConstructorArgument("apiKey",ConfigurationManager.AppSettings["MailChimpApiKey"]);If you were to use a framework like Moq you might write something like:
publicclassThingThatDependsOnMailChimpManager{IMailChimpManager_mailChimpManager;publicThingThatDependsOnMailChimpManager(IMailChimpManagermailChimpManager){_mailChimpManager=mailChimpManager;}publicboolDoSomething(){_mailChimpManager.UpdateCampaign("campaignId","name",newobject());returntrue;}}// ArrangeMock<IMailChimpManager>mailChimpManagerMock=newMock<IMailChimpManager>();mailChimpManagerMock.Setup(x =>x.UpdateCampaign(It.IsAny<string>(),It.IsAny<string>(),It.IsAny<object>()).Return(newCampaignUpdateResult());// Actvarthing=newThingThatDependsOnMailChimpManager(mailChimpManagerMock.Object);varresult=thing.DoSomething();// AssertAssert.IsTrue(result);This project is not affiliated with MailChimp. All contributors to this project are unpaid average folks (just like you!) who choose to volunteer their time. If you like MailChimp and want to contribute, we would appreciate your help! To get started, just fork the repo, make your changes and submit a pull request.
Also: If you're reading this and you're from MailChimp, we wouldn't mind some swag.
Here is the progress so far (according to the MailChimp API docs ) :
- Campaigns related: 100% (15 of 15)
- Ecomm related: 100% (3 of 3)
- Folder related: 100% (4 of 4)
- Gallery related: 100% (7 of 7)
- List related: 78% (32 of 41)
- Helper related: 70% (7 of 10)
- Reports related: 27% (5 of 18)
- Templates related: 100% (6 of 6)
- User related: 86% (6 of 7)
- Vip related: 0% (0 of 4)
- Goal related 0% (0 of 2)
- Conversations related 0% (0 of 3)
Overall: 71% (85 of 120)