Blog

Using the Constant Contact API with ASP.NET

Several of my clients use Constant Contact to manage their email newsletter subscription lists.  It’s a cost-effective solution to handle user signups, send and track emails, and manage unsubscribe requests.  The default web form that Constant Contact gives you however, leaves a lot to be desired in the design department.  For direct integration into your website you need to leverage their API which, at the time of this writing, is somewhat poorly documented and doesn’t include any code samples for ASP.NET.

Below is a simple code example of how you can use the API to embed a simple form on your site that will submit a request to Constant Contact and add a user to your list.  I’m sure there is much more you can do with the API but this is the basic requirement for most implementations.

Step one is to request an API key from Constant Contact.  Go to http://developer.constantcontact.com/apikey/login and input your username and password to generate the key that you’ll need for API access.

We can now use this authentication to make an HTTP POST to the Constant Contact server and submit our user’s form data.

Step two will be to setup a form with validation controls that will allow a user to enter their first name, last name, email address, and other fields that you may want to capture (like their company name).  If you’re reading this post I’m assuming you know how to setup an ASP.NET web form. If not, buy Stephen Walther’s book.

Step three is the actual code you need to transmit the data that the user entered into the form.

UPDATE: 12/4/09 – As ‘Urbal the Great’ points out in the comments below, Constant Contact has updated their authentication method so we need to use HTTPS instead of just HTTP for the main URI. The code has been updated below.

Imports System.Net
Imports System.IO

'Setup your variables
Dim sUsername As String = "CONSTANT_CONTACT_USERNAME"
Dim sPassword As String = "CONSTANT_CONTACT_PASSWORD"
Dim sUri As String = "https://api.constantcontact.com/ws/customers/" & sUsername & "/activities"
Dim sListUri As String = "http://api.constantcontact.com/ws/customers/" & sUsername & "/lists/1" 'If you have more than one list, change this number to whichever list you're targeting
Dim sAPIKey As String = "CONSTANT_CONTACT_API_KEY"

'Setup an HttpWebRequest to send the data
Dim address As New Uri(sUri)
Dim request As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest)
request.Credentials = New NetworkCredential((sAPIKey & "%" & sUsername), sPassword)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"

'Build an encoded string of the data to pass to Constant Contact
'More info on this can be found at http://developer.constantcontact.com/doc/activities
Dim data As New StringBuilder()
data.Append("activityType=" + HttpUtility.UrlEncode("SV_ADD", Encoding.UTF8))
data.Append("&data=" + HttpUtility.UrlEncode(("Email Address,Email Type,First Name,Last Name,Company Name" & Chr(10)), Encoding.UTF8))
data.Append(HttpUtility.UrlEncode((txtEmail.Text & ",HTML," & txtFirstName.Text & "," & txtLastName.Text & "," & txtOrganization.Text), Encoding.UTF8))
data.Append("&lists=" + HttpUtility.UrlEncode(sListUri)

'The "guts" of the code to execute the request and return a response
'The response (returned as 'strResponse') will be XML.  You can parse this for status messages if you like, or just ignore it.
Dim byteData As Byte() = UTF8Encoding.UTF8.GetBytes(data.ToString())
Dim st As String = String.Empty
request.ContentLength = byteData.Length
Using postStream As Stream = request.GetRequestStream()
     postStream.Write(byteData, 0, byteData.Length)
End Using
Using response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse)
     Dim reader As New StreamReader(response.GetResponseStream())
     st = reader.ReadToEnd()
End Using

Step four would be to provide the user with some sort of confirmation.  Perhaps redirect them to a “thank you page” or turn some panels on and off to signal completion of the process.

That’s it!  I’m sure there are other ways to do this, probably more elegant ones, and I’d love to hear from anyone who’s done more Constant Contact integration work with .NET.  If you’re just looking to quickly and easily integrate a sign-up form on your site though, this should get you started.

Special thanks to “cbaugh” on the Constant Contact forums who provided a C# example which was the baseline for this VB code example that I’ve documented here.

7 comments on “Using the Constant Contact API with ASP.NET

  1. Tom Mignosa on said:

    Thanks for sharing this example. I have referenced it from the Constant Contact developers forum.
    Tom M

  2. This is exactly what I was looking for. I spent an hour going through the Constant Contact API documentation and then I found your post. You’re awesome! Many thanks!!!

  3. HI
    I have a client who uses constant contact and I want to help him automate gettting ‘leads’ into a new ‘customers’ list once they buy a product. Can constant contact do this? I know aweber has lots of email parsing functions to deal with this kind of automated list management based on actions in third party payment processors…
    thanks
    Dan

  4. Jay Buys on said:

    I’m not an expert in Constant Contact but I don’t see why you couldn’t just add users to a list (assuming they check a box to opt-in) during the time that you process their order using similar code to what I used in this post.
    In my experience the Constant Contact support people are also extremely helpful so it might be worth contacting them to discuss your options.

  5. thespider on said:

    Or if you just want to add your info to and from the list, just do it this way:
    http://www.dotnetspider.com/forum/ViewForum.aspx?ForumId=72083

  6. Jay Buys on said:

    I just want to note that while the method suggested by thespider will work, it is an outdated method. Constant Contact states: “THE SITE VISITOR APIs, while still functional, ARE BEING REPLACED BY THE REST APIs available on this site. The REST APIs provide more functionality than the Site Visitor API, and are the preferred platform for future development. Support for development using the the SiteVisitor APIs will be withdrawn at some point in 2009.”

  7. Urbal the Great on said:

    Just a tip, CC changed their authentication and in order to get this code working you must change the uri’s to “https” as opposed to “http”.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

HTML tags are not allowed.