How to setup Update Event URL for HTTP Post (webhook)?

Basically I would like to get notified the previous and new email address of subscriber when it is updated. Sample code is appreciated?

Thanks,
Rizwan

Phil Phil, 1 year ago

Hi Rizwan,

Apologies for the slow reply. I'm responding here to the combination of the support request and this forum post, because it's more useful to everyone if it's public.

Broadly the process you're talking about has two main components:
1. Creating an endpoint in your application to receive the webhook POSTs from us
2. Setting up and activating the webhook in our API

The second of those is easier to answer, so i'll start there. Once the endpoint exists in your application on a defined URL, you setup the webhook in the API via this route:
https://www.campaignmonitor.com/api/lists/#creating_a_webhook

You'll notice that the post includes the kind of Events you want to subscribe to. In your case, you're interested in "Update" events (all events documented here), so the data you'd send through to the create route would be something like:

{
    "Events": [ "Update" ],
    "Url": "http://yourapplication.com/path/to/webhook",
    "PayloadFormat": "json"
}

Since you're using the C# wrapper, you want to call the CreateWebhook method on a List object. Unfortunately we don't have any sample code there, but if you're familiar with the API for other purposes it shouldn't be hard to figure out.

Of course in order to do this, you need a URL to pass in, and i'll address that in the next reply.

Phil Phil, 1 year ago

Hi again Rizwan.

As mentioned in the previous post, in order to use webhooks you need to be able to create an endpoint in your application which can receive and process POSTed data from us.

Unfortunately we don't have sample code or even helper libraries to assist people with that process.

How you go about doing that is going to depend very much on your preferences for creating web applications. Two main questions to be considered there:
* Do you use WebForms, or ASP.NET MVC, or some other framework?
* Would you prefer to parse XML or JSON?

I won't go too far down the path of sample code then. At this point the broad sequences of steps is something like this:
* Create the new endpoint. It can be an .aspx page, an MVC action, a web API route, whatever. It doesn't matter as long is it can accept HTTP requests of verb POST, and you can parse the body of the request.
* Deserialise the body of the request. You get to decide whether it's going to be XML or JSON and tell our API as the Payload format mentioned in the previous post. The data structure will be as dictated in the "Subscriber Update" event here:https://www.campaignmonitor.com/api/webhooks/#currently_available_webhooks
You can see that the payload there includes "OldEmailAddress" and "EmailAddress"
* Process the parsed request. If OldEmailAddress and EmailAddress are present but different then do something (ie notify yourself), otherwise throw it away
* (i've said "present but different" instead of just "present" because i can't remember if we include OldEmailAddress if the email didn't change),
* Create the webhook as per my first post, subscribing to the Update events
* Activate the webhook (https://www.campaignmonitor.com/api/lists/#activating_a_webhook). Sorry i should have mentioned that in my first post.

As you can see, providing sample code for all of that would not be a small job. Hopefully the wordy explanation is useful.

Please let me know if you have any more questions.

rhkiyani, 1 year ago

Thank you very much Phil. I'll go through your instructions.

rhkiyani, 1 year ago

I would like to share my solution to this problem. After following your detail instruction, I came up with this solution. Because the request will come from campaign monitor, so we can't display the data from code behind, instead I am storing it in text file to view it for this example.

I am using asp.net, C#, CreateSend CSharp wrapper api.

Note: You would need to add required reference libraries.

Step 1:
Create an aspx page. This page must be able to accept HTTP POST request.

Step 2:
Add this code to get HTTP POST data.

File: default.aspx.cs

var reader = new StreamReader(Request.InputStream);
var json = reader.ReadToEnd();

FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;

ostrm = new FileStream(@"C:\logfile4webhook.txt", FileMode.Append, FileAccess.Write);
writer = new StreamWriter(ostrm);
Console.SetOut(writer);
Console.Write(DateTime.Now + " ");
Console.WriteLine(json.ToString() + " ");
Console.SetOut(oldOut);
writer.Close();
ostrm.Close();

Here we are reading data and writing it to text file. If you run the project, nothing will be added to text file because this page hasn't received any post request.

Step 3:
Create webhook. This code can be linked to a button on click event.

File:default.aspx.cs

AuthenticationDetails auth = new ApiKeyAuthenticationDetails("your api key");
string listID = "";
listID = "your list id";
List list = new List(auth, listID);
List<string> events = new List<string>();
events.Add("Update");

string postback = list.CreateWebhook(events, "URL", "json");

FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;

ostrm = new FileStream(@"C:\logfile4webhook.txt", FileMode.Append, FileAccess.Write);
writer = new StreamWriter(ostrm);
Console.SetOut(writer);
Console.Write(DateTime.Now + " ");
Console.WriteLine(postback + " ");
Console.SetOut(oldOut);
writer.Close();
ostrm.Close();

You'll get a webhook id which is now written to text file.

Step 4:
Activate webhook. Copy that webhook id from the text file and past it to the code below.
File:default.aspx.cs

AuthenticationDetails auth = new ApiKeyAuthenticationDetails("your api key");
string listID = "";
listID = "your list id";
List list = new List(auth, listID);
list.ActivateWebhook("webhook id");

Step 5:
Test weebhook.

File: default.aspx.cs

AuthenticationDetails auth = new ApiKeyAuthenticationDetails("your api key");
string listID = "";
listID = "your list id";
List list = new List(auth, listID);
string postback = list.TestWebhook("webhook id").ToString();
FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;

ostrm = new FileStream(@"C:\logfile4webhook.txt", FileMode.Append, FileAccess.Write);
writer = new StreamWriter(ostrm);
Console.SetOut(writer);
Console.Write(DateTime.Now + " ");
Console.WriteLine(postback + " ");
Console.SetOut(oldOut);
writer.Close();
ostrm.Close();

If webhook is able to send post data to the url you added, you'll get 'True' response otherwise error code

Step 6:
Deserialize body of JSON object. We need to create class structure based on JSON data. I put sample json here and it created required classes. http://json2csharp.com/ . Here is what I got.

public class CustomField
{
    public string Key { get; set; }
    public string Value { get; set; }
}

public class Event
{
    public List<CustomField> CustomFields { get; set; }
    public string Date { get; set; }
    public string EmailAddress { get; set; }
    public string Name { get; set; }
    public string SignupIPAddress { get; set; }
    public string Type { get; set; }
}

public class RootObject
{
    public List<Event> Events { get; set; }
    public string ListID { get; set; }
}

Once you have created your class, append the code from step 2 after

var json = reader.ReadToEnd();

to deserialize and parse json.

RootObject myClass = JsonConvert.DeserializeObject<RootObject>(json);

if (myClass != null)
{
    List<Event> t = myClass.Events;

    string old_email = "", new_email = "";
    old_email = t[0].OldEmailAddress;
    new_email = t[0].EmailAddress;

    //now you can do your logic with old_email and new_email
}

Join 200,000 companies around the world that use Campaign Monitor to run email marketing campaigns that deliver results for their business.

Get started for free
1-888-533-8098