Twilio +.Net — Incoming and Outgoing Calls
Twilio provides telecommunication functionalities like SMS, Calling, WhatsApp integration, and many more. The goal of this post is to describe Twilio integration with .net and take advantage of Twilio’s amazing features.
Buy New Number
Login to Twilio > Go to Console. Note down Account SID, and Auth Token. Both will be required in future while integrating with .net.
The next step is to buy a new number. Go To Console > Phone Number > Active Numbers > Buy Number. Twilio gives one number free along with a free account and has limited features.
Settings for Incoming call
Go to Console > Active Numbers > Select the number > Set API as webhook
Set .Net API URL as “A Call Comes In” webhook (as shown in the below image). We used ngrok for incoming call testing for localhost. ngrok is a proxy URL associated with .Net API localhost. Any calls coming to ngrok will be redirected to .Net API URL.
.Net API URL: http://localhost:23548/api/home/incomingcall
ngrok URL: [http/https]://subdomain.ngrok.io/api/home/incomingcall
Generate Token for Incoming call
Go to Account Menu > Auth Tokens & API Keys
Create New API Key: Give meaningful name and note down API Key and API Secret. This will be used later while integrating incoming call in code.
Setting for Outgoing Calls
For outgoing calls, we need to create TwiML app. There are 2 ways to create TwiML App (Both are the same, it’s just different menu options).
1. Go to Console > Voice > Manage > TwiML Apps
2. Go to Console > Phone Numbers > Manage > TwiML Apps
Let’s set Outgoing Call API URL as Request URL (same like incoming, we are using ngrok for localhost) — http://localhost:23548/api/home/outgoingcall
Note down TwiML SID for Outgoing call which will be used later in code.
Let’s create a project in .net
To showcase Twilio integration, we are using the .Net 6.0 (you can use the same code for .Net core and .Net framework as well)
Package Name :
Twilio: Version 5.45.0
Twilio.AspNet. Common: Version 5.33.1
Twilio.AspNet.Mvc: Version 5.33.1
To perform Outgoing and incoming calls, we are going to create 2 different methods
- Outgoing Call — http://localhost:23548/api/home/outgoingcall
- Incoming Call — http://localhost:23548/api/home/incomingcall
You can download the sample code from here.
intforce software is a software development firm. With a fresh perspective and dedicated attention to each client, we provide a complete IT solution globally. By defining, designing, and developing solutions tailored to meet our clients’ business objectives, we ensure that our clients get the maximum return on their investment and support them in tackling evolving business and technological issues. Our mission is to provide the best customer service and expertise using the most practical and robust web technologies/software technologies to satisfy our clients’ IT objectives and to provide them with the business and competitive advantage they needed to succeed.