Capturing SendGrid Events With ASP.NET WebAPI and Task async

Posted by Peter Kellner on February 12, 2014 · 5 mins read



I’ve been using SendGrid’s API WebHooks to capture email events (Version 1).  This version sends one event at a time which has always been a pretty bad idea.  When I first did the integration a year or so ago, the only way to capture multiple events at once was to consume some proprietary non-JSON like protocol.  I declined.  They finally have upgraded the API to send standard JSON records and are going to obsolete version 1 so I need to change.  Here goes…

Send Grid Event Notification Configuration

First, you need to go into Event Notification and change to (v3) of the config.  Then, I pressed the “Test Your Integration” button while running fiddler to grab the JSON SendGrid generates (see screen shot)


Figure Out The JSON Structure

The generated JSON looks like this:


At this point, there are lots of ways to decode the JSON.  My choice is to us, paste in my json data and it generates the classes I need to encode the JSON data into.


Next, I create a WebAPI controller in ASP.NET and simply have with one method for POST.  I add the above classes and then unwind the data when it comes in as follows.

The controller I build (as a guess) looks like this: (I’m leaving out what I do with the data in the foreach.  Reality is I’m going to stuff it in a database)

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.Ajax.Utilities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

    public class SendGridController : ApiController
        public async Task Post()
            string jsonData = await Request.Content.ReadAsStringAsync();
            dynamic dataList = JArray.Parse(jsonData);

            using (
                var sqlConnection =
                    new SqlConnection(ConfigurationManager.ConnectionStrings["CodeCampSV06"].ConnectionString))

                foreach (var rec in dataList)

                    string eventName = (string) rec["event"] ?? "";
                    string email = (string) rec["email"] ?? "";
                    string category = "";
                    //string category = rec.category != null && rec.category.Count > 0 ? rec.category[0] : "";
                    string url =  (string) rec["url"] ?? "";
                    string generalType =  (string) rec["type"] ?? "";
                    string reason =  (string) rec["reason"] ?? "";
                    string statusString = (string) rec["status"] ?? "";
                    string attempt =(string) rec["attempt"] ?? "";
                    string responseString = (string)rec["response"] ?? "";
                    string emailpkid = (string)rec["emailpkid"] ?? "";

                     // do something with the data!


Testing The WebAPI Rest Controller

To test the controller, I need to simulate a call that SendGrid will be issuing to me.  I do that by using fiddler to compose a new query.  Below is a screen shot of what that composed query looks like on my system.  Notice I’ve set it to POST and I’ve pasted in the content JSON that SendGrid will be sending me (got it from the test post above)


You can now see, when I press “Execute in fiddler” while Visual Studio is running in debug mode, my data is all coming in as I expect.



That’s it! basically we’ve done it.  HTHs.  We’ve build a WebAPI controller using async to capture data coming in as a JSON payload.