Git Product home page Git Product logo

sendgrid-webhooks's Introduction

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": "[email protected]",
    "timestamp": 1337197600,
    "smtp-id": "<[email protected]>",
    "event": "processed"
  },
  {
    "email": "[email protected]",
    "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.

sendgrid-webhooks's People

Contributors

andymccready avatar brianp101 avatar callumvass avatar kkasias avatar m0sa avatar mirajavora avatar simonluckenuik avatar vanillajonathan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.