{"id":8961,"date":"2016-03-31T23:01:44","date_gmt":"2016-04-01T04:01:44","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/buckh\/?p=8961"},"modified":"2016-03-31T23:01:44","modified_gmt":"2016-04-01T04:01:44","slug":"using-vs-team-services-web-hooks-with-azure-functions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/buckh\/using-vs-team-services-web-hooks-with-azure-functions\/","title":{"rendered":"Using VS Team Services Web Hooks with Azure Functions"},"content":{"rendered":"<p>Today at the Build 2016 conference, Azure announced a new feature called <a href=\"https:\/\/azure.microsoft.com\/en-us\/services\/functions\/\">Azure Functions<\/a>. Here\u2019s a quick description from the main page.<\/p>\n<blockquote>\n<p>Azure Functions is an event driven, compute-on-demand experience that extends the existing Azure application platform with capabilities to implement code triggered by events occurring in virtually any Azure or 3rd party service as well as on-premises systems. Azure Functions allows developers to take action by connecting to data sources or messaging solutions, thus making it easy to process and react to events. Azure Functions scale based on demand and you pay only for the resources you consume.<\/p>\n<\/blockquote>\n<p>When I saw it demoed, I thought I\u2019d hook up a <a href=\"https:\/\/www.visualstudio.com\/get-started\/integrate\/service-hooks\/webhooks-and-vso-vs\">VS Team Services Web Hook<\/a> to an Azure Function. Let\u2019s see what that looks like.<\/p>\n<p>First, let\u2019s create an Azure Function.<\/p>\n<p>The easiest way to do that is to use the <a href=\"https:\/\/functions.azure.com\/signin\">Azure Functions Getting Started<\/a> page. I\u2019ve already got one, but let\u2019s create a new one in West US.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image820.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb626.png\" width=\"546\" height=\"480\"><\/a><\/p>\n<p>After clicking Create + get started, we\u2019re taken to this page in the Azure Portal. Let\u2019s click on Webhook + API and click Create a function to get our new Azure Function created.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image821.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb627.png\" width=\"532\" height=\"480\"><\/a><\/p>\n<p>The first time through you should take the guided tour. When you are done with that, it\u2019s the Develop tab you want to be on normally.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image822.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 20px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb628.png\" width=\"463\" height=\"480\"><\/a><\/p>\n<p>Right now we are going to change the type over in the Integrate tab. Change Webhook type to Generic JSON and Save.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image823.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb629.png\" width=\"293\" height=\"480\"><\/a><\/p>\n<p>Before going further with the Azure Function, we need to go over to Visual Studio Team Services and configure a Web Hook. Go to the home page for the team project where you want to configure the Web Hook.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image824.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb630.png\" width=\"640\" height=\"474\"><\/a><\/p>\n<p>Now click the gear icon in the upper right to go to the Admin web UI.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image825.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb631.png\" width=\"640\" height=\"330\"><\/a><\/p>\n<p>Now click on Service Hooks to get to this page.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image826.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb632.png\" width=\"693\" height=\"216\"><\/a><\/p>\n<p>As you can see, I don\u2019t yet have a service hook, so let\u2019s click on Create. We want to select Web Hooks from the dialog that comes up.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image827.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb633.png\" width=\"482\" height=\"480\"><\/a><\/p>\n<p>When we click Next, we see the Trigger, and there are a number of different triggers that we could use. Let\u2019s just use the Build completed trigger for this and click Next.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image828.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb634.png\" width=\"482\" height=\"480\"><\/a><\/p>\n<p>Now we are going to need fill in the URL. It\u2019s the only setting that we have to fill in.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image829.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb635.png\" width=\"485\" height=\"484\"><\/a><\/p>\n<p>Going back to the Azure Portal, you\u2019ll find the Function Url in the Develop tab. Copy and paste that URL into the URL field in the Action page we were just looking at.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image830.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb636.png\" width=\"694\" height=\"145\"><\/a><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image831.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb637.png\" width=\"640\" height=\"396\"><\/a><\/p>\n<p>Before we test this out, we\u2019ll need to change the code in the Develop tab in the Azure Portal. The default code that it generated is looking for fields in the request that don\u2019t exist, and you\u2019ll get an HTTP 500 error if you leave it that way. Replace the code with the following code.<\/p>\n<blockquote>\n<p><font face=\"Courier New\">module.exports = function(context, req) {<br>&nbsp;&nbsp;&nbsp; context.log(&#8216;******************&#8217;);<br>&nbsp;&nbsp;&nbsp; context.log(&#8216;Node.js HTTP trigger function processed a request. id=%s&#8217;, req.id);<\/font><\/p>\n<p><font face=\"Courier New\">&nbsp;&nbsp;&nbsp; context.res = {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; status: 200, \/* Defaults to 200 *\/<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; body: &#8220;Received event of type &#8221; + req.eventType<br>&nbsp;&nbsp;&nbsp; };<\/font><\/p>\n<p><font face=\"Courier New\">&nbsp;&nbsp;&nbsp; context.log(&#8216;Hello = %s&#8217;, req.eventType);<\/font><\/p>\n<p><font face=\"Courier New\">&nbsp;&nbsp;&nbsp; context.done();<br>};<\/font><\/p>\n<\/blockquote>\n<p>Now you\u2019ll need to save the updated code, and you\u2019re Azure Function is ready.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image832.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb638.png\" width=\"644\" height=\"482\"><\/a><\/p>\n<p>Back in VSTS, use the Test button to try it out.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image833.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb639.png\" width=\"485\" height=\"484\"><\/a><\/p>\n<p>If everything has been set up properly, you\u2019ll&nbsp; see that the call succeeded.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image834.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb640.png\" width=\"644\" height=\"431\"><\/a><\/p>\n<p>Clicking on the Request tab you\u2019ll see the payload that is sent in the request going out from VSTS.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image837.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb643.png\" width=\"640\" height=\"427\"><\/a><\/p>\n<p>If you click on the Response tab, you can see the headers from the response from your Azure Function.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image835.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb641.png\" width=\"644\" height=\"429\"><\/a><\/p>\n<p>Back in the Azure Portal, you\u2019ll see the text from the context.log calls in the code over in the Logs view underneath Code on the Develop tab.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image836.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;margin: 0px 0px 0px 30px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/39\/2019\/03\/image_thumb642.png\" width=\"682\" height=\"148\"><\/a><\/p>\n<p>Be sure to save your Web Hook in VSTS, and you\u2019re Azure Function will get called every time a build completes. You now have your first <a href=\"https:\/\/azure.microsoft.com\/en-us\/services\/functions\/\">Azure Function<\/a> connected to a <a href=\"https:\/\/www.visualstudio.com\/get-started\/integrate\/service-hooks\/webhooks-and-vso-vs\">VS Team Services Web Hook<\/a>.<\/p>\n<p>Enjoy!<\/p>\n<p><em>Follow me at <\/em><a href=\"https:\/\/twitter.com\/tfsbuck\"><em>twitter.com\/tfsbuck<\/em><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today at the Build 2016 conference, Azure announced a new feature called Azure Functions. Here\u2019s a quick description from the main page. Azure Functions is an event driven, compute-on-demand experience that extends the existing Azure application platform with capabilities to implement code triggered by events occurring in virtually any Azure or 3rd party service as [&hellip;]<\/p>\n","protected":false},"author":94,"featured_media":10268,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[24],"class_list":["post-8961","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-vsts"],"acf":[],"blog_post_summary":"<p>Today at the Build 2016 conference, Azure announced a new feature called Azure Functions. Here\u2019s a quick description from the main page. Azure Functions is an event driven, compute-on-demand experience that extends the existing Azure application platform with capabilities to implement code triggered by events occurring in virtually any Azure or 3rd party service as [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts\/8961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/users\/94"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/comments?post=8961"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts\/8961\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/media\/10268"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/media?parent=8961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/categories?post=8961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/tags?post=8961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}