Skip to content

A library to parse webhook events from Sendgrid. Supports all events, categories and unique args.

License

Notifications You must be signed in to change notification settings

Petteroe/sendgrid-webhooks

 
 

Repository files navigation

Travis Build Status

Sendgrid Webhooks

A library to parse event webhooks from Sendgrid. Contains Parser and a set of strongly typed DTOs. It supports all available webhook events, unique arguments and categories.

Download via NuGet

Install Sendgrid.Webhooks via NuGet package manager (nuget.org)

Install-Package Sendgrid.Webhooks

Usage

Declare WebhookParser and call ParseEvents. This takes in string as JSON received from the HTTP POST callback.

var parser = new WebhookParser();
var events = parser.ParseEvents(json);

The parser returns a polymorphic IList of WebhookEventBase, where each item is a strongly typed Webhook Event.

Example Properties

var webhookEvent = events[0];

//shared base properties
webhookEvent.EventType; //Enum - type of the event as enum
webhookEvent.Categories; //IList<string> - list of categories assigned ot the event
webhookEvent.TimeStamp; //DateTime - datetime of the event converted from unix time
webhookEvent.UniqueParameters; //IDictionary<string, string> - map of key-value unique parameters

//event specific properties
var clickEvent = webhookEvent as ClickEvent; //cast to the parent based on EventType
clickEvent.Url; //string - url on what the user has clicked

Example JSON

[
  {
    "email": "john.doe@sendgrid.com",
    "timestamp": 1337197600,
    "smtp-id": "<4FB4041F.6080505@sendgrid.com>",
    "event": "processed"
  },
  {
    "email": "john.doe@sendgrid.com",
    "timestamp": 1337966815,
    "category": "newuser",
    "event": "click",
    "url": "https://sendgrid.com"
  }
]

Unique Arguments

Is the ability to pass in additional arguments along with the message. This is mainly to add meta-data to the message. Find out more in the documentation.

{
  "unique_args": {
    "customerAccountNumber": "55555",
    "activationAttempt": "1",
    "New Argument 1": "New Value 1",
    "New Argument 2": "New Value 2",
    "New Argument 3": "New Value 3",
    "New Argument 4": "New Value 4"
  }
}

The WebhookParser looks at each unique property within the JSON and adds it to a UniqueParameters dictionary.

var value = event.UniqueParameters["customerAccountNumber"];
Console.WriteLine(value); // outputs 55555

Overriding JsonConverter

The parser supports a custom JsonConverter as an argument. This theoretically allows you to write your own custom DTOs, as long as they are still based on the WebhookEventBase.

About

A library to parse webhook events from Sendgrid. Supports all events, categories and unique args.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%