Skip to content

Instantly share code, notes, and snippets.

@siddharthlatest
Forked from yashshah/webhooks.md
Last active November 24, 2015 17:21
Show Gist options
  • Save siddharthlatest/aae09dd6086ce40bf212 to your computer and use it in GitHub Desktop.
Save siddharthlatest/aae09dd6086ce40bf212 to your computer and use it in GitHub Desktop.
Appbase Webhooks + Sendgrid

##Appbase + Sendgrid Webhooks Usecase

Basically in all the cases when you want to send transactional mail (i.e based on some activity), it is perfect fit to use Appbase and Sendgrid together.

  • When you want to notify users if their account information has been changed
  • Mails related to order confirmation, send an email when new entry comes in
  • Welcome mail to the users as soon as they sign up
  • Alert mails

You won't need to run your server to handle the mails. It is advisable to run the webhooks through the backend as you will be passing your API key in the request.

How to use

Basic structure:

POST /<appname>/.percolator/<id>
body: {
	"query": {
		"match_all" : {
		}
	},
	"type": ":typename",
	"webhooks": [
		{
			"method": "POST",
			"url": "http://something.com/hook?stream={{value}}",
			"headers": {
				"header1": "value1"
			},
			"body": {"foo": "bar"}   // body field accepts escaped strings and mustache valid syntax.
		}
	]
}

Few points to note:

  • is the unique ID you need to provide for the webhooks.
  • It is mandatory to have type in the body
  • You're required to mention in the header if you're submitting the body as form/json/xml/plain-text
  • You can use the results you get during your search in the webhook requests. Refer to configureation part for more details.

cURL Example:

curl -XPUT https://qz4ZD8xq1:[email protected]/meetup2/.percolator/3 -d '{ "query": { "match_all" : { } },  "type" : "meetup","webhooks": [ { "method": "POST", "url": "https://api.sendgrid.com/api/mail.send.json", "headers": {"Content-Type" : "application/x-www-form-urlencoded"}, "body": "[email protected]&amp;toname=Yash&amp;subject=Bitcoin Price&amp;text=Mail body text&amp;from=Appbase.io&amp;api_user=yashshah&amp;api_key=appbase12" }] }'

So as you can see in pretty print the Body, search body remains the same, you just add webhooks parameter in the json.

{
  "query": {
    "match_all": {
      
    }
  },
  "type": "meetup",
  "webhooks": [
    {
      "method": "POST",
      "url": "https:\\api.sendgrid.com\api\mail.send.json",
      "headers": {
        "Content-Type": "application\x-www-form-urlencoded"
      },
      "body": "[email protected]&amp;toname=Yash&amp;subject=Bitcoin Price&amp;text=Lets rock and roll now&amp;from=Appbase.io&amp;api_user=yashshah&amp;api_key=appbase12"
    }
  ]
}

Configuration

Body field accepts the escaped strings and mustache valid syntax.

PUT /<appname>/.percolator/1
body: {
	"query": {
		"match" : {
			"price" : 250.57
		}
	},
	"type": ":typename",
	"webhooks": [
		{
			"method": "POST",
			"url": "http://something.com/pricealert/",
			"body": {"email": "The bitcoin price has changed to {{price}}. This is a webhook notification configured from appbase.io"}
		}
	]
}

Here {{price}} is the value that will be return by the search results.

Javascript example

<html>
<head>
	<title>Webhook Example</title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
	<script type="text/javascript">
		var email_id = 'your_email_id_goes_here'
		jQuery.ajax( {
		    url: 'https://scalr.api.appbase.io/meetup2/.percolator/1',
		    type: 'PUT',
		    data: '{ "query": { "match_all" : { } },  "type" : "meetup","webhooks": [ { "method": "POST", "url": "https://api.sendgrid.com/api/mail.send.json", "headers": {"Content-Type" : "application/x-www-form-urlencoded"}, "body": "to=' + email_id  +'&amp;toname=Yash&amp;subject=Bitcoin Price&amp;text=Mail body text&amp;from=Appbase.io&amp;api_user=yashshah&amp;api_key=appbase12" }] }',
		    beforeSend : function( xhr ) {
		        xhr.setRequestHeader( "Authorization", "Basic " + btoa("qz4ZD8xq1" + ":" + "a0edfc7f-5611-46f6-8fe1-d4db234631f3"));
		    },
		    success: function( response ) {
		        console.log(response)
		    }
		} );
		// Uncomment the Below code to delete the webhook
		// jQuery.ajax({
		//     url: 'https://scalr.api.appbase.io/meetup2/.percolator/1',
		//     type: 'DELETE',
		//     data: '',
		//     beforeSend : function(xhr) {
		//         xhr.setRequestHeader( "Authorization", "Basic " + btoa("qz4ZD8xq1" + ":" + "a0edfc7f-5611-46f6-8fe1-d4db234631f3"));
		//     },
		//     success: function(response) {
		//         console.log(response)
		//     }
		// });		
	</script>
</head>
<body>

</body>
</html>

Deleting Webhook requests

If you want to stop any webhook request, delete it by running following command

curl -XDELETE https://<$username:$password>@scalr.api.appbase.io/<app_name>/.percolator/<webhook_id>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment