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

jlcarpenter/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

//All delivery events (deliver, bounce, deferred) also contain
var deliveryEvent = webhookEvent as DeliveryEvent; // Cast to the parent based on EventType
deliveryEvent.Ip; //string ip address used to send the event
deliveryEvent.Tls; //bool whether or not TLS was used when sending the email
deliveryEvent.CertificateError; //bool whether there was a certificate error on the receiving side
deliveryEvent.SmtpId; //string id attached to the message by the originating system
	
// Event-specific properties for example
var clickEvent = webhookEvent as ClickEvent; // Cast to the parent based on EventType
clickEvent.Url; // string - URL on what the user has clicked
clickEvent.UrlOffset; //UrlOffset - further info about what link was 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 metadata 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 class.

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%