Webhooks - URLs you expose to handle callbacks - are pivotal to harnessing the power of Twilio in your web application. A powerful concept, webhooks also happen to be very easy to create in your ASP.NET MVC Project. We'll prove it - let's expose some webhooks in ASP.NET MVC and receive some text messages!
Twilio can send your web application an HTTP request when certain events happen, such as an incoming text message to one of your Twilio phone numbers. These requests are called webhooks, or status callbacks. For more, check out our guide to Getting Started with Twilio Webhooks. Find other webhook pages, such as a security guide and an FAQ in the Webhooks section of the docs.
Webhooks and ASP.NET MVC Projects
For one webhook example, when receiving an SMS (text) message, Twilio will call your application with data describing the incoming message. The same process applies for Programmable Voice, Programmable Chat, Programmable Video, and the other Twilio APIs.
This guide will walk you through setting up a basic project for handling incoming webhook requests in your project. The screenshots in the steps shown were made with Visual Studio 2015, but the process is much the same in Visual Studio 2013 and 2017.
Create the Project
Create a new ASP.NET MVC project that can handle incoming requests from Twilio. Open up Visual Studio and select File... New Project. Find the ASP.NET Web Application template for C# as shown here:
Name the project and solution to suit your needs.
After clicking OK, you will be prompted for the ASP.NET Project details:
An "Empty" project is all you need if you will only be handling webhook requests, but be sure to select the MVC option for adding folders and core references. Finally, select “Host in the cloud” if you want to be able to easily publish your app to Azure. Twilio will need a publicly reachable URL to which to send requests and hosting in Azure fits this bill.
Hosting with Azure
If you select the option to host in Azure, the next dialog to pop up will be the Azure Web App Settings:
At this point you will need to login to your Microsoft Azure subscription. Once you’re logged in, you must enter a Web App name that will be globally unique (you will get a green checkmark once you’ve selected a unique name). Keep track of that name for later, as it will be part of the URL for your webhook.
You will likely need to create a new App Service plan and Resource group, unless you’ve already been deploying your own Web Apps to Azure. You can select whatever Region you like and a Database server based on whether you will need one for your app. If you will just be calling other API's, then you likely do not need one.
Once you click OK, Visual Studio will create your new project and publish your app to Azure. You will know it is complete by looking at the "Azure App Service Activity" tab in Visual Studio:
NuGet Packages
To finish off the framing of the project, you need to install a few dependencies via the NuGet Package Manager. This can be most quickly accomplished using the Console, which you can reach by choosing the Tools menu, NuGet Package Manager, and Package Manager Console.
To get the Twilio helper libraries you will need use the following command. It requests the Twilio.AspNet.Mvc package, telling NuGet to get the latest versions of the dependent Twilio package:
Install-Package Twilio.AspNet.Mvc -DependencyVersion HighestMinor
Create a "Hello World" Controller
To explore how to handle webhooks in ASP.NET MVC, this guide will use the Twilio Programmable SMS product. When Twilio receives an SMS message at a phone number that you own it will call your webhook. We can listen for this webhook using an ASP.NET MVC Controller.
Find the “Controllers” folder of your new project in the Solution Explorer:
Right-click the folder and select Add... Controller... Choose an empty MVC 5 Controller:
Name the controller whatever makes sense for your use case:
Update your using
statements to import the Twilio namespaces:
using Twilio.AspNet.Common;using Twilio.AspNet.Mvc;using Twilio.TwiML;
Then, change your controller class to inherit from TwilioController
instead of the default Controller
provided by ASP.NET MVC:
public class HelloWorldController : TwilioController
Next, modify the Index
action method to have the [HttpPost]
attribute, a return value of TwiMLResult
and an SmsRequest
parameter:
[HttpPost]public TwiMLResult Index(SmsRequest request)
The SmsRequest
class is defined in the Twilio.AspNet.Mvc
library and will contain all of the deserialized parameters that Twilio will pass to us. If you were responding to a voice call, then you would use the VoiceRequest
class.
Twilio expects your webhook to return TwiML (XML), but you don’t need to code the XML by hand. You can make use of the MessagingResponse
and VoiceResponse
classes (from the Twilio
library) to build a TwiML response programmatically. These classes contain methods corresponding to the TwiML verbs that Twilio understands.
Update your action method as follows:
You are viewing an outdated version of this SDK.
Show sample response
Example JSON response
Show code
Example ASP.NET MVC Action that responds to a Twilio SMS webhook.
ASP.NET MVC Webhook Example
Example ASP.NET MVC Action that responds to a Twilio SMS webhook.
After adding the “Hello World” message, call the TwiML
function provided by TwilioController
. This is analogous to calling View(myModel)
in a typical ASP.NET MVC controller action.
Test and Debug Locally
Run your application from within Visual Studio to view your application in the browser. In this case, there isn't a default home page for your app:
You might think to add /HelloWorld
(or whatever you named your controller) to the URL in the browser. You would be on the right track, but this will still return a 404
because your browser will be doing a GET
request instead of a POST
request. You can break out a tool like Fiddler or Postman to test POST requests to your newly created controller, but there is a simpler way that’s built right into Windows - PowerShell. Open a new PowerShell window and run the following command:
Invoke-WebRequest http://localhost:XXXXX/HelloWorld -Method POST
Replace the XXXXX with the random port number that Visual Studio assigned to your web app. You can find this in the URL when Visual Studio first launched your app in the browser. Change HelloWorld to whatever you named your controller (without the Controller
suffix).
This will return a PowerShell object with the response from your controller:
StatusCode : 200StatusDescription : OKContent : <?xml version="1.0" encoding="utf-8"?> <Response> <Message>Hello World</Message> </Response>RawContent : ...
You should be able to see the raw XML response we are returning in the Content
property. Pro-tip: you can see just the Content
property with a single command:
(Invoke-WebRequest http://localhost:XXXXX/HelloWorld -Method POST).Content
(Want more detailed information on using PowerShell with HTTP Requests or Twilio? Try our guide to sending HTTP Requests with PowerShell).
Deploy and Test Your ASP.NET MVC App
Publish to Azure
To publish your app to Azure, find the “Azure App Service Activity” tab in Visual Studio and click the Publish button:
Once the publish is complete, you can test your app from PowerShell again:
Invoke-WebRequest https://yourapp.azurewebsites.net/HelloWorld -Method POST
Replace yourapp
with the name you selected for your Azure Web App and HelloWorld
with the name of your controller.
Assuming that works, you can move on to attaching your webhook to a phone number in Twilio. (To learn how to debug your webhook locally without deploying, see our guide on that topic.)
Configure a Twilio Number
If you haven’t already purchased a Twilio phone number, do so now. Once that is done, you can configure the Messaging Webhook like so:
Use the same URL for your Azure Web App that you tested in PowerShell earlier. Be sure to click the SAVE button to save your configuration for the phone number.
Test it out! Send any text message to your number and you should get back the ‘Hello World’ response:
Conclusion
This walkthrough should get you most of the way down the path to handling webhooks in ASP.NET MVC projects. For more in-depth examples, see the complete applications for a variety of Twilio use cases that are documented in our Tutorials.
Rate this page:
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.
FAQs
Can I create my own webhook? ›
You can install webhooks on an organization or on a specific repository. To set up a webhook, go to the settings page of your repository or organization. From there, click Webhooks, then Add webhook. Alternatively, you can choose to build and manage a webhook through the Webhooks API.
How do I create a webhook application? ›- Go to your stack, and click on the “Settings” icon on the left navigation panel.
- Click on Webhooks. ...
- Click on the + New Webhook button located at the top of the page.
- In the Create Webhook page, provide the following webhook details: ...
- Click on the Save button.
- Create a content type and add an entry.
- Create an AWS Lambda Function.
- Create an API Gateway.
- Deploy your API.
- Create a Webhook in Contentstack.
- Try it out.
An application programming interface (API) is a software interface that serves as a bridge between computers and applications. A webhook is a way for one application to deliver data to another app in real-time.
How do I setup a webhook server? ›- Setup steps. Step 1 - Create a new Node.js project. Step 2 - Create a HTTP server. Step 3 - Return the challenge. Step 4 - Webhook endpoint. Main steps in the verification process: Step 5 - Publish your webhook server. Step 6 - Set up webhook in ChatBot. Step 7 - Add the webhook in your Story. ...
- Ready to use examples.
WebHooks is a lightweight HTTP pattern providing a simple pub/sub model for wiring together Web APIs and SaaS services. When an event happens in a service, a notification is sent in the form of an HTTP POST request to registered subscribers.
What is a webhook example? ›Some real-world examples of webhooks include: Automatically receive an email every morning about your first meeting in case you forget to check your calendar. Have Instagram photos upload automatically to Twitter accounts. Configure the doorbell to flash the lights when it rings.
Is webhook post or get? ›By definition, a webhook (also called a web callback or HTTP push API) is a way for an app to provide other applications with real-time information. A webhook delivers data to other applications as it happens, meaning you get data immediately.
How do I create a custom webhook URL? ›- Head over to dev.flock.com and click on the Webhooks section.
- Since we need to post updates to Flock, we need to create an incoming webhook.
- Select the channel in Flock where you would like to receive the updates.
- Click on Save and Generate URL.
- Copy the Flock webhook URL.
- Navigate to your Slack workspace.
- Search for webhook.
- Click Incoming WebHooks.
- Click Add to Slack.
- For the Post to Channel list, select an existing channel or click create a new channel to create a new channel. ...
- Click Add Incoming WebHooks integration.
What is the purpose of webhook? ›
Webhooks typically are used to connect two different applications. When an event happens on the trigger application, it serializes data about that event and sends it to a webhook URL from the action application—the one you want to do something based on the data from the first application.
How do I send data to webhook? ›With webhooks, it's generally a three-step process: Get the webhook URL from the application you want to send data to. Use that URL in the webhook section of the application you want to receive data from. Choose the type of events you want the application to notify you about.
What is a webhook listener? ›A webhook listener is an app that you create that exposes an endpoint URL that will receive event messages from Connect.
What are two use cases where webhooks are effective? ›Webhooks offer lightweight data sharing
Common use cases for webhooks include sending new email list subscriptions and unsubscriptions to a CRM system, automatically updating accounting software when invoices are paid, or setting up any type of notifications.
Conclusion. Both APIs and webhooks have different use cases, but if your goal is to transfer data between two services, webhooks are the way to go. However, if your platform or application would demand frequent data changes, then an API should be used.
What is the difference between webhooks and WebSockets? ›Differences between webhooks and WebSockets
Webhooks are used for one-way communication from a source application to a destination application, while WebSockets facilitate two-way communication between server and client.
- Security threats and solution recap. ...
- Encrypt all data. ...
- Verify the source. ...
- Verify the consumer. ...
- Verify the message. ...
- Prevent replay attacks. ...
- Conclusion.
Working with webhooks exposes an HTTP endpoint that can be called from any actor on your server. Without appropriate measures, this could be extremely unsafe. However, there are now well-understood strategies that ensure your webhook endpoints are secured.
How do you create a webhook token? ›- Click the menu icon on the top left corner of the screen.
- Click Settings.
- Under the App Developer Tools category, click Webhooks. ...
- Click Create Webhook.
- From the Create Webhook page, enter the following information: ...
- Click Save.
- IP address.
- Domain Name System.
- IP address of a Web server.
How do you create a webhook token? ›
- Click the menu icon on the top left corner of the screen.
- Click Settings.
- Under the App Developer Tools category, click Webhooks. ...
- Click Create Webhook.
- From the Create Webhook page, enter the following information: ...
- Click Save.
- Open a new browser tab and login in to your account at Discord.
- Click the gear icon (Edit Channel) of the channel you want to post to.
- Click Webhooks in the left menu.
- Click the Create Webhook button.
- Enter a Name of your choice.
- Click the Copy button of the Webhook URL.
Go to the Integrations page on your Discord server as described above and click Create Webhook. Give your webhook a descriptive name and select the channel you want your messages to be sent to. Remember to click Save Changes when you make any change to your webhook configuration.
How do you write a webhook in Python? ›- Step 1: Install Flask to your Python Environment. The first step in setting up Python Webhook Integration is to install Flask to your Python environment. ...
- Step 2: Create a Web Service. Once you have installed Flask to your Python environment, you need to create a Web Service using Flask. ...
- Step 3: Run the Flask Server.